comparison gamelib/engine.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 3c4db7bba432
children db78e8b1f8b0
comparison
equal deleted inserted replaced
68:6b8ac424da83 69:18db99fda6bd
2 2
3 from pgu.engine import Game, State, Quit 3 from pgu.engine import Game, State, Quit
4 import pygame 4 import pygame
5 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s 5 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s
6 6
7 from tiles import TILE_MAP
8 import gameboard 7 import gameboard
9 import animal
10 import random
11 8
12 class Engine(Game): 9 class Engine(Game):
13 def __init__(self, main_menu_app): 10 def __init__(self, main_menu_app):
14 self.main_menu_app = main_menu_app 11 self.main_menu_app = main_menu_app
15 self.clock = pygame.time.Clock() 12 self.clock = pygame.time.Clock()
49 """Add some chickens to the farm""" 46 """Add some chickens to the farm"""
50 self.game.gameboard.tv.sun(True) 47 self.game.gameboard.tv.sun(True)
51 48
52 # disable timer 49 # disable timer
53 pygame.time.set_timer(MOVE_FOX_ID, 0) 50 pygame.time.set_timer(MOVE_FOX_ID, 0)
54 # Very simple, we walk around the tilemap, and, for each farm tile,
55 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
56 # or we run out of board
57 self.game.gameboard.clear_foxes() 51 self.game.gameboard.clear_foxes()
58 chickens = len(self.game.gameboard.chickens) 52 self.game.gameboard.update_chickens()
59 x, y = 0, 0
60 width, height = self.game.gameboard.tv.size
61 while chickens < 5:
62 if x < width:
63 tile = self.game.gameboard.tv.get((x, y))
64 else:
65 y += 1
66 if y >= height:
67 break
68 x = 0
69 continue
70 # See if we place a chicken
71 if 'grassland' == TILE_MAP[tile]:
72 # Farmland
73 roll = random.randint(1, 20)
74 # We don't place within a tile of the fence, this is to make things
75 # easier
76 for xx in range(x-1, x+2):
77 if xx >= height or xx < 0:
78 continue
79 for yy in range(y-1, y+2):
80 if yy >= height or yy < 0:
81 continue
82 neighbour = self.game.gameboard.tv.get((xx, yy))
83 if 'fence' == TILE_MAP[neighbour]:
84 # Fence
85 roll = 10
86 if roll == 1:
87 # Create a chicken
88 chickens += 1
89 chick = animal.Chicken((x, y))
90 self.game.gameboard.add_chicken(chick)
91 x += 1
92 53
93 def event(self, e): 54 def event(self, e):
94 if events_equal(e, START_NIGHT): 55 if events_equal(e, START_NIGHT):
95 return NightState(self.game) 56 return NightState(self.game)
96 elif e.type is KEYDOWN and e.key == K_ESCAPE: 57 elif e.type is KEYDOWN and e.key == K_ESCAPE:
118 """Add some foxes to the farm""" 79 """Add some foxes to the farm"""
119 self.game.gameboard.tv.sun(False) 80 self.game.gameboard.tv.sun(False)
120 81
121 # Add a timer to the event queue 82 # Add a timer to the event queue
122 self.cycle_count = 0 83 self.cycle_count = 0
123 pygame.time.set_timer(MOVE_FOX_ID, 300) 84 pygame.time.set_timer(MOVE_FOX_ID, 200)
124 # Very simple, we walk around the tilemap, and, for each farm tile, 85 self.game.gameboard.spawn_foxes()
125 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
126 # or we run out of board
127 foxes = len(self.game.gameboard.foxes)
128 x, y = 0, 0
129 width, height = self.game.gameboard.tv.size
130 while foxes < 5:
131 if x < width:
132 tile = self.game.gameboard.tv.get((x, y))
133 else:
134 y += 1
135 if y >= height:
136 break
137 x = 0
138 continue
139 # See if we place a fox
140 if TILE_MAP[tile] == 'woodland':
141 # Forest
142 roll = random.randint(1, 20)
143 if roll == 1:
144 # Create a fox
145 foxes += 1
146 fox = animal.Fox((x, y))
147 self.game.gameboard.add_fox(fox)
148 x += 5
149 86
150 def event(self, e): 87 def event(self, e):
151 if events_equal(e, START_DAY): 88 if events_equal(e, START_DAY):
152 return DayState(self.game) 89 return DayState(self.game)
153 elif e.type is KEYDOWN and e.key == K_d: 90 elif e.type is KEYDOWN and e.key == K_d:
154 return pygame.event.post(START_DAY) 91 return pygame.event.post(START_DAY)
155 elif e.type is KEYDOWN and e.key == K_ESCAPE: 92 elif e.type is KEYDOWN and e.key == K_ESCAPE:
156 return MainMenuState(self.game) 93 return MainMenuState(self.game)
157 elif e.type is MOVE_FOX_ID: 94 elif e.type is MOVE_FOX_ID:
158 self.cycle_count += 1 95 self.cycle_count += 1
159 if self.cycle_count > 15: 96 if self.cycle_count > 50:
160 return pygame.event.post(START_DAY) 97 return pygame.event.post(START_DAY)
161 return self.game.gameboard.move_foxes() 98 return self.game.gameboard.move_foxes()
162 elif e.type is not QUIT: 99 elif e.type is not QUIT:
163 self.game.gameboard.event(e) 100 self.game.gameboard.event(e)
164 101