changeset 98:e386ec5d179b

The game can end
author Neil Muller <drnlmuller@gmail.com>
date Wed, 09 May 2012 22:03:07 +0200
parents 069a2d34b8b5
children 531eb3239da2 b27b4dede626 ff7c953502d5
files gamelib/engine.py gamelib/gamegui.py gamelib/mainmenu.py
diffstat 3 files changed, 50 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/engine.py	Wed May 09 21:50:29 2012 +0200
+++ b/gamelib/engine.py	Wed May 09 22:03:07 2012 +0200
@@ -47,6 +47,16 @@
         super(PopWindow, cls).post()
 
 
+class GameOver(UserEvent):
+    # The game is over
+
+    TYPE = "GAME_OVER"
+
+    @classmethod
+    def post(cls, window):
+        super(GameOver, cls).post(window=window)
+
+
 class Engine(object):
 
     def __init__(self, screen):
@@ -87,3 +97,9 @@
                 self._window_stack.append(event.window)
             elif PopWindow.matches(event):
                 self._window_stack.pop()
+            elif GameOver.matches(event):
+                # call game_over method on every item in the stack
+                for win in self._window_stack:
+                    if hasattr(win, 'game_over'):
+                        win.game_over()
+                self._window_stack.append(event.window)
--- a/gamelib/gamegui.py	Wed May 09 21:50:29 2012 +0200
+++ b/gamelib/gamegui.py	Wed May 09 22:03:07 2012 +0200
@@ -13,11 +13,12 @@
 
 from gamelib.data import filepath
 from gamelib.game_base import get_save_filename
-from gamelib.gui_base import Window, TextLabel, font_small, font_medium
+from gamelib.gui_base import (Window, TextLabel, font_small, font_medium,
+        font_large)
 from gamelib.gui import BigButton, ImageDrawable
-from gamelib.engine import PopWindow, AddWindow
+from gamelib.engine import PopWindow, AddWindow, GameOver
 from gamelib.constants import (WIDTH, FAILURE, SUCCESS, NEW_SCIENCE,
-        NEW_SCHEMATIC)
+        NEW_SCHEMATIC, GAME_WIN)
 from gamelib.gamestate import Game
 
 
@@ -263,6 +264,7 @@
         title = TextLabel((200, 20, 400, 50), "Results for turn %d" % turn,
                 font_medium, (255, 255, 255))
         self.add_child(title)
+        self.is_game_over = False
         if not messages:
             results = TextLabel((200, 200, 400, 50),
                     "Nothing of interest happened", font_medium,
@@ -281,12 +283,32 @@
                 elif msg_type == SUCCESS:
                     text = TextLabel((50, y, 750, 25),
                             msg, font_medium, (60, 255, 60))
+                elif msg_type == GAME_WIN:
+                    self._make_win_screen(turn, msg)
+                    self.is_game_over = True
+                    break
                 else:
                     text = TextLabel((50, y, 750, 25),
                             msg, font_medium, (255, 255, 0))
                 y += 50
                 self.add_child(text)
 
+    def _make_win_screen(self, turn, msg):
+        # Clear existing widgets, and turn this into a won screen
+        for child in self.children[:]:
+            self.remove_child(child)
+        exitbut = ExitGameButton()
+        self.add_child(exitbut)
+        title = TextLabel((200, 20, 400, 50), "Results for turn %d" % turn,
+                font_medium, (255, 255, 255))
+        self.add_child(title)
+        won = TextLabel((200, 200, 400, 50), "You've succeeded in your quest",
+                font_large, (255, 255, 255))
+        self.add_child(won)
+        won = TextLabel((200, 250, 400, 50), msg, font_large,
+                (255, 255, 255))
+        self.add_child(won)
+
 
 class ActivityWindow(Window):
 
@@ -526,6 +548,10 @@
         self.game.cur_missions = self.activity.get_mission_list()
         messages = self.game.end_turn()
         results = ResultsWindow(self.screen, messages, self.game.turn)
+        if results.is_game_over:
+            PopWindow.post()
+            GameOver.post(results)
+            return
         self.game.start_turn()
         self.update_labels()
         self.update_widgets()
--- a/gamelib/mainmenu.py	Wed May 09 21:50:29 2012 +0200
+++ b/gamelib/mainmenu.py	Wed May 09 22:03:07 2012 +0200
@@ -109,5 +109,10 @@
             self.resume = ResumeGameButton(self)
             self.add_child(self.resume)
 
+    def game_over(self):
+        if self.resume:
+            self.remove_child(self.resume)
+            self.resume = None
+
     def resume_game(self):
         AddWindow.post(self.game_window)