comparison gamelib/gameover.py @ 555:296c73dcd286

Add high score dialog to main menu.
author Simon Cross <hodgestar@gmail.com>
date Sat, 28 Nov 2009 17:53:42 +0000
parents bb75979b58e6
children 8cd13b82585e
comparison
equal deleted inserted replaced
554:46fa3cdfddf4 555:296c73dcd286
1 """The Game Over Screen""" 1 """The Game Over Screen"""
2 import tempfile
3 import random 2 import random
3 import os
4 4
5 from pgu import gui 5 from pgu import gui
6 from pgu.high import Highs 6 from pgu.high import Highs
7 import pygame 7 import pygame
8 8
9 import engine 9 import engine
10 import constants 10 import constants
11 import imagecache 11 import imagecache
12 import config
12 13
13 WON, LOST, LEFT = range(3) 14 WON, LOST, LEFT = range(3)
14 15
15 WON_MESSAGES = [ 16 WON_MESSAGES = [
16 "You won.", 17 "You won.",
32 "What will your chickens do now?", 33 "What will your chickens do now?",
33 ] 34 ]
34 35
35 def ScoreTable(level): 36 def ScoreTable(level):
36 """Create and initialise a score table""" 37 """Create and initialise a score table"""
37 # We need a true file, so load will work, but, as we never save, 38 score_path = os.path.join(config.config.prefs_folder, "highscores.dat")
38 # the deletion doesn't bother us. 39 all_scores = Highs(score_path, 4)
39 our_scores = Highs(tempfile.NamedTemporaryFile(), 4) 40 all_scores.load()
40 #for mode in constants.TURN_LIMITS: 41 level_scores = all_scores[level.level_name]
41 # for score in range(700,1000,100):
42 # our_scores[mode].submit(score, 'No-one', None)
43 for score in range(700,1000,100):
44 our_scores[level.level_name].submit(score, 'No-one', None)
45 return our_scores
46 42
47 def create_game_over(gameboard, scores, level): 43 if not list(level_scores):
44 authors = [auth[2] for auth in constants.AUTHORS]
45 scores = [700+i*100 for i in range(len(authors))]
46 random.shuffle(scores)
47
48 for auth, score in zip(authors, scores):
49 level_scores.submit(score, auth, None)
50
51 level_scores.save()
52
53 return level_scores
54
55 def create_game_over(gameboard, level):
48 """Create a game over screen""" 56 """Create a game over screen"""
49 game_over = GameOver(gameboard, scores, level) 57 game_over = GameOver(gameboard, level)
50 return GameOverContainer(game_over, align=0, valign=0) 58 return GameOverContainer(game_over, align=0, valign=0)
59
51 60
52 class GameOverContainer(gui.Container): 61 class GameOverContainer(gui.Container):
53 def __init__(self, game_over, *args, **kwargs): 62 def __init__(self, game_over, *args, **kwargs):
54 gui.Container.__init__(self, *args, **kwargs) 63 gui.Container.__init__(self, *args, **kwargs)
55 self.add(game_over, 0, 0) 64 self.add(game_over, 0, 0)
66 def paint(self, s): 75 def paint(self, s):
67 pygame.display.set_caption('Game Over') 76 pygame.display.set_caption('Game Over')
68 pygame.display.get_surface().blit(self.splash, (0, 0)) 77 pygame.display.get_surface().blit(self.splash, (0, 0))
69 gui.Container.paint(self, s) 78 gui.Container.paint(self, s)
70 79
80
71 class GameOver(gui.Table): 81 class GameOver(gui.Table):
72 82
73 def __init__(self, gameboard, scoreboard, level, **params): 83 def __init__(self, gameboard, level, **params):
74 gui.Table.__init__(self, **params) 84 gui.Table.__init__(self, **params)
85 scoreboard = ScoreTable(level)
75 86
76 def return_pressed(): 87 def return_pressed():
77 pygame.event.post(engine.GO_MAIN_MENU) 88 pygame.event.post(engine.GO_MAIN_MENU)
78 89
79 def quit_pressed(): 90 def quit_pressed():
124 self.add_spacer() 135 self.add_spacer()
125 self.tr() 136 self.tr()
126 self.td(gui.Label("Final score : %d" % score, 137 self.td(gui.Label("Final score : %d" % score,
127 color=constants.FG_COLOR), colspan=3) 138 color=constants.FG_COLOR), colspan=3)
128 if made_list: 139 if made_list:
140 scoreboard.save()
129 self.tr() 141 self.tr()
130 if self.survived == WON: 142 if self.survived == WON:
131 self.td(gui.Label("You made the high scores", 143 self.td(gui.Label("You made the high scores",
132 color=constants.FG_COLOR), colspan=3) 144 color=constants.FG_COLOR), colspan=3)
133 else: 145 else:
158 self.td(quit_button, **td_kwargs) 170 self.td(quit_button, **td_kwargs)
159 171
160 def add_spacer(self, height=5): 172 def add_spacer(self, height=5):
161 self.tr() 173 self.tr()
162 self.td(gui.Spacer(0, height), colspan=3) 174 self.td(gui.Spacer(0, height), colspan=3)
175
176
177 class Scoreboard(gui.Table):
178
179 def __init__(self, level, **params):
180 gui.Table.__init__(self, **params)
181
182 scoreboard = ScoreTable(level)
183
184 self.tr()
185 self.td(gui.Label('Level: %s' % level.level_name, colspan=3))
186
187 for highscore in scoreboard:
188 self.tr()
189 self.td(gui.Label(highscore.name), colspan=2)
190 self.td(gui.Label('%d' % highscore.score))
191