annotate gamelib/engine.py @ 31:3c4db7bba432

Add 's' as a key for starting the game from the menu.
author Simon Cross <hodgestar@gmail.com>
date Sun, 30 Aug 2009 18:56:09 +0000
parents 2eec29085060
children 18db99fda6bd
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
31
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
5 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s
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)
31
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
29 elif e.type is KEYDOWN:
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
30 if e.key == K_ESCAPE:
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
31 return Quit(self.game)
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
32 elif e.key == K_s:
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
33 self.game.create_game_board()
3c4db7bba432 Add 's' as a key for starting the game from the menu.
Simon Cross <hodgestar@gmail.com>
parents: 30
diff changeset
34 return DayState(self.game)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 elif e.type is not QUIT:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 self.game.main_menu_app.event(e)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 def paint(self, screen):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39 screen.fill((0,0,0))
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
40 self.game.main_menu_app.paint(screen)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
41 pygame.display.flip()
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
42
15
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
43 def update(self, screen):
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
44 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
45 pygame.display.update(update)
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
46
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
47 class DayState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
48 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
49 """Add some chickens to the farm"""
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
50 self.game.gameboard.tv.sun(True)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
51
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
52 # disable timer
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
53 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
54 # 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
55 # 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
56 # or we run out of board
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
57 self.game.gameboard.clear_foxes()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
58 chickens = len(self.game.gameboard.chickens)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
59 x, y = 0, 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
60 width, height = self.game.gameboard.tv.size
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
61 while chickens < 5:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
62 if x < width:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
63 tile = self.game.gameboard.tv.get((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
64 else:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
65 y += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
66 if y >= height:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
67 break
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
68 x = 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
69 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
70 # See if we place a chicken
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
71 if 'grassland' == TILE_MAP[tile]:
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
72 # Farmland
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
73 roll = random.randint(1, 20)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
74 # 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
75 # easier
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
76 for xx in range(x-1, x+2):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
77 if xx >= height or xx < 0:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
78 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
79 for yy in range(y-1, y+2):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
80 if yy >= height or yy < 0:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
81 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
82 neighbour = self.game.gameboard.tv.get((xx, yy))
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
83 if 'fence' == TILE_MAP[neighbour]:
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
84 # Fence
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
85 roll = 10
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
86 if roll == 1:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
87 # Create a chicken
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
88 chickens += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
89 chick = animal.Chicken((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
90 self.game.gameboard.add_chicken(chick)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
91 x += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
92
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
93 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
94 if events_equal(e, START_NIGHT):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
95 return NightState(self.game)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
96 elif e.type is KEYDOWN and e.key == K_ESCAPE:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
97 return MainMenuState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
98 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
99 return pygame.event.post(START_NIGHT)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
100 elif events_equal(e, GO_MAIN_MENU):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
101 return MainMenuState(self.game)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
102 elif e.type is not QUIT:
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
103 self.game.gameboard.event(e)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
104
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
105 def paint(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
106 self.game.gameboard.paint(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
107 pygame.display.flip()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
108
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
109 def update(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
110 update = self.game.gameboard.update(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
111 pygame.display.update(update)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
112
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
113 def loop(self):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
114 self.game.gameboard.loop()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
115
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
116 class NightState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
117 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
118 """Add some foxes to the farm"""
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
119 self.game.gameboard.tv.sun(False)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
120
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
121 # Add a timer to the event queue
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
122 self.cycle_count = 0
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
123 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
124 # 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
125 # 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
126 # or we run out of board
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
127 foxes = len(self.game.gameboard.foxes)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
128 x, y = 0, 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
129 width, height = self.game.gameboard.tv.size
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
130 while foxes < 5:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
131 if x < width:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
132 tile = self.game.gameboard.tv.get((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
133 else:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
134 y += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
135 if y >= height:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
136 break
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
137 x = 0
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
138 continue
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
139 # See if we place a fox
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
140 if TILE_MAP[tile] == 'woodland':
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
141 # Forest
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
142 roll = random.randint(1, 20)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
143 if roll == 1:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
144 # Create a fox
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
145 foxes += 1
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
146 fox = animal.Fox((x, y))
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
147 self.game.gameboard.add_fox(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
148 x += 5
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
149
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
150 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
151 if events_equal(e, START_DAY):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
152 return DayState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
153 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
154 return pygame.event.post(START_DAY)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
155 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
156 return MainMenuState(self.game)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
157 elif e.type is MOVE_FOX_ID:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
158 self.cycle_count += 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
159 if self.cycle_count > 15:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
160 return pygame.event.post(START_DAY)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
161 return self.game.gameboard.move_foxes()
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
162 elif e.type is not QUIT:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
163 self.game.gameboard.event(e)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
164
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
165 def loop(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
166 self.game.gameboard.loop()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
167
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
168 def paint(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
169 self.game.gameboard.paint(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
170 pygame.display.flip()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
171
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
172 def update(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
173 update = self.game.gameboard.update(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
174 pygame.display.update(update)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
175
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
176 # pygame events
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
177
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
178 def events_equal(e1, e2):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
179 """Compare two user events."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
180 return (e1.type is e2.type and e1.name == e2.name)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
181
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
182 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
183 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
184 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
185 MOVE_FOX_ID = USEREVENT + 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
186 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
187 QUIT = pygame.event.Event(QUIT)