Mercurial > rinkhals
annotate gamelib/engine.py @ 363:e23637457aa4
Prevent error stopping music when sound not initialized
author | David Fraser <davidf@sjsoft.com> |
---|---|
date | Tue, 08 Sep 2009 06:35:14 +0000 |
parents | a36ab7dd0eb9 |
children | 25f7d4824250 |
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 from pgu.engine import Game, State, Quit |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
3 import pygame |
182
d63951985313
Make 'i' take you from the main menu to the instructions.
Simon Cross <hodgestar@gmail.com>
parents:
180
diff
changeset
|
4 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s, K_i |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
5 |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
6 import gameboard |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
7 import gameover |
94 | 8 import sound |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
9 import constants |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
10 import mainmenu |
179 | 11 import helpscreen |
336 | 12 from misc import check_exit |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
13 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
14 class Engine(Game): |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
15 def __init__(self, main_app): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
16 self.main_app = main_app |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
17 self.clock = pygame.time.Clock() |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
18 self.main_menu = mainmenu.make_main_menu() |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
19 self._open_window = None |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
223
diff
changeset
|
20 self.scoreboard = gameover.ScoreTable() |
254
f399e4926c36
Fix events getting sent to the gameboard
Neil Muller <drnlmuller@gmail.com>
parents:
245
diff
changeset
|
21 self.gameboard = None |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
22 |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
23 def tick(self): |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
24 """Tic toc.""" |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
25 pygame.time.wait(10) |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
26 |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
27 def open_window(self, window): |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
28 """Open a widget as the main window.""" |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
29 if self._open_window is not None: |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
30 self.main_app.close(self._open_window) |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
31 self.main_app.open(window) |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
32 self._open_window = window |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
33 |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
34 def create_game_board(self): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
35 """Create and open a gameboard window.""" |
346
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
36 self.mode = self.main_menu.get_mode() |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
37 if not self.mode: |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
38 self.mode = constants.DEFAULT_MODE |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
39 self.gameboard = gameboard.GameBoard(self.main_app, |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
40 constants.TURN_LIMITS[self.mode]) |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
41 self.open_window(self.gameboard.get_top_widget()) |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
42 |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
43 def set_main_menu(self): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
44 """Open the main menu""" |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
45 self.open_window(self.main_menu) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
46 |
179 | 47 def set_help_screen(self): |
48 """Open the main menu""" | |
49 help_screen = helpscreen.make_help_screen() | |
50 self.open_window(help_screen) | |
51 | |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
52 def create_game_over(self): |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
53 """Create and open the Game Over window""" |
346
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
54 game_over = gameover.create_game_over(self.gameboard, |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
336
diff
changeset
|
55 self.scoreboard[self.mode], self.mode) |
254
f399e4926c36
Fix events getting sent to the gameboard
Neil Muller <drnlmuller@gmail.com>
parents:
245
diff
changeset
|
56 self.gameboard = None |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
57 self.open_window(game_over) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
58 |
254
f399e4926c36
Fix events getting sent to the gameboard
Neil Muller <drnlmuller@gmail.com>
parents:
245
diff
changeset
|
59 def event(self, e): |
262
d508248041ff
avoid passing events in twice
Neil Muller <drnlmuller@gmail.com>
parents:
254
diff
changeset
|
60 if not Game.event(self, e): |
d508248041ff
avoid passing events in twice
Neil Muller <drnlmuller@gmail.com>
parents:
254
diff
changeset
|
61 if self.gameboard: |
d508248041ff
avoid passing events in twice
Neil Muller <drnlmuller@gmail.com>
parents:
254
diff
changeset
|
62 return self.gameboard.event(e) |
d508248041ff
avoid passing events in twice
Neil Muller <drnlmuller@gmail.com>
parents:
254
diff
changeset
|
63 return False |
d508248041ff
avoid passing events in twice
Neil Muller <drnlmuller@gmail.com>
parents:
254
diff
changeset
|
64 return True |
254
f399e4926c36
Fix events getting sent to the gameboard
Neil Muller <drnlmuller@gmail.com>
parents:
245
diff
changeset
|
65 |
f399e4926c36
Fix events getting sent to the gameboard
Neil Muller <drnlmuller@gmail.com>
parents:
245
diff
changeset
|
66 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
67 class MainMenuState(State): |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
68 def init(self): |
156
210fc1ea0516
Stop background music whenever state changes.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
69 sound.stop_background_music() |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
70 self.game.set_main_menu() |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
71 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
72 def event(self, e): |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
73 if events_equal(e, START_DAY): |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
74 self.game.create_game_board() |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
75 return DayState(self.game) |
179 | 76 elif events_equal(e, GO_HELP_SCREEN): |
77 return HelpScreenState(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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 return DayState(self.game) |
182
d63951985313
Make 'i' take you from the main menu to the instructions.
Simon Cross <hodgestar@gmail.com>
parents:
180
diff
changeset
|
84 elif e.key == K_i: |
d63951985313
Make 'i' take you from the main menu to the instructions.
Simon Cross <hodgestar@gmail.com>
parents:
180
diff
changeset
|
85 return HelpScreenState(self.game) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
86 elif e.type is not QUIT: |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
87 self.game.main_app.event(e) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
88 |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
89 def paint(self, screen): |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
90 screen.fill((0,0,0)) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
91 self.game.main_app.paint(screen) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
92 pygame.display.flip() |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
93 |
15
b69d954f2f1d
Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents:
14
diff
changeset
|
94 def update(self, screen): |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
95 update = self.game.main_app.update(screen) |
15
b69d954f2f1d
Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents:
14
diff
changeset
|
96 pygame.display.update(update) |
b69d954f2f1d
Add update call to main menu event
Neil Muller <drnlmuller@gmail.com>
parents:
14
diff
changeset
|
97 |
179 | 98 class HelpScreenState(State): |
99 def init(self): | |
100 sound.stop_background_music() | |
101 self.game.set_help_screen() | |
102 | |
103 def event(self, e): | |
104 if e.type is KEYDOWN and e.key == K_ESCAPE: | |
180
c0cad267222b
Fix typo in reference to MainMenuState.
Simon Cross <hodgestar@gmail.com>
parents:
179
diff
changeset
|
105 return MainMenuState(self.game) |
179 | 106 elif events_equal(e, GO_MAIN_MENU): |
107 return MainMenuState(self.game) | |
108 elif e.type is not QUIT: | |
109 self.game.main_app.event(e) | |
110 | |
111 def paint(self, screen): | |
112 screen.fill((0,0,0)) | |
113 self.game.main_app.paint(screen) | |
114 pygame.display.flip() | |
115 | |
116 def update(self, screen): | |
117 update = self.game.main_app.update(screen) | |
118 pygame.display.update(update) | |
119 | |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
120 class DayState(State): |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
121 def init(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
122 """Add some chickens to the farm""" |
99
f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents:
98
diff
changeset
|
123 sound.stop_background_music() |
223 | 124 self.game.gameboard.start_day() |
30 | 125 |
98
725b292ca07b
Added sounds killing foxes and chickens, and nightfall
David Fraser <davidf@sjsoft.com>
parents:
94
diff
changeset
|
126 sound.play_sound("daybreak.ogg") |
29 | 127 # disable timer |
128 pygame.time.set_timer(MOVE_FOX_ID, 0) | |
237 | 129 pygame.time.set_timer(ANIM_ID, SLOW_ANIM_SPEED) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
130 self.game.gameboard.advance_day() |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
131 self.game.gameboard.clear_foxes() |
99
f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents:
98
diff
changeset
|
132 sound.background_music("daytime.ogg") |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
133 self.game.gameboard.hatch_eggs() |
336 | 134 self.dialog = None |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
135 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
136 def event(self, e): |
336 | 137 if self.dialog and self.dialog.running: |
138 if self.dialog.event(e): | |
139 return | |
140 elif self.dialog: | |
141 if self.dialog.do_quit: | |
142 self.dialog = None | |
143 self.game.gameboard.reset_states() | |
144 return GameOver(self.game) | |
145 self.dialog=None | |
146 return | |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
147 if events_equal(e, START_NIGHT): |
245
634491bf37e8
Change toolbar to gui.Toolbar -- gives nicer highlighting.
Simon Cross <hodgestar@gmail.com>
parents:
237
diff
changeset
|
148 self.game.gameboard.reset_states() |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
149 return NightState(self.game) |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
150 elif e.type is KEYDOWN and e.key == K_ESCAPE: |
336 | 151 self.dialog = check_exit() |
202 | 152 elif e.type is ANIM_ID: |
153 self.game.gameboard.run_animations() | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
154 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
|
155 return pygame.event.post(START_NIGHT) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
156 elif events_equal(e, GO_MAIN_MENU): |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
157 return MainMenuState(self.game) |
14 | 158 elif e.type is not QUIT: |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
159 self.game.main_app.event(e) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
160 |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
161 def paint(self, screen): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
162 self.game.main_app.paint(screen) |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
163 pygame.display.flip() |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
164 |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
165 def update(self, screen): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
166 self.game.gameboard.update() |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
167 update = self.game.main_app.update(screen) |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
168 pygame.display.update(update) |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
169 |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
170 def loop(self): |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
171 self.game.gameboard.loop() |
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
172 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
173 class NightState(State): |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
174 def init(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
175 """Add some foxes to the farm""" |
99
f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents:
98
diff
changeset
|
176 sound.stop_background_music() |
223 | 177 self.game.gameboard.start_night() |
30 | 178 |
98
725b292ca07b
Added sounds killing foxes and chickens, and nightfall
David Fraser <davidf@sjsoft.com>
parents:
94
diff
changeset
|
179 sound.play_sound("nightfall.ogg") |
29 | 180 # Add a timer to the event queue |
181 self.cycle_count = 0 | |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
182 self.cycle_time = SLOW_ANIM_SPEED |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
183 pygame.time.set_timer(MOVE_FOX_ID, 4*self.cycle_time) |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
184 pygame.time.set_timer(ANIM_ID, self.cycle_time) |
69
18db99fda6bd
Move spawing code from engine to gameboard - seems more natural.
Neil Muller <drnlmuller@gmail.com>
parents:
31
diff
changeset
|
185 self.game.gameboard.spawn_foxes() |
99
f5d56688943b
Added background music support, and daytime and night time music (really animal sounds, not music)
David Fraser <davidf@sjsoft.com>
parents:
98
diff
changeset
|
186 sound.background_music("nighttime.ogg") |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
187 |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
188 self.game.gameboard.lay_eggs() |
349
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
189 self.dialog = None |
116
d539ef5a3333
Add basic chicken->egg cycle
Neil Muller <drnlmuller@gmail.com>
parents:
99
diff
changeset
|
190 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
191 def event(self, e): |
349
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
192 if self.dialog and self.dialog.running: |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
193 if self.dialog.event(e): |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
194 return |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
195 elif self.dialog: |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
196 if self.dialog.do_quit: |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
197 self.dialog = None |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
198 self.game.gameboard.reset_states() |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
199 return GameOver(self.game) |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
200 self.dialog=None |
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
201 return |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
202 if events_equal(e, START_DAY): |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
203 if self.game.gameboard.is_game_over(): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
204 return GameOver(self.game) |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
205 return DayState(self.game) |
320
9bf0e701a36e
Switch between 'Finished Day' and 'Fast Forward' modes
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
206 elif (e.type is KEYDOWN and e.key == K_d) or \ |
9bf0e701a36e
Switch between 'Finished Day' and 'Fast Forward' modes
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
207 events_equal(e, FAST_FORWARD): |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
208 if self.cycle_time > FAST_ANIM_SPEED: |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
209 self.cycle_time = FAST_ANIM_SPEED |
222
322e6d3a46e4
Turn 'd' into a fast forward / normal speed toggle at night
Neil Muller <drnlmuller@gmail.com>
parents:
213
diff
changeset
|
210 else: |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
211 self.cycle_time = SLOW_ANIM_SPEED |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
212 pygame.time.set_timer(ANIM_ID, self.cycle_time) |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
213 pygame.time.set_timer(MOVE_FOX_ID, 4*self.cycle_time) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
214 elif e.type is KEYDOWN and e.key == K_ESCAPE: |
349
a36ab7dd0eb9
Fix 'Are You Sure?' dialog to work at night
Neil Muller <drnlmuller@gmail.com>
parents:
346
diff
changeset
|
215 self.dialog = check_exit() |
202 | 216 elif e.type is ANIM_ID: |
217 self.game.gameboard.run_animations() | |
29 | 218 elif e.type is MOVE_FOX_ID: |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
219 # Timer aren't nessecairly ordered, so we make sure |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
220 # we don't get a ANIM event until at least cycle after this |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
221 pygame.time.set_timer(ANIM_ID, self.cycle_time) |
237 | 222 # Ensure any outstanding animitions get cleaned up |
223 self.game.gameboard.run_animations() | |
29 | 224 self.cycle_count += 1 |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
225 if self.cycle_count > constants.NIGHT_LENGTH: |
29 | 226 return pygame.event.post(START_DAY) |
122
d2b19131d537
Don't continue the night if we're not doing anything anymore
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
227 if self.game.gameboard.move_foxes(): |
d2b19131d537
Don't continue the night if we're not doing anything anymore
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
228 # All foxes are gone/safe, so dawn happens |
d2b19131d537
Don't continue the night if we're not doing anything anymore
Neil Muller <drnlmuller@gmail.com>
parents:
118
diff
changeset
|
229 return pygame.event.post(START_DAY) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
230 elif e.type is not QUIT: |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
231 self.game.main_app.event(e) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
232 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
233 def loop(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
234 self.game.gameboard.loop() |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
235 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
236 def paint(self, screen): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
237 self.game.main_app.paint(screen) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
238 pygame.display.flip() |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
239 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
240 def update(self, screen): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
241 self.game.gameboard.update() |
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
242 update = self.game.main_app.update(screen) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
243 pygame.display.update(update) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
15
diff
changeset
|
244 |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
245 class GameOver(State): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
246 def init(self): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
247 """Setup everything""" |
156
210fc1ea0516
Stop background music whenever state changes.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
248 sound.stop_background_music() |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
142
diff
changeset
|
249 self.game.create_game_over() |
142
1597081e5a84
Disable timer when game is over
Neil Muller <drnlmuller@gmail.com>
parents:
141
diff
changeset
|
250 pygame.time.set_timer(MOVE_FOX_ID, 0) |
202 | 251 pygame.time.set_timer(ANIM_ID, 0) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
252 |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
253 def event(self, e): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
254 if e.type is KEYDOWN: |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
255 if e.key == K_ESCAPE: |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
256 return MainMenuState(self.game) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
257 elif events_equal(e, GO_MAIN_MENU): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
258 return MainMenuState(self.game) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
259 elif e.type is not QUIT: |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
260 self.game.main_app.event(e) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
261 |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
262 def paint(self, screen): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
263 screen.fill((0,0,0)) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
264 self.game.main_app.paint(screen) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
265 pygame.display.flip() |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
266 |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
267 def update(self, screen): |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
268 update = self.game.main_app.update(screen) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
269 pygame.display.update(update) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
122
diff
changeset
|
270 |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
271 # pygame events |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
272 |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
273 def events_equal(e1, e2): |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
274 """Compare two user events.""" |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
275 return (e1.type is e2.type and e1.name == e2.name) |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
276 |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
277 START_DAY = pygame.event.Event(USEREVENT, name="START_DAY") |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
278 START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT") |
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
diff
changeset
|
279 GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU") |
179 | 280 GO_HELP_SCREEN = pygame.event.Event(USEREVENT, name="GO_HELP_SCREEN") |
320
9bf0e701a36e
Switch between 'Finished Day' and 'Fast Forward' modes
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
281 FAST_FORWARD = pygame.event.Event(USEREVENT, name="FAST_FORWARD") |
29 | 282 MOVE_FOX_ID = USEREVENT + 1 |
202 | 283 ANIM_ID = USEREVENT + 6 |
29 | 284 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
|
285 QUIT = pygame.event.Event(QUIT) |
233
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
286 |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
287 # Due to the way pgu's loop timing works, these will only get proceesed |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
288 # at intervals of 10ms, so there's no point in them not being multiples of 10 |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
289 FAST_ANIM_SPEED=20 |
d3d5352f5853
Twek speed loop. Document animation assumptions
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
290 SLOW_ANIM_SPEED=50 |