Mercurial > rinkhals
annotate gamelib/gameover.py @ 237:4a87bfa5cc63
Twiddle animation cycle
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 09:48:48 +0000 |
parents | 96d440bebdaa |
children | 592bfad67488 |
rev | line source |
---|---|
139 | 1 """The Game Over Screen""" |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
2 import tempfile |
139 | 3 |
4 from pgu import gui | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
5 from pgu.high import High |
139 | 6 import pygame |
7 | |
8 import engine | |
9 import constants | |
10 import imagecache | |
11 | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
12 def ScoreTable(): |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
13 """Create and initialise a score table""" |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
14 # We need a true file, so load will work, but, as we never save, |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
15 # the deletion doesn't bother us. |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
16 our_scores = High(tempfile.NamedTemporaryFile(), 4) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
17 for score in range(700,1000,100): |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
18 our_scores.submit(score, 'No-one', None) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
19 return our_scores |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
20 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
21 def create_game_over(gameboard, scores): |
151
082868bea873
Refactor UI so that only a single gui.App is used. Pass all UI events via main_app. Change Toolbar table to use .td() everywhere. Move toolbar to top.
Simon Cross <hodgestar@gmail.com>
parents:
139
diff
changeset
|
22 """Create a game over screen""" |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
23 game_over = GameOver(gameboard, scores) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
24 return GameOverContainer(game_over, align=0, valign=0) |
139 | 25 |
26 class GameOverContainer(gui.Container): | |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
27 def __init__(self, game_over, *args, **kwargs): |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
28 gui.Container.__init__(self, *args, **kwargs) |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
29 self.add(game_over, 0, 0) |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
30 if game_over.survived: |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
157
diff
changeset
|
31 self.splash = imagecache.load_image("images/gameover_win.png", ["darken_center"]) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
32 else: |
159
7bb8d9d6858a
Darken center of game over splash screen to make text legible.
Simon Cross <hodgestar@gmail.com>
parents:
157
diff
changeset
|
33 self.splash = imagecache.load_image("images/gameover_lose.png", ["darken_center"]) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
34 |
139 | 35 def paint(self, s): |
36 pygame.display.set_caption('Game Over') | |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
37 pygame.display.get_surface().blit(self.splash, (0, 0)) |
139 | 38 gui.Container.paint(self, s) |
39 | |
40 class GameOver(gui.Table): | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
41 def __init__(self, gameboard, scoreboard, **params): |
139 | 42 gui.Table.__init__(self, **params) |
43 | |
44 def return_pressed(): | |
45 pygame.event.post(engine.GO_MAIN_MENU) | |
46 | |
47 def quit_pressed(): | |
48 pygame.event.post(engine.QUIT) | |
49 | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
50 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
51 score = gameboard.cash + \ |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
52 constants.SELL_PRICE_CHICKEN * len(gameboard.chickens) + \ |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
53 constants.SELL_PRICE_EGG * gameboard.eggs |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
54 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
55 self.tr() |
139 | 56 if len(gameboard.chickens) > 0: |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
57 self.survived = True |
139 | 58 self.td(gui.Label("You Survived", color=constants.FG_COLOR), |
59 colspan=3) | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
60 scoreboard.submit(score, 'Player') |
139 | 61 else: |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
62 self.survived = False |
139 | 63 self.td(gui.Label("You Lost", color=constants.FG_COLOR), |
64 colspan=3) | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
65 # show the scoreboard |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
66 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
67 for highscore in scoreboard: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
68 self.tr() |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
69 self.td(gui.Label(highscore.name, color=constants.FG_COLOR), colspan=2) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
70 self.td(gui.Label('%d' % highscore.score, color=constants.FG_COLOR)) |
139 | 71 |
72 self.tr() | |
73 self.td(gui.Label("Groats : %d" % gameboard.cash, | |
74 color=constants.FG_COLOR)) | |
75 self.td(gui.Label(" Chickens : %d " % len(gameboard.chickens), | |
76 color=constants.FG_COLOR)) | |
77 self.td(gui.Label(" Eggs : %d" % gameboard.eggs, | |
78 color=constants.FG_COLOR)) | |
79 self.tr() | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
80 self.td(gui.Label("Final score : %d" % score, |
139 | 81 color=constants.FG_COLOR), colspan=3) |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
82 if scoreboard.check(score) is not None: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
83 self.tr() |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
84 if self.survived: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
85 self.td(gui.Label("You made the high scores", |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
86 color=constants.FG_COLOR), colspan=3) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
87 else: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
88 self.td(gui.Label("Pity, you could have made the high scores", |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
89 color=constants.FG_COLOR), colspan=3) |
139 | 90 |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
91 self.tr() |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
92 self.td(gui.Spacer(0, 50), colspan=3) |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
93 |
139 | 94 return_button = gui.Button("Return to Main Menu") |
95 return_button.connect(gui.CLICK, return_pressed) | |
96 | |
97 quit_button = gui.Button("Quit") | |
98 quit_button.connect(gui.CLICK, quit_pressed) | |
99 | |
100 style = { | |
101 "padding_bottom": 15, | |
102 } | |
103 td_kwargs = { | |
104 "align": 0, | |
105 "style": style, | |
106 "colspan": 3, | |
107 } | |
108 | |
109 self.tr() | |
110 self.td(return_button, **td_kwargs) | |
111 | |
112 self.tr() | |
113 self.td(quit_button, **td_kwargs) |