comparison gamelib/gameboard.py @ 64:99fbb652ce8d

Refactor buildings so that new ones can be added just by adding a class to buildings.py.
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 20:25:11 +0000
parents e2631c8e2cd6
children 7e9c8ad06d32
comparison
equal deleted inserted replaced
63:1047ccd22dac 64:99fbb652ce8d
26 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR) 26 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR)
27 self.tr() 27 self.tr()
28 self.add(self.cash_counter) 28 self.add(self.cash_counter)
29 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN) 29 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
30 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG) 30 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
31 self.add_tool_button("Sell building", constants.TOOL_SELL_BUILDING)
31 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE) 32 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
32 self.add_tool_button("Buy henhouse", constants.TOOL_BUY_HENHOUSE) 33 for building_cls in buildings.BUILDINGS:
33 self.add_tool_button("Buy guard tower", constants.TOOL_BUY_GUARDTOWER) 34 self.add_tool_button("Buy %s" % (building_cls.NAME,), building_cls)
34 35
35 def update_cash_counter(self, amount): 36 def update_cash_counter(self, amount):
36 self.cash_counter.update_value("Groats: %s" % amount) 37 self.cash_counter.update_value("Groats: %s" % amount)
37 self.repaint() 38 self.repaint()
38 39
85 self.create_disp() 86 self.create_disp()
86 87
87 self.selected_tool = None 88 self.selected_tool = None
88 self.chickens = [] 89 self.chickens = []
89 self.foxes = [] 90 self.foxes = []
90 self.henhouses = [] 91 self.buildings = []
91 self.guardtowers = []
92 self.cash = 0 92 self.cash = 0
93 self.add_cash(constants.STARTING_CASH) 93 self.add_cash(constants.STARTING_CASH)
94 94
95 def create_disp(self): 95 def create_disp(self):
96 width, height = pygame.display.get_surface().get_size() 96 width, height = pygame.display.get_surface().get_size()
121 self.sell_chicken(e.pos) 121 self.sell_chicken(e.pos)
122 elif self.selected_tool == constants.TOOL_SELL_EGG: 122 elif self.selected_tool == constants.TOOL_SELL_EGG:
123 pass 123 pass
124 elif self.selected_tool == constants.TOOL_BUY_FENCE: 124 elif self.selected_tool == constants.TOOL_BUY_FENCE:
125 self.buy_fence(self.tv.screen_to_tile(e.pos)) 125 self.buy_fence(self.tv.screen_to_tile(e.pos))
126 elif self.selected_tool == constants.TOOL_BUY_HENHOUSE: 126 elif buildings.is_building(self.selected_tool):
127 self.buy_henhouse(self.tv.screen_to_tile(e.pos)) 127 building_cls = self.selected_tool
128 elif self.selected_tool == constants.TOOL_BUY_GUARDTOWER: 128 tile_pos = self.tv.screen_to_tile(e.pos)
129 self.buy_guardtower(self.tv.screen_to_tile(e.pos)) 129 building = building_cls(tile_pos)
130 self.buy_building(building)
130 131
131 def get_chicken(self, pos): 132 def get_chicken(self, pos):
132 for chick in self.chickens: 133 for chick in self.chickens:
133 if chick.rect.collidepoint(pos): 134 if chick.rect.collidepoint(pos):
134 return chick 135 return chick
151 print "You can't afford a fence." 152 print "You can't afford a fence."
152 return 153 return
153 self.add_cash(-constants.BUY_PRICE_FENCE) 154 self.add_cash(-constants.BUY_PRICE_FENCE)
154 self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence']) 155 self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence'])
155 156
156 def buy_henhouse(self, tile_pos): 157 def buy_building(self, building):
157 if self.cash < constants.BUY_PRICE_HENHOUSE: 158 if self.cash < building.buy_price():
158 return 159 return
159 henhouse = buildings.HenHouse(tile_pos) 160 if building.place(self.tv):
160 if henhouse.place(self.tv): 161 self.add_cash(-building.buy_price())
161 self.add_cash(-constants.BUY_PRICE_HENHOUSE) 162 self.add_building(building)
162 self.add_henhouse(henhouse)
163
164 def buy_guardtower(self, tile_pos):
165 if self.cash < constants.BUY_PRICE_GUARDTOWER:
166 return
167 guardtower = buildings.GuardTower(tile_pos)
168 if guardtower.place(self.tv):
169 self.add_cash(-constants.BUY_PRICE_GUARDTOWER)
170 self.add_guardtower(guardtower)
171 163
172 def event(self, e): 164 def event(self, e):
173 if e.type == KEYDOWN: 165 if e.type == KEYDOWN:
174 if e.key == K_UP: 166 if e.key == K_UP:
175 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) 167 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])
197 189
198 def add_fox(self, fox): 190 def add_fox(self, fox):
199 self.foxes.append(fox) 191 self.foxes.append(fox)
200 self.tv.sprites.append(fox) 192 self.tv.sprites.append(fox)
201 193
202 def add_henhouse(self, henhouse): 194 def add_building(self, building):
203 self.henhouses.append(henhouse) 195 self.buildings.append(building)
204 self.tv.sprites.append(henhouse) 196 self.tv.sprites.append(building)
205
206 def add_guardtower(self, guardtower):
207 self.guardtowers.append(guardtower)
208 self.tv.sprites.append(guardtower)
209 197
210 def remove_fox(self, fox): 198 def remove_fox(self, fox):
211 if fox in self.foxes: 199 if fox in self.foxes:
212 self.foxes.remove(fox) 200 self.foxes.remove(fox)
213 self.tv.sprites.remove(fox) 201 self.tv.sprites.remove(fox)