changeset 229:96d440bebdaa

Non-permenant high score table
author Neil Muller <drnlmuller@gmail.com>
date Fri, 04 Sep 2009 23:40:13 +0000
parents f74de4280e20
children fa0e818c3fee
files gamelib/engine.py gamelib/gameover.py
diffstat 2 files changed, 38 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/engine.py	Fri Sep 04 23:32:32 2009 +0000
+++ b/gamelib/engine.py	Fri Sep 04 23:40:13 2009 +0000
@@ -1,5 +1,4 @@
 """Game engine and states."""
-
 from pgu.engine import Game, State, Quit
 import pygame
 from pygame.locals import USEREVENT, QUIT, KEYDOWN, K_ESCAPE, K_n, K_d, K_s, K_i
@@ -17,6 +16,7 @@
         self.clock = pygame.time.Clock()
         self.main_menu = mainmenu.make_main_menu()
         self._open_window = None
+        self.scoreboard = gameover.ScoreTable()
 
     def tick(self):
         """Tic toc."""
@@ -45,7 +45,7 @@
 
     def create_game_over(self):
         """Create and open the Game Over window"""
-        game_over = gameover.create_game_over(self.gameboard)
+        game_over = gameover.create_game_over(self.gameboard, self.scoreboard)
         self.open_window(game_over)
 
 class MainMenuState(State):
--- a/gamelib/gameover.py	Fri Sep 04 23:32:32 2009 +0000
+++ b/gamelib/gameover.py	Fri Sep 04 23:40:13 2009 +0000
@@ -1,15 +1,26 @@
 """The Game Over Screen"""
+import tempfile
 
 from pgu import gui
+from pgu.high import High
 import pygame
 
 import engine
 import constants
 import imagecache
 
-def create_game_over(gameboard):
+def ScoreTable():
+    """Create and initialise a score table"""
+    # We need a true file, so load will work, but, as we never save,
+    # the deletion doesn't bother us.
+    our_scores = High(tempfile.NamedTemporaryFile(), 4)
+    for score in range(700,1000,100):
+        our_scores.submit(score, 'No-one', None)
+    return our_scores
+
+def create_game_over(gameboard, scores):
     """Create a game over screen"""
-    game_over = GameOver(gameboard)
+    game_over = GameOver(gameboard, scores)
     return GameOverContainer(game_over, align=0, valign=0)
 
 class GameOverContainer(gui.Container):
@@ -27,9 +38,8 @@
         gui.Container.paint(self, s)
 
 class GameOver(gui.Table):
-    def __init__(self, gameboard, **params):
+    def __init__(self, gameboard, scoreboard, **params):
         gui.Table.__init__(self, **params)
-        self.tr()
 
         def return_pressed():
             pygame.event.post(engine.GO_MAIN_MENU)
@@ -37,14 +47,27 @@
         def quit_pressed():
             pygame.event.post(engine.QUIT)
 
+
+        score = gameboard.cash + \
+                constants.SELL_PRICE_CHICKEN * len(gameboard.chickens) + \
+                constants.SELL_PRICE_EGG * gameboard.eggs
+
+        self.tr()
         if len(gameboard.chickens) > 0:
             self.survived = True
             self.td(gui.Label("You Survived", color=constants.FG_COLOR),
                     colspan=3)
+            scoreboard.submit(score, 'Player')
         else:
             self.survived = False
             self.td(gui.Label("You Lost", color=constants.FG_COLOR),
                     colspan=3)
+        # show the scoreboard
+
+        for highscore in scoreboard:
+            self.tr()
+            self.td(gui.Label(highscore.name, color=constants.FG_COLOR), colspan=2)
+            self.td(gui.Label('%d' % highscore.score, color=constants.FG_COLOR))
 
         self.tr()
         self.td(gui.Label("Groats : %d" % gameboard.cash,
@@ -54,10 +77,16 @@
         self.td(gui.Label("   Eggs : %d" % gameboard.eggs,
             color=constants.FG_COLOR))
         self.tr()
-        self.td(gui.Label("Final score : %d" % (gameboard.cash +
-            constants.SELL_PRICE_CHICKEN * len(gameboard.chickens) +
-            constants.SELL_PRICE_EGG * gameboard.eggs),
+        self.td(gui.Label("Final score : %d" % score,
             color=constants.FG_COLOR), colspan=3)
+        if scoreboard.check(score) is not None:
+            self.tr()
+            if self.survived:
+                self.td(gui.Label("You made the high scores",
+                    color=constants.FG_COLOR), colspan=3)
+            else:
+                self.td(gui.Label("Pity, you could have made the high scores",
+                    color=constants.FG_COLOR), colspan=3)
 
         self.tr()
         self.td(gui.Spacer(0, 50), colspan=3)