diff gamelib/engine.py @ 139:1d73de63bd71

Add basic game over screen
author Neil Muller <drnlmuller@gmail.com>
date Wed, 02 Sep 2009 22:48:39 +0000
parents d2b19131d537
children dc4bb10cc54c
line wrap: on
line diff
--- a/gamelib/engine.py	Wed Sep 02 22:45:58 2009 +0000
+++ b/gamelib/engine.py	Wed Sep 02 22:48:39 2009 +0000
@@ -5,11 +5,14 @@
 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s
 
 import gameboard
+import gameover
 import sound
+import constants
+import mainmenu
 
 class Engine(Game):
-    def __init__(self, main_menu_app):
-        self.main_menu_app = main_menu_app
+    def __init__(self, main_app):
+        self.main_app = main_app
         self.clock = pygame.time.Clock()
 
     def tick(self):
@@ -19,7 +22,18 @@
     def create_game_board(self):
         self.gameboard = gameboard.GameBoard()
 
+    def set_main_menu(self):
+        """Create the main menu"""
+        mainmenu.add_main_menu(self.main_app)
+
+    def generate_score(self):
+        """Create the Game Over state"""
+        gameover.add_game_over(self.main_app, self.gameboard)
+
 class MainMenuState(State):
+    def init(self):
+        self.game.set_main_menu()
+
     def event(self, e):
         if events_equal(e, START_DAY):
             self.game.create_game_board()
@@ -31,15 +45,15 @@
                 self.game.create_game_board()
                 return DayState(self.game)
         elif e.type is not QUIT:
-            self.game.main_menu_app.event(e)
+            self.game.main_app.event(e)
 
     def paint(self, screen):
         screen.fill((0,0,0))
-        self.game.main_menu_app.paint(screen)
+        self.game.main_app.paint(screen)
         pygame.display.flip()
 
     def update(self, screen):
-        update = self.game.main_menu_app.update(screen)
+        update = self.game.main_app.update(screen)
         pygame.display.update(update)
 
 class DayState(State):
@@ -51,6 +65,7 @@
         sound.play_sound("daybreak.ogg")
         # disable timer
         pygame.time.set_timer(MOVE_FOX_ID, 0)
+        self.game.gameboard.advance_day()
         self.game.gameboard.clear_foxes()
         sound.background_music("daytime.ogg")
         self.game.gameboard.hatch_eggs()
@@ -95,6 +110,8 @@
 
     def event(self, e):
         if events_equal(e, START_DAY):
+            if self.game.gameboard.is_game_over():
+                return GameOver(self.game)
             return DayState(self.game)
         elif e.type is KEYDOWN and e.key == K_d:
             return pygame.event.post(START_DAY)
@@ -102,7 +119,7 @@
             return MainMenuState(self.game)
         elif e.type is MOVE_FOX_ID:
             self.cycle_count += 1
-            if self.cycle_count > NIGHT_LENGTH:
+            if self.cycle_count > constants.NIGHT_LENGTH:
                 return pygame.event.post(START_DAY)
             if self.game.gameboard.move_foxes():
                 # All foxes are gone/safe, so dawn happens
@@ -121,6 +138,30 @@
         update = self.game.gameboard.update(screen)
         pygame.display.update(update)
 
+class GameOver(State):
+    def init(self):
+        """Setup everything"""
+        self.game.gameboard.tv.sun(True)
+        self.game.generate_score()
+
+    def event(self, e):
+        if e.type is KEYDOWN:
+            if e.key == K_ESCAPE:
+                return MainMenuState(self.game)
+        elif events_equal(e, GO_MAIN_MENU):
+            return MainMenuState(self.game)
+        elif e.type is not QUIT:
+            self.game.main_app.event(e)
+
+    def paint(self, screen):
+        screen.fill((0,0,0))
+        self.game.main_app.paint(screen)
+        pygame.display.flip()
+
+    def update(self, screen):
+        update = self.game.main_app.update(screen)
+        pygame.display.update(update)
+
 # pygame events
 
 def events_equal(e1, e2):
@@ -133,4 +174,3 @@
 MOVE_FOX_ID = USEREVENT + 1
 MOVE_FOXES = pygame.event.Event(MOVE_FOX_ID, name="MOVE_FOXES")
 QUIT = pygame.event.Event(QUIT)
-NIGHT_LENGTH = 150