Mercurial > rinkhals
comparison gamelib/engine.py @ 29:2e88c680672c
Minimal fox raid logic
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 18:46:46 +0000 |
parents | 6d6ab0c1479d |
children | 2eec29085060 |
comparison
equal
deleted
inserted
replaced
28:ac3a74352b74 | 29:2e88c680672c |
---|---|
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 | 5 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d |
6 | 6 |
7 from tiles import TILE_MAP | |
7 import gameboard | 8 import gameboard |
8 import animal | 9 import animal |
9 import random | 10 import random |
10 | 11 |
11 class Engine(Game): | 12 class Engine(Game): |
40 pygame.display.update(update) | 41 pygame.display.update(update) |
41 | 42 |
42 class DayState(State): | 43 class DayState(State): |
43 def init(self): | 44 def init(self): |
44 """Add some chickens to the farm""" | 45 """Add some chickens to the farm""" |
46 # disable timer | |
47 pygame.time.set_timer(MOVE_FOX_ID, 0) | |
45 # Very simple, we walk around the tilemap, and, for each farm tile, | 48 # Very simple, we walk around the tilemap, and, for each farm tile, |
46 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens | 49 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens |
47 # or we run out of board | 50 # or we run out of board |
48 self.game.gameboard.clear_foxes() | 51 self.game.gameboard.clear_foxes() |
49 chickens = len(self.game.gameboard.chickens) | 52 chickens = len(self.game.gameboard.chickens) |
57 if y >= height: | 60 if y >= height: |
58 break | 61 break |
59 x = 0 | 62 x = 0 |
60 continue | 63 continue |
61 # See if we place a chicken | 64 # See if we place a chicken |
62 if tile == 1: | 65 if 'grassland' == TILE_MAP[tile]: |
63 # Farmland | 66 # Farmland |
64 roll = random.randint(1, 10) | 67 roll = random.randint(1, 20) |
65 # We don't place within a tile of the fence, this is to make things | 68 # We don't place within a tile of the fence, this is to make things |
66 # easier | 69 # easier |
67 for xx in range(x-1, x+2): | 70 for xx in range(x-1, x+2): |
68 if xx >= height or xx < 0: | 71 if xx >= height or xx < 0: |
69 continue | 72 continue |
70 for yy in range(y-1, y+2): | 73 for yy in range(y-1, y+2): |
71 if yy >= height or yy < 0: | 74 if yy >= height or yy < 0: |
72 continue | 75 continue |
73 neighbour = self.game.gameboard.tv.get((xx, yy)) | 76 neighbour = self.game.gameboard.tv.get((xx, yy)) |
74 if neighbour == 2: | 77 if 'fence' == TILE_MAP[neighbour]: |
75 # Fence | 78 # Fence |
76 roll = 10 | 79 roll = 10 |
77 if roll == 1: | 80 if roll == 1: |
78 # Create a chicken | 81 # Create a chicken |
79 chickens += 1 | 82 chickens += 1 |
105 self.game.gameboard.loop() | 108 self.game.gameboard.loop() |
106 | 109 |
107 class NightState(State): | 110 class NightState(State): |
108 def init(self): | 111 def init(self): |
109 """Add some foxes to the farm""" | 112 """Add some foxes to the farm""" |
113 # Add a timer to the event queue | |
114 self.cycle_count = 0 | |
115 pygame.time.set_timer(MOVE_FOX_ID, 300) | |
110 # Very simple, we walk around the tilemap, and, for each farm tile, | 116 # Very simple, we walk around the tilemap, and, for each farm tile, |
111 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens | 117 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens |
112 # or we run out of board | 118 # or we run out of board |
113 foxes = len(self.game.gameboard.foxes) | 119 foxes = len(self.game.gameboard.foxes) |
114 x, y = 0, 0 | 120 x, y = 0, 0 |
121 if y >= height: | 127 if y >= height: |
122 break | 128 break |
123 x = 0 | 129 x = 0 |
124 continue | 130 continue |
125 # See if we place a fox | 131 # See if we place a fox |
126 if tile == 0: | 132 if TILE_MAP[tile] == 'woodland': |
127 # Forest | 133 # Forest |
128 roll = random.randint(1, 10) | 134 roll = random.randint(1, 20) |
129 if roll == 1: | 135 if roll == 1: |
130 # Create a fox | 136 # Create a fox |
131 foxes += 1 | 137 foxes += 1 |
132 fox = animal.Fox((x, y)) | 138 fox = animal.Fox((x, y)) |
133 self.game.gameboard.add_fox(fox) | 139 self.game.gameboard.add_fox(fox) |
138 return DayState(self.game) | 144 return DayState(self.game) |
139 elif e.type is KEYDOWN and e.key == K_d: | 145 elif e.type is KEYDOWN and e.key == K_d: |
140 return pygame.event.post(START_DAY) | 146 return pygame.event.post(START_DAY) |
141 elif e.type is KEYDOWN and e.key == K_ESCAPE: | 147 elif e.type is KEYDOWN and e.key == K_ESCAPE: |
142 return MainMenuState(self.game) | 148 return MainMenuState(self.game) |
149 elif e.type is MOVE_FOX_ID: | |
150 self.cycle_count += 1 | |
151 if self.cycle_count > 15: | |
152 return pygame.event.post(START_DAY) | |
153 return self.game.gameboard.move_foxes() | |
143 elif e.type is not QUIT: | 154 elif e.type is not QUIT: |
144 self.game.gameboard.event(e) | 155 self.game.gameboard.event(e) |
145 | 156 |
146 def loop(self): | 157 def loop(self): |
147 self.game.gameboard.loop() | 158 self.game.gameboard.loop() |
152 | 163 |
153 def update(self, screen): | 164 def update(self, screen): |
154 update = self.game.gameboard.update(screen) | 165 update = self.game.gameboard.update(screen) |
155 pygame.display.update(update) | 166 pygame.display.update(update) |
156 | 167 |
157 | |
158 # pygame events | 168 # pygame events |
159 | 169 |
160 def events_equal(e1, e2): | 170 def events_equal(e1, e2): |
161 """Compare two user events.""" | 171 """Compare two user events.""" |
162 return (e1.type is e2.type and e1.name == e2.name) | 172 return (e1.type is e2.type and e1.name == e2.name) |
163 | 173 |
164 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY") | 174 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY") |
165 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT") | 175 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT") |
166 GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU") | 176 GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU") |
177 MOVE_FOX_ID = USEREVENT + 1 | |
178 MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES") | |
167 QUIT = pygame.event.Event(QUIT) | 179 QUIT = pygame.event.Event(QUIT) |