comparison gamelib/gameboard.py @ 66:edc15ce8fa30

Implement fence selling (a bit hackish, but fine for now).
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 20:57:20 +0000
parents 7e9c8ad06d32
children 9171d9b9ab35
comparison
equal deleted inserted replaced
65:7e9c8ad06d32 66:edc15ce8fa30
76 76
77 class GameBoard(object): 77 class GameBoard(object):
78 TILE_DIMENSIONS = (20, 20) 78 TILE_DIMENSIONS = (20, 20)
79 TOOLBAR_WIDTH = 140 79 TOOLBAR_WIDTH = 140
80 80
81 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
82 FENCE = tiles.REVERSE_TILE_MAP['fence']
83
81 def __init__(self): 84 def __init__(self):
82 self.tv = tiles.FarmVid() 85 self.tv = tiles.FarmVid()
83 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) 86 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
84 self.tv.png_folder_load_tiles(data.filepath('tiles')) 87 self.tv.png_folder_load_tiles(data.filepath('tiles'))
85 self.tv.tga_load_level(data.filepath('level1.tga')) 88 self.tv.tga_load_level(data.filepath('level1.tga'))
143 return 146 return
144 self.add_cash(constants.SELL_PRICE_CHICKEN) 147 self.add_cash(constants.SELL_PRICE_CHICKEN)
145 self.remove_chicken(chick) 148 self.remove_chicken(chick)
146 149
147 def buy_fence(self, tile_pos): 150 def buy_fence(self, tile_pos):
148 if self.tv.get(tile_pos) != tiles.REVERSE_TILE_MAP['grassland']: 151 if self.tv.get(tile_pos) != self.GRASSLAND:
149 return 152 return
150 if self.cash < constants.BUY_PRICE_FENCE: 153 if self.cash < constants.BUY_PRICE_FENCE:
151 print "You can't afford a fence." 154 print "You can't afford a fence."
152 return 155 return
153 self.add_cash(-constants.BUY_PRICE_FENCE) 156 self.add_cash(-constants.BUY_PRICE_FENCE)
154 self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence']) 157 self.tv.set(tile_pos, self.FENCE)
158
159 def sell_fence(self, tile_pos):
160 if self.tv.get(tile_pos) != self.FENCE:
161 return
162 self.add_cash(constants.SELL_PRICE_FENCE)
163 self.tv.set(tile_pos, self.GRASSLAND)
155 164
156 def buy_building(self, tile_pos, building_cls): 165 def buy_building(self, tile_pos, building_cls):
157 building = building_cls(tile_pos) 166 building = building_cls(tile_pos)
158 if self.cash < building.buy_price(): 167 if self.cash < building.buy_price():
159 return 168 return
160 if building.place(self.tv): 169 if building.place(self.tv):
161 self.add_cash(-building.buy_price()) 170 self.add_cash(-building.buy_price())
162 self.add_building(building) 171 self.add_building(building)
163 172
164 def sell_building(self, tile_pos): 173 def sell_building(self, tile_pos):
174 if self.tv.get(tile_pos) == self.FENCE:
175 return self.sell_fence(tile_pos)
165 for building in self.buildings: 176 for building in self.buildings:
166 if building.covers(tile_pos): 177 if building.covers(tile_pos):
167 self.add_cash(building.sell_price()) 178 self.add_cash(building.sell_price())
168 building.remove(self.tv) 179 building.remove(self.tv)
169 self.remove_building(building) 180 self.remove_building(building)
181 break
170 182
171 def event(self, e): 183 def event(self, e):
172 if e.type == KEYDOWN: 184 if e.type == KEYDOWN:
173 if e.key == K_UP: 185 if e.key == K_UP:
174 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) 186 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])