annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
1 """Game engine and states."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
2
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
3 from pgu.engine import Game, State, Quit
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
4 import pygame
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
5 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
6
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
7 from tiles import TILE_MAP
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
8 import gameboard
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
9 import animal
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
10 import random
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
11
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12 class Engine(Game):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13 def __init__(self, main_menu_app):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14 self.main_menu_app = main_menu_app
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
15 self.clock = pygame.time.Clock()
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
16
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
17 def tick(self):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
18 """Tic toc."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
19 pygame.time.wait(10)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
20
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
21 def create_game_board(self):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
22 self.gameboard = gameboard.GameBoard()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
23
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
24 class MainMenuState(State):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
25 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
26 if events_equal(e, START_DAY):
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
27 self.game.create_game_board()
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
28 return DayState(self.game)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
29 elif e.type is KEYDOWN and e.key == K_ESCAPE:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
30 return Quit(self.game)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
31 elif e.type is not QUIT:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 self.game.main_menu_app.event(e)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
34 def paint(self, screen):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 screen.fill((0,0,0))
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 self.game.main_menu_app.paint(screen)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 pygame.display.flip()
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38
15
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
39 def update(self, screen):
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
40 update = self.game.main_menu_app.update(screen)
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
41 pygame.display.update(update)
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
42
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
43 class DayState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
44 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
45 """Add some chickens to the farm"""
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
46 # disable timer
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
47 pygame.time.set_timer(MOVE_FOX_ID, 0)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
48 # Very simple, we walk around the tilemap, and, for each farm tile,
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
49 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
50 # or we run out of board
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
51 self.game.gameboard.clear_foxes()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
52 chickens = len(self.game.gameboard.chickens)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
53 x, y = 0, 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
54 width, height = self.game.gameboard.tv.size
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
55 while chickens < 5:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
56 if x < width:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
57 tile = self.game.gameboard.tv.get((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
58 else:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
59 y += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
60 if y >= height:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
61 break
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
62 x = 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
63 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
64 # See if we place a chicken
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
65 if 'grassland' == TILE_MAP[tile]:
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
66 # Farmland
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
67 roll = random.randint(1, 20)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
68 # We don't place within a tile of the fence, this is to make things
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
69 # easier
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
70 for xx in range(x-1, x+2):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
71 if xx >= height or xx < 0:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
72 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
73 for yy in range(y-1, y+2):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
74 if yy >= height or yy < 0:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
75 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
76 neighbour = self.game.gameboard.tv.get((xx, yy))
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
77 if 'fence' == TILE_MAP[neighbour]:
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
78 # Fence
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
79 roll = 10
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
80 if roll == 1:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
81 # Create a chicken
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
82 chickens += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
83 chick = animal.Chicken((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
84 self.game.gameboard.add_chicken(chick)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
85 x += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
86
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
87 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
88 if events_equal(e, START_NIGHT):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
89 return NightState(self.game)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
90 elif e.type is KEYDOWN and e.key == K_ESCAPE:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
91 return MainMenuState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
92 elif e.type is KEYDOWN and e.key == K_n:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
93 return pygame.event.post(START_NIGHT)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
94 elif events_equal(e, GO_MAIN_MENU):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
95 return MainMenuState(self.game)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
96 elif e.type is not QUIT:
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
97 self.game.gameboard.event(e)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
98
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
99 def paint(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
100 self.game.gameboard.paint(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
101 pygame.display.flip()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
102
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
103 def update(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
104 update = self.game.gameboard.update(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
105 pygame.display.update(update)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
106
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
107 def loop(self):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
108 self.game.gameboard.loop()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
109
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
110 class NightState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
111 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
112 """Add some foxes to the farm"""
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
113 # Add a timer to the event queue
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
114 self.cycle_count = 0
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
115 pygame.time.set_timer(MOVE_FOX_ID, 300)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
116 # Very simple, we walk around the tilemap, and, for each farm tile,
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
117 # we randomly add a chicken (1 in 10 chance) until we have 5 chickens
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
118 # or we run out of board
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
119 foxes = len(self.game.gameboard.foxes)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
120 x, y = 0, 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
121 width, height = self.game.gameboard.tv.size
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
122 while foxes < 5:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
123 if x < width:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
124 tile = self.game.gameboard.tv.get((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
125 else:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
126 y += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
127 if y >= height:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
128 break
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
129 x = 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
130 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
131 # See if we place a fox
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
132 if TILE_MAP[tile] == 'woodland':
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
133 # Forest
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
134 roll = random.randint(1, 20)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
135 if roll == 1:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
136 # Create a fox
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
137 foxes += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
138 fox = animal.Fox((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
139 self.game.gameboard.add_fox(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
140 x += 5
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
141
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
142 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
143 if events_equal(e, START_DAY):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
144 return DayState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
145 elif e.type is KEYDOWN and e.key == K_d:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
146 return pygame.event.post(START_DAY)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
147 elif e.type is KEYDOWN and e.key == K_ESCAPE:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
148 return MainMenuState(self.game)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
149 elif e.type is MOVE_FOX_ID:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
150 self.cycle_count += 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
151 if self.cycle_count > 15:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
152 return pygame.event.post(START_DAY)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
153 return self.game.gameboard.move_foxes()
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
154 elif e.type is not QUIT:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
155 self.game.gameboard.event(e)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
156
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
157 def loop(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
158 self.game.gameboard.loop()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
159
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
160 def paint(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
161 self.game.gameboard.paint(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
162 pygame.display.flip()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
163
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
164 def update(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
165 update = self.game.gameboard.update(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
166 pygame.display.update(update)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
167
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
168 # pygame events
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
169
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
170 def events_equal(e1, e2):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
171 """Compare two user events."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
172 return (e1.type is e2.type and e1.name == e2.name)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
173
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
174 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
175 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
176 GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU")
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
177 MOVE_FOX_ID = USEREVENT + 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
178 MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES")
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
179 QUIT = pygame.event.Event(QUIT)