annotate gamelib/engine.py @ 80:ad9d1bc7ef0c

Kill foxes that don't reach safety
author Neil Muller <drnlmuller@gmail.com>
date Tue, 01 Sep 2009 11:21:43 +0000
parents 18db99fda6bd
children db78e8b1f8b0
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
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
7 import gameboard
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
8
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
9 class Engine(Game):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
10 def __init__(self, main_menu_app):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
11 self.main_menu_app = main_menu_app
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
12 self.clock = pygame.time.Clock()
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
13
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
14 def tick(self):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
15 """Tic toc."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
16 pygame.time.wait(10)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
17
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
18 def create_game_board(self):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
19 self.gameboard = gameboard.GameBoard()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
20
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
21 class MainMenuState(State):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
22 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
23 if events_equal(e, START_DAY):
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
24 self.game.create_game_board()
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
25 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
26 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
27 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
28 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
29 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
30 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
31 return DayState(self.game)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
32 elif e.type is not QUIT:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
33 self.game.main_menu_app.event(e)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
34
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
35 def paint(self, screen):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
36 screen.fill((0,0,0))
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
37 self.game.main_menu_app.paint(screen)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
38 pygame.display.flip()
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
39
15
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
40 def update(self, screen):
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
41 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
42 pygame.display.update(update)
b69d954f2f1d Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents: 14
diff changeset
43
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
44 class DayState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
45 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
46 """Add some chickens to the farm"""
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
47 self.game.gameboard.tv.sun(True)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
48
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
49 # disable timer
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
50 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
51 self.game.gameboard.clear_foxes()
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 31
diff changeset
52 self.game.gameboard.update_chickens()
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
53
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
54 def event(self, e):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
55 if events_equal(e, START_NIGHT):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
56 return NightState(self.game)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
57 elif e.type is KEYDOWN and e.key == K_ESCAPE:
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
58 return MainMenuState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
59 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
60 return pygame.event.post(START_NIGHT)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
61 elif events_equal(e, GO_MAIN_MENU):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
62 return MainMenuState(self.game)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
63 elif e.type is not QUIT:
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
64 self.game.gameboard.event(e)
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
65
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
66 def paint(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
67 self.game.gameboard.paint(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
68 pygame.display.flip()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
69
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
70 def update(self, screen):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
71 update = self.game.gameboard.update(screen)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
72 pygame.display.update(update)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
73
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
74 def loop(self):
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
75 self.game.gameboard.loop()
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
76
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
77 class NightState(State):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
78 def init(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
79 """Add some foxes to the farm"""
30
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
80 self.game.gameboard.tv.sun(False)
2eec29085060 Color night and day.
Simon Cross <hodgestar@gmail.com>
parents: 29
diff changeset
81
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
82 # Add a timer to the event queue
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
83 self.cycle_count = 0
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 31
diff changeset
84 pygame.time.set_timer(MOVE_FOX_ID, 200)
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 31
diff changeset
85 self.game.gameboard.spawn_foxes()
25
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_DAY):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
89 return DayState(self.game)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
90 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
91 return pygame.event.post(START_DAY)
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_ESCAPE:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
93 return MainMenuState(self.game)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
94 elif e.type is MOVE_FOX_ID:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
95 self.cycle_count += 1
69
18db99fda6bd Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents: 31
diff changeset
96 if self.cycle_count > 50:
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
97 return pygame.event.post(START_DAY)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
98 return self.game.gameboard.move_foxes()
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
99 elif e.type is not QUIT:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
100 self.game.gameboard.event(e)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
101
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
102 def loop(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
103 self.game.gameboard.loop()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
104
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
105 def paint(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
106 self.game.gameboard.paint(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
107 pygame.display.flip()
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
108
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
109 def update(self, screen):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
110 update = self.game.gameboard.update(screen)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
111 pygame.display.update(update)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 15
diff changeset
112
11
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
113 # pygame events
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
114
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
115 def events_equal(e1, e2):
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
116 """Compare two user events."""
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
117 return (e1.type is e2.type and e1.name == e2.name)
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
118
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
119 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
120 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
5d58a5b13731 Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff changeset
121 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
122 MOVE_FOX_ID = USEREVENT + 1
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
123 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
124 QUIT = pygame.event.Event(QUIT)