source: gamelib/engine.py @ 182:d63951985313

Last change on this file since 182:d63951985313 was 182:d63951985313, checked in by Simon Cross <hodgestar@…>, 11 years ago

Make 'i' take you from the main menu to the instructions.

File size: 7.1 KB
Line 
1"""Game engine and states."""
2
3from pgu.engine import Game, State, Quit
4import pygame
5from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s, K_i
6
7import gameboard
8import gameover
9import sound
10import constants
11import mainmenu
12import helpscreen
13
14class Engine(Game):
15    def __init__(self, main_app):
16        self.main_app = main_app
17        self.clock = pygame.time.Clock()
18        self.main_menu = mainmenu.make_main_menu()
19        self._open_window = None
20
21    def tick(self):
22        """Tic toc."""
23        pygame.time.wait(10)
24
25    def open_window(self, window):
26        """Open a widget as the main window."""
27        if self._open_window is not None:
28            self.main_app.close(self._open_window)
29        self.main_app.open(window)
30        self._open_window = window
31
32    def create_game_board(self):
33        """Create and open a gameboard window."""
34        self.gameboard = gameboard.GameBoard(self.main_app)
35        self.open_window(self.gameboard.get_top_widget())
36
37    def set_main_menu(self):
38        """Open the main menu"""
39        self.open_window(self.main_menu)
40
41    def set_help_screen(self):
42        """Open the main menu"""
43        help_screen = helpscreen.make_help_screen()
44        self.open_window(help_screen)
45
46    def create_game_over(self):
47        """Create and open the Game Over window"""
48        game_over = gameover.create_game_over(self.gameboard)
49        self.open_window(game_over)
50
51class MainMenuState(State):
52    def init(self):
53        sound.stop_background_music()
54        self.game.set_main_menu()
55
56    def event(self, e):
57        if events_equal(e, START_DAY):
58            self.game.create_game_board()
59            return DayState(self.game)
60        elif events_equal(e, GO_HELP_SCREEN):
61            return HelpScreenState(self.game)
62        elif e.type is KEYDOWN:
63            if e.key == K_ESCAPE:
64                return Quit(self.game)
65            elif e.key == K_s:
66                self.game.create_game_board()
67                return DayState(self.game)
68            elif e.key == K_i:
69                return HelpScreenState(self.game)
70        elif e.type is not QUIT:
71            self.game.main_app.event(e)
72
73    def paint(self, screen):
74        screen.fill((0,0,0))
75        self.game.main_app.paint(screen)
76        pygame.display.flip()
77
78    def update(self, screen):
79        update = self.game.main_app.update(screen)
80        pygame.display.update(update)
81
82class HelpScreenState(State):
83    def init(self):
84        sound.stop_background_music()
85        self.game.set_help_screen()
86
87    def event(self, e):
88        if e.type is KEYDOWN and e.key == K_ESCAPE:
89            return MainMenuState(self.game)
90        elif events_equal(e, GO_MAIN_MENU):
91            return MainMenuState(self.game)
92        elif e.type is not QUIT:
93            self.game.main_app.event(e)
94
95    def paint(self, screen):
96        screen.fill((0,0,0))
97        self.game.main_app.paint(screen)
98        pygame.display.flip()
99
100    def update(self, screen):
101        update = self.game.main_app.update(screen)
102        pygame.display.update(update)
103
104class DayState(State):
105    def init(self):
106        """Add some chickens to the farm"""
107        sound.stop_background_music()
108        self.game.gameboard.tv.sun(True)
109
110        sound.play_sound("daybreak.ogg")
111        # disable timer
112        pygame.time.set_timer(MOVE_FOX_ID, 0)
113        self.game.gameboard.advance_day()
114        self.game.gameboard.clear_foxes()
115        sound.background_music("daytime.ogg")
116        self.game.gameboard.hatch_eggs()
117
118    def event(self, e):
119        if events_equal(e, START_NIGHT):
120            return NightState(self.game)
121        elif e.type is KEYDOWN and e.key == K_ESCAPE:
122            self.game.gameboard.reset_cursor()
123            return GameOver(self.game)
124        elif e.type is KEYDOWN and e.key == K_n:
125            return pygame.event.post(START_NIGHT)
126        elif events_equal(e, GO_MAIN_MENU):
127            return MainMenuState(self.game)
128        elif e.type is not QUIT:
129            self.game.main_app.event(e)
130
131    def paint(self, screen):
132        self.game.main_app.paint(screen)
133        pygame.display.flip()
134
135    def update(self, screen):
136        self.game.gameboard.update()
137        update = self.game.main_app.update(screen)
138        pygame.display.update(update)
139
140    def loop(self):
141        self.game.gameboard.loop()
142
143class NightState(State):
144    def init(self):
145        """Add some foxes to the farm"""
146        sound.stop_background_music()
147        self.game.gameboard.tv.sun(False)
148
149        sound.play_sound("nightfall.ogg")
150        # Add a timer to the event queue
151        self.cycle_count = 0
152        pygame.time.set_timer(MOVE_FOX_ID, 200)
153        self.game.gameboard.spawn_foxes()
154        sound.background_music("nighttime.ogg")
155
156        self.game.gameboard.lay_eggs()
157
158    def event(self, e):
159        if events_equal(e, START_DAY):
160            if self.game.gameboard.is_game_over():
161                return GameOver(self.game)
162            return DayState(self.game)
163        elif e.type is KEYDOWN and e.key == K_d:
164            return pygame.event.post(START_DAY)
165        elif e.type is KEYDOWN and e.key == K_ESCAPE:
166            self.game.gameboard.reset_cursor()
167            return GameOver(self.game)
168        elif e.type is MOVE_FOX_ID:
169            self.cycle_count += 1
170            if self.cycle_count > constants.NIGHT_LENGTH:
171                return pygame.event.post(START_DAY)
172            if self.game.gameboard.move_foxes():
173                # All foxes are gone/safe, so dawn happens
174                return pygame.event.post(START_DAY)
175        elif e.type is not QUIT:
176            self.game.main_app.event(e)
177
178    def loop(self):
179        self.game.gameboard.loop()
180
181    def paint(self, screen):
182        self.game.main_app.paint(screen)
183        pygame.display.flip()
184
185    def update(self, screen):
186        self.game.gameboard.update()
187        update = self.game.main_app.update(screen)
188        pygame.display.update(update)
189
190class GameOver(State):
191    def init(self):
192        """Setup everything"""
193        sound.stop_background_music()
194        self.game.create_game_over()
195        pygame.time.set_timer(MOVE_FOX_ID, 0)
196
197    def event(self, e):
198        if e.type is KEYDOWN:
199            if e.key == K_ESCAPE:
200                return MainMenuState(self.game)
201        elif events_equal(e, GO_MAIN_MENU):
202            return MainMenuState(self.game)
203        elif e.type is not QUIT:
204            self.game.main_app.event(e)
205
206    def paint(self, screen):
207        screen.fill((0,0,0))
208        self.game.main_app.paint(screen)
209        pygame.display.flip()
210
211    def update(self, screen):
212        update = self.game.main_app.update(screen)
213        pygame.display.update(update)
214
215# pygame events
216
217def events_equal(e1, e2):
218    """Compare two user events."""
219    return (e1.type is e2.type and e1.name == e2.name)
220
221START_DAY = pygame.event.Event(USEREVENT, name="START_DAY")
222START_NIGHT = pygame.event.Event(USEREVENT, name="START_NIGHT")
223GO_MAIN_MENU = pygame.event.Event(USEREVENT, name="GO_MAIN_MENU")
224GO_HELP_SCREEN = pygame.event.Event(USEREVENT, name="GO_HELP_SCREEN")
225MOVE_FOX_ID = USEREVENT + 1
226MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES")
227QUIT = pygame.event.Event(QUIT)
Note: See TracBrowser for help on using the repository browser.