annotate gamelib/gameboard.py @ 79:8241386a1651

Add chicken counter
author Neil Muller <drnlmuller@gmail.com>
date Tue, 01 Sep 2009 11:20:14 +0000
parents 65958516c7d9
children ad9d1bc7ef0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
1 import random
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
2
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
3 import pygame
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
4 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
5 from pgu import gui
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7 import data
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
8 import tiles
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
9 import icons
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
10 import constants
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
11 import buildings
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
12 import animal
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
13
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
14 class OpaqueLabel(gui.Label):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
15 def paint(self, s):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
16 s.fill(self.style.background)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
17 gui.Label.paint(self, s)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
18
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
19 def update_value(self, value):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
20 self.value = value
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
21 self.style.width, self.style.height = self.font.size(self.value)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
22 self.repaint()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
23
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
24
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
25 class ToolBar(gui.Table):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
26 def __init__(self, gameboard, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
27 gui.Table.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
28 self.gameboard = gameboard
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
29 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR)
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
30 self.chicken_counter = OpaqueLabel(" ", color=constants.FG_COLOR)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
31 self.killed_foxes = OpaqueLabel(" ", color=constants.FG_COLOR)
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
32 self.tr()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
33 self.add(self.cash_counter)
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
34 self.tr()
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
35 self.td(icons.CHKN_ICON, align=-1)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
36 self.add(self.chicken_counter)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
37 self.tr()
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
38 self.td(icons.KILLED_FOX, align=-1)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
39 self.add(self.killed_foxes)
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
40 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
41 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
42 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING)
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
43 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
44 for building_cls in buildings.BUILDINGS:
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
45 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
46
68
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
47 day_done_button = gui.Button("Finished Day")
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
48 day_done_button.connect(gui.CLICK, self.day_done)
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
49 self.tr()
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
50 self.td(day_done_button, style={"padding_top": 30})
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
51
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
52 def day_done(self):
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
53 import engine
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
54 pygame.event.post(engine.START_NIGHT)
6b8ac424da83 Add button for finishing day. Remove debugging print.
Simon Cross <hodgestar@gmail.com>
parents: 67
diff changeset
55
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
56 def update_cash_counter(self, amount):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
57 self.cash_counter.update_value("Groats: %s" % amount)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
58 self.repaint()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
59
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
60 def update_chicken_counter(self, number):
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
61 self.chicken_counter.update_value(" %s" % number)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
62 self.repaint()
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
63
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
64 def update_fox_counter(self, number):
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
65 self.killed_foxes.update_value(" %s" % number)
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
66 self.repaint()
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
67
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
68 def add_tool_button(self, text, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
69 button = gui.Button(text)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
70 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool))
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
71 self.tr()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
72 self.add(button)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
73
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
74
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
75 class VidWidget(gui.Widget):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
76 def __init__(self, gameboard, vid, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
77 gui.Widget.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
78 self.gameboard = gameboard
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
79 vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
80 self.vid = vid
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
81 self.width = params.get('width', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
82 self.height = params.get('height', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
83
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
84 def paint(self, surface):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
85 self.vid.paint(surface)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
86
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
87 def update(self, surface):
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
88 return self.vid.update(surface)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
89
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
90 def resize(self, width=0, height=0):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
91 if width is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
92 self.width = width
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
93 if height is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
94 self.height = height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
95 return self.width, self.height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
96
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
97 def move_view(self, x, y):
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
98 self.vid.view.move_ip((x, y))
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
99
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
100 def event(self, e):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
101 if e.type == MOUSEBUTTONDOWN:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
102 self.gameboard.use_tool(e)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
103
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
104
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
105 class GameBoard(object):
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
106 TILE_DIMENSIONS = (20, 20)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
107 TOOLBAR_WIDTH = 140
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
108
66
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
109 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
110 FENCE = tiles.REVERSE_TILE_MAP['fence']
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
111 WOODLAND = tiles.REVERSE_TILE_MAP['woodland']
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
112 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
66
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
113
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
114 def __init__(self):
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
115 self.tv = tiles.FarmVid()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
116 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
117 self.tv.png_folder_load_tiles(data.filepath('tiles'))
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
118 self.tv.tga_load_level(data.filepath('level1.tga'))
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
119 self.create_disp()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
120
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
121 self.selected_tool = None
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
122 self.chickens = []
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
123 self.foxes = []
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
124 self.buildings = []
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
125 self.cash = 0
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
126 self.killed_foxes = 0
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
127 self.add_cash(constants.STARTING_CASH)
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
128
67
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
129 self.fix_buildings()
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
130
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
131 def create_disp(self):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
132 width, height = pygame.display.get_surface().get_size()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
133 tbl = gui.Table()
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
134 tbl.tr()
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
135 self.toolbar = ToolBar(self)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
136 tbl.td(self.toolbar, width=self.TOOLBAR_WIDTH)
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
137 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height)
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
138 tbl.td(self.tvw)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
139 self.disp = gui.App()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
140 self.disp.init(tbl)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
141
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
142 def paint(self, screen):
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
143 self.disp.paint(screen)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
144
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
145 def update(self, screen):
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
146 self.tvw.reupdate()
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
147 return self.disp.update(screen)
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
148
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
149 def loop(self):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
150 self.tv.loop()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
151
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
152 def set_selected_tool(self, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
153 self.selected_tool = tool
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
154
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
155 def in_bounds(self, pos):
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
156 """Check if a position is within the game boundaries"""
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
157 if pos.x < 0 or pos.y < 0:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
158 return False
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
159 width, height = self.tv.size
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
160 if pos.x >= width or pos.y >= height:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
161 return False
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
162 return True
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
163
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
164 def use_tool(self, e):
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
165 if self.selected_tool == constants.TOOL_SELL_CHICKEN:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
166 self.sell_chicken(e.pos)
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
167 elif self.selected_tool == constants.TOOL_SELL_EGG:
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
168 pass
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
169 elif self.selected_tool == constants.TOOL_BUY_FENCE:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
170 self.buy_fence(self.tv.screen_to_tile(e.pos))
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
171 elif self.selected_tool == constants.TOOL_SELL_BUILDING:
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
172 self.sell_building(self.tv.screen_to_tile(e.pos))
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
173 elif buildings.is_building(self.selected_tool):
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
174 self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
175
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
176 def get_chicken(self, pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
177 for chick in self.chickens:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
178 if chick.rect.collidepoint(pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
179 return chick
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
180 return None
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
181
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
182 def sell_chicken(self, pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
183 chick = self.get_chicken(pos)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
184 if chick is None:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
185 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
186 if len(self.chickens) == 1:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
187 print "You can't sell your last chicken!"
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
188 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
189 self.add_cash(constants.SELL_PRICE_CHICKEN)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
190 self.remove_chicken(chick)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
191
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
192 def buy_fence(self, tile_pos):
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
193 this_tile = self.tv.get(tile_pos)
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
194 if this_tile not in [self.GRASSLAND, self.BROKEN_FENCE]:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
195 return
77
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
196 if this_tile == self.GRASSLAND:
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
197 cost = constants.BUY_PRICE_FENCE
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
198 else:
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
199 cost = constants.REPAIR_PRICE_FENCE
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
200 if self.cash < cost:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
201 print "You can't afford a fence."
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
202 return
77
65958516c7d9 Implement separate fence repair cost (currently 25 groats).
Simon Cross <hodgestar@gmail.com>
parents: 73
diff changeset
203 self.add_cash(-cost)
66
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
204 self.tv.set(tile_pos, self.FENCE)
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
205
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
206 def sell_fence(self, tile_pos):
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
207 if self.tv.get(tile_pos) != self.FENCE:
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
208 return
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
209 self.add_cash(constants.SELL_PRICE_FENCE)
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
210 self.tv.set(tile_pos, self.GRASSLAND)
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
211
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
212 def buy_building(self, tile_pos, building_cls):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
213 building = building_cls(tile_pos)
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
214 if self.cash < building.buy_price():
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
215 return
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
216 if building.place(self.tv):
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
217 self.add_cash(-building.buy_price())
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
218 self.add_building(building)
60
e2631c8e2cd6 Implement guard towers (with temporary sprite PNG).
Simon Cross <hodgestar@gmail.com>
parents: 57
diff changeset
219
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
220 def sell_building(self, tile_pos):
66
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
221 if self.tv.get(tile_pos) == self.FENCE:
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
222 return self.sell_fence(tile_pos)
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
223 for building in self.buildings:
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
224 if building.covers(tile_pos):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
225 self.add_cash(building.sell_price())
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
226 building.remove(self.tv)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
227 self.remove_building(building)
66
edc15ce8fa30 Implement fence selling (a bit hackish, but fine for now).
Simon Cross <hodgestar@gmail.com>
parents: 65
diff changeset
228 break
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
229
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
230 def event(self, e):
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
231 if e.type == KEYDOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
232 if e.key == K_UP:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
233 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
234 if e.key == K_DOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
235 self.tvw.move_view(0, self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
236 if e.key == K_LEFT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
237 self.tvw.move_view(-self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
238 if e.key == K_RIGHT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
239 self.tvw.move_view(self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
240 else:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
241 self.disp.event(e)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
242
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
243 def clear_foxes(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
244 for fox in self.foxes:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
245 self.tv.sprites.remove(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
246 self.foxes = [] # Remove all the foxes
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
247
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
248 def move_foxes(self):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
249 for fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
250 fox.move(self)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
251
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
252 def add_chicken(self, chicken):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
253 self.chickens.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
254 self.tv.sprites.append(chicken)
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
255 self.toolbar.update_chicken_counter(len(self.chickens))
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
256
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
257 def add_fox(self, fox):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
258 self.foxes.append(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
259 self.tv.sprites.append(fox)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
260
64
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
261 def add_building(self, building):
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
262 self.buildings.append(building)
99fbb652ce8d Refactor buildings so that new ones can be added just by adding a class to buildings.py.
Simon Cross <hodgestar@gmail.com>
parents: 60
diff changeset
263 self.tv.sprites.append(building)
60
e2631c8e2cd6 Implement guard towers (with temporary sprite PNG).
Simon Cross <hodgestar@gmail.com>
parents: 57
diff changeset
264
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
265 def remove_fox(self, fox):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
266 if fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
267 self.foxes.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
268 self.tv.sprites.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
269
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
270 def remove_chicken(self, chick):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
271 if chick in self.chickens:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
272 self.chickens.remove(chick)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
273 self.tv.sprites.remove(chick)
79
8241386a1651 Add chicken counter
Neil Muller <drnlmuller@gmail.com>
parents: 77
diff changeset
274 self.toolbar.update_chicken_counter(len(self.chickens))
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
275
65
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
276 def remove_building(self, building):
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
277 if building in self.buildings:
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
278 self.buildings.remove(building)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
279 self.tv.sprites.remove(building)
7e9c8ad06d32 Implement building selling.
Simon Cross <hodgestar@gmail.com>
parents: 64
diff changeset
280
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
281 def add_cash(self, amount):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
282 self.cash += amount
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
283 self.toolbar.update_cash_counter(self.cash)
67
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
284
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
285 def update_chickens(self):
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
286 """Update the chickens state at the start of the new day"""
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
287 # Currently random chickens appear
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
288 # Very simple, we walk around the tilemap, and, for each farm tile,
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
289 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
290 # or we run out of board
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
291 x, y = 0, 0
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
292 width, height = self.tv.size
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
293 while len(self.chickens) < 5:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
294 if x < width:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
295 tile = self.tv.get((x, y))
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
296 else:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
297 y += 1
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
298 if y >= height:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
299 break
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
300 x = 0
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
301 continue
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
302 # See if we place a chicken
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
303 if 'grassland' == tiles.TILE_MAP[tile]:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
304 # Farmland
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
305 roll = random.randint(1, 20)
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
306 # We don't place within a tile of the fence, this is to make things
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
307 # easier
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
308 for xx in range(x-1, x+2):
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
309 if xx >= width or xx < 0:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
310 continue
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
311 for yy in range(y-1, y+2):
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
312 if yy >= height or yy < 0:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
313 continue
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
314 neighbour = self.tv.get((xx, yy))
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
315 if 'fence' == tiles.TILE_MAP[neighbour]:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
316 # Fence
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
317 roll = 10
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
318 if roll == 1:
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
319 # Create a chicken
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
320 chick = animal.Chicken((x, y))
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
321 self.add_chicken(chick)
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
322 x += 1
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
323
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
324 def spawn_foxes(self):
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
325 """The foxes come at night, and this is where they come from."""
73
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
326 # Foxes spawn just outside the map
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
327 x, y = 0, 0
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
328 width, height = self.tv.size
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
329 new_foxes = random.randint(3, 7)
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
330 while len(self.foxes) < new_foxes:
73
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
331 side = random.randint(0, 3)
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
332 if side == 0:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
333 # top
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
334 y = -1
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
335 x = random.randint(-1, width)
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
336 elif side == 1:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
337 # bottom
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
338 y = height
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
339 x = random.randint(-1, width)
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
340 elif side == 2:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
341 # left
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
342 x = -1
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
343 y = random.randint(-1, height)
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
344 else:
73
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
345 x = width
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
346 y = random.randint(-1, height)
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
347 skip = False
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
348 for other_fox in self.foxes:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
349 if other_fox.pos.x == x and other_fox.pos.y == y:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
350 skip = True # Choose a new position
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
351 break
73
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
352 if not skip:
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
353 fox = animal.Fox((x, y))
f3ce3346e25e Spawn foxes jsut outside the map
Neil Muller <drnlmuller@gmail.com>
parents: 69
diff changeset
354 self.add_fox(fox)
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 68
diff changeset
355
67
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
356 def fix_buildings(self):
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
357 """Go through the level map looking for buildings that haven't
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
358 been added to self.buildings and adding them.
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
359
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
360 Where partial buildings exist (i.e. places where the building
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
361 cannot fit on the available tiles) the building is added anyway
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
362 to the top left corner.
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
363
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
364 Could be a lot faster.
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
365 """
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
366 tile_to_building = dict((b.TILE_NO, b) for b in buildings.BUILDINGS)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
367
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
368 w, h = self.tv.size
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
369 for x in xrange(w):
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
370 for y in xrange(h):
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
371 tile_pos = (x, y)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
372 tile_no = self.tv.get(tile_pos)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
373 if tile_no not in tile_to_building:
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
374 continue
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
375
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
376 covered = False
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
377 for building in self.buildings:
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
378 if building.covers(tile_pos):
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
379 covered = True
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
380 break
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
381
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
382 if covered:
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
383 continue
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
384
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
385 building_cls = tile_to_building[tile_no]
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
386 building = building_cls(tile_pos)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
387 building.remove(self.tv)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
388 building.place(self.tv)
9171d9b9ab35 Sanity check and fix up buildings on map after loading.
Simon Cross <hodgestar@gmail.com>
parents: 66
diff changeset
389 self.add_building(building)