comparison gamelib/gameboard.py @ 69:18db99fda6bd

Move spawing code from engine to gameboard - seems more natural.
author Neil Muller <drnlmuller@gmail.com>
date Mon, 31 Aug 2009 22:14:12 +0000
parents 6b8ac424da83
children f3ce3346e25e
comparison
equal deleted inserted replaced
68:6b8ac424da83 69:18db99fda6bd
1 import random
2
1 import pygame 3 import pygame
2 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
3 from pgu import gui 5 from pgu import gui
4 6
5 import data 7 import data
6 import tiles 8 import tiles
7 import constants 9 import constants
8 import buildings 10 import buildings
9 11 import animal
12 from misc import Position
10 13
11 class OpaqueLabel(gui.Label): 14 class OpaqueLabel(gui.Label):
12 def paint(self, s): 15 def paint(self, s):
13 s.fill(self.style.background) 16 s.fill(self.style.background)
14 gui.Label.paint(self, s) 17 gui.Label.paint(self, s)
87 TILE_DIMENSIONS = (20, 20) 90 TILE_DIMENSIONS = (20, 20)
88 TOOLBAR_WIDTH = 140 91 TOOLBAR_WIDTH = 140
89 92
90 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland'] 93 GRASSLAND = tiles.REVERSE_TILE_MAP['grassland']
91 FENCE = tiles.REVERSE_TILE_MAP['fence'] 94 FENCE = tiles.REVERSE_TILE_MAP['fence']
95 BROKEN_FENCE = tiles.REVERSE_TILE_MAP['broken fence']
92 96
93 def __init__(self): 97 def __init__(self):
94 self.tv = tiles.FarmVid() 98 self.tv = tiles.FarmVid()
95 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) 99 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
96 self.tv.png_folder_load_tiles(data.filepath('tiles')) 100 self.tv.png_folder_load_tiles(data.filepath('tiles'))
127 def loop(self): 131 def loop(self):
128 self.tv.loop() 132 self.tv.loop()
129 133
130 def set_selected_tool(self, tool): 134 def set_selected_tool(self, tool):
131 self.selected_tool = tool 135 self.selected_tool = tool
136
137 def in_bounds(self, pos):
138 """Check if a position is within the game boundaries"""
139 if pos.x < 0 or pos.y < 0:
140 return False
141 width, height = self.tv.size
142 if pos.x >= width or pos.y >= height:
143 return False
144 return True
132 145
133 def use_tool(self, e): 146 def use_tool(self, e):
134 if self.selected_tool == constants.TOOL_SELL_CHICKEN: 147 if self.selected_tool == constants.TOOL_SELL_CHICKEN:
135 self.sell_chicken(e.pos) 148 self.sell_chicken(e.pos)
136 elif self.selected_tool == constants.TOOL_SELL_EGG: 149 elif self.selected_tool == constants.TOOL_SELL_EGG:
157 return 170 return
158 self.add_cash(constants.SELL_PRICE_CHICKEN) 171 self.add_cash(constants.SELL_PRICE_CHICKEN)
159 self.remove_chicken(chick) 172 self.remove_chicken(chick)
160 173
161 def buy_fence(self, tile_pos): 174 def buy_fence(self, tile_pos):
162 if self.tv.get(tile_pos) != self.GRASSLAND: 175 this_tile = self.tv.get(tile_pos)
176 if this_tile not in [self.GRASSLAND, self.BROKEN_FENCE]:
163 return 177 return
164 if self.cash < constants.BUY_PRICE_FENCE: 178 if self.cash < constants.BUY_PRICE_FENCE:
165 print "You can't afford a fence." 179 print "You can't afford a fence."
166 return 180 return
167 self.add_cash(-constants.BUY_PRICE_FENCE) 181 self.add_cash(-constants.BUY_PRICE_FENCE)
242 256
243 def add_cash(self, amount): 257 def add_cash(self, amount):
244 self.cash += amount 258 self.cash += amount
245 self.toolbar.update_cash_counter(self.cash) 259 self.toolbar.update_cash_counter(self.cash)
246 260
261 def update_chickens(self):
262 """Update the chickens state at the start of the new day"""
263 # Currently random chickens appear
264 # Very simple, we walk around the tilemap, and, for each farm tile,
265 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
266 # or we run out of board
267 x, y = 0, 0
268 width, height = self.tv.size
269 while len(self.chickens) < 5:
270 if x < width:
271 tile = self.tv.get((x, y))
272 else:
273 y += 1
274 if y >= height:
275 break
276 x = 0
277 continue
278 # See if we place a chicken
279 if 'grassland' == tiles.TILE_MAP[tile]:
280 # Farmland
281 roll = random.randint(1, 20)
282 # We don't place within a tile of the fence, this is to make things
283 # easier
284 for xx in range(x-1, x+2):
285 if xx >= width or xx < 0:
286 continue
287 for yy in range(y-1, y+2):
288 if yy >= height or yy < 0:
289 continue
290 neighbour = self.tv.get((xx, yy))
291 if 'fence' == tiles.TILE_MAP[neighbour]:
292 # Fence
293 roll = 10
294 if roll == 1:
295 # Create a chicken
296 chick = animal.Chicken((x, y))
297 self.add_chicken(chick)
298 x += 1
299
300 def spawn_foxes(self):
301 """The foxes come at night, and this is where they come from."""
302 # Very simple, we walk around the tilemap, and, for each farm tile,
303 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
304 # or we run out of board
305 x, y = 0, 0
306 width, height = self.tv.size
307 new_foxes = random.randint(3, 7)
308 while len(self.foxes) < new_foxes:
309 if x < width:
310 tile = self.tv.get((x, y))
311 else:
312 y += 1
313 if y >= height:
314 break
315 x = 0
316 continue
317 # See if we place a fox
318 if tiles.TILE_MAP[tile] == 'woodland':
319 # Forest
320 roll = random.randint(1, 20)
321 if roll == 1:
322 # Create a fox
323 fox = animal.Fox((x, y))
324 self.add_fox(fox)
325 x += 5
326
247 def fix_buildings(self): 327 def fix_buildings(self):
248 """Go through the level map looking for buildings that haven't 328 """Go through the level map looking for buildings that haven't
249 been added to self.buildings and adding them. 329 been added to self.buildings and adding them.
250 330
251 Where partial buildings exist (i.e. places where the building 331 Where partial buildings exist (i.e. places where the building