comparison 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
comparison
equal deleted inserted replaced
78:7d043a210121 79:8241386a1651
4 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT 4 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT
5 from pgu import gui 5 from pgu import gui
6 6
7 import data 7 import data
8 import tiles 8 import tiles
9 import icons
9 import constants 10 import constants
10 import buildings 11 import buildings
11 import animal 12 import animal
12 13
13 class OpaqueLabel(gui.Label): 14 class OpaqueLabel(gui.Label):
24 class ToolBar(gui.Table): 25 class ToolBar(gui.Table):
25 def __init__(self, gameboard, **params): 26 def __init__(self, gameboard, **params):
26 gui.Table.__init__(self, **params) 27 gui.Table.__init__(self, **params)
27 self.gameboard = gameboard 28 self.gameboard = gameboard
28 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR) 29 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR)
30 self.chicken_counter = OpaqueLabel(" ", color=constants.FG_COLOR)
31 self.killed_foxes = OpaqueLabel(" ", color=constants.FG_COLOR)
29 self.tr() 32 self.tr()
30 self.add(self.cash_counter) 33 self.add(self.cash_counter)
34 self.tr()
35 self.td(icons.CHKN_ICON, align=-1)
36 self.add(self.chicken_counter)
37 self.tr()
38 self.td(icons.KILLED_FOX, align=-1)
39 self.add(self.killed_foxes)
31 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) 40 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
32 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) 41 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
33 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING) 42 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING)
34 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE) 43 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
35 for building_cls in buildings.BUILDINGS: 44 for building_cls in buildings.BUILDINGS:
44 import engine 53 import engine
45 pygame.event.post(engine.START_NIGHT) 54 pygame.event.post(engine.START_NIGHT)
46 55
47 def update_cash_counter(self, amount): 56 def update_cash_counter(self, amount):
48 self.cash_counter.update_value("Groats: %s" % amount) 57 self.cash_counter.update_value("Groats: %s" % amount)
58 self.repaint()
59
60 def update_chicken_counter(self, number):
61 self.chicken_counter.update_value(" %s" % number)
62 self.repaint()
63
64 def update_fox_counter(self, number):
65 self.killed_foxes.update_value(" %s" % number)
49 self.repaint() 66 self.repaint()
50 67
51 def add_tool_button(self, text, tool): 68 def add_tool_button(self, text, tool):
52 button = gui.Button(text) 69 button = gui.Button(text)
53 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool)) 70 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool))
89 TILE_DIMENSIONS = (20, 20) 106 TILE_DIMENSIONS = (20, 20)
90 TOOLBAR_WIDTH = 140 107 TOOLBAR_WIDTH = 140
91 108
92 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] 109 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
93 FENCE = tiles.REVERSE_TILE_MAP['fence'] 110 FENCE = tiles.REVERSE_TILE_MAP['fence']
111 WOODLAND = tiles.REVERSE_TILE_MAP['woodland']
94 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence'] 112 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
95 113
96 def __init__(self): 114 def __init__(self):
97 self.tv = tiles.FarmVid() 115 self.tv = tiles.FarmVid()
98 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) 116 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
103 self.selected_tool = None 121 self.selected_tool = None
104 self.chickens = [] 122 self.chickens = []
105 self.foxes = [] 123 self.foxes = []
106 self.buildings = [] 124 self.buildings = []
107 self.cash = 0 125 self.cash = 0
126 self.killed_foxes = 0
108 self.add_cash(constants.STARTING_CASH) 127 self.add_cash(constants.STARTING_CASH)
109 128
110 self.fix_buildings() 129 self.fix_buildings()
111 130
112 def create_disp(self): 131 def create_disp(self):
231 fox.move(self) 250 fox.move(self)
232 251
233 def add_chicken(self, chicken): 252 def add_chicken(self, chicken):
234 self.chickens.append(chicken) 253 self.chickens.append(chicken)
235 self.tv.sprites.append(chicken) 254 self.tv.sprites.append(chicken)
255 self.toolbar.update_chicken_counter(len(self.chickens))
236 256
237 def add_fox(self, fox): 257 def add_fox(self, fox):
238 self.foxes.append(fox) 258 self.foxes.append(fox)
239 self.tv.sprites.append(fox) 259 self.tv.sprites.append(fox)
240 260
249 269
250 def remove_chicken(self, chick): 270 def remove_chicken(self, chick):
251 if chick in self.chickens: 271 if chick in self.chickens:
252 self.chickens.remove(chick) 272 self.chickens.remove(chick)
253 self.tv.sprites.remove(chick) 273 self.tv.sprites.remove(chick)
274 self.toolbar.update_chicken_counter(len(self.chickens))
254 275
255 def remove_building(self, building): 276 def remove_building(self, building):
256 if building in self.buildings: 277 if building in self.buildings:
257 self.buildings.remove(building) 278 self.buildings.remove(building)
258 self.tv.sprites.remove(building) 279 self.tv.sprites.remove(building)