Mercurial > rinkhals
annotate gamelib/gameover.py @ 296:010ab166c582
Add killed fox count to gameover. Tweak spacing
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sat, 05 Sep 2009 16:27:31 +0000 |
parents | d037e37c5240 |
children | dd1ffee5ccf5 |
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 |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
3 import random |
139 | 4 |
5 from pgu import gui | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
6 from pgu.high import High |
139 | 7 import pygame |
8 | |
9 import engine | |
10 import constants | |
11 import imagecache | |
12 | |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
13 WON, LOST, LEFT = range(3) |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
14 |
295
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
15 WON_MESSAGES = [ |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
16 "You won.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
17 "You survived!", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
18 "Your chickens will one day rule the world.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
19 ] |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
20 |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
21 LOST_MESSAGES = [ |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
22 "You lost.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
23 "You failed to protect your chickens.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
24 "Unopposed, the foxes overrun the farm.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
25 ] |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
26 |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
27 LEFT_MESSAGES = [ |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
28 "You gave up.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
29 "You sold your farm.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
30 "Real life got in the way.", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
31 "Are you siding with the foxes?", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
32 "What will your chickens do now?", |
d037e37c5240
Some endgame message changes.
Jeremy Thurgood <firxen@gmail.com>
parents:
246
diff
changeset
|
33 ] |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
34 |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
35 def ScoreTable(): |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
36 """Create and initialise a score table""" |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
37 # 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
|
38 # the deletion doesn't bother us. |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
39 our_scores = High(tempfile.NamedTemporaryFile(), 4) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
40 for score in range(700,1000,100): |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
41 our_scores.submit(score, 'No-one', None) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
42 return our_scores |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
43 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
44 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
|
45 """Create a game over screen""" |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
46 game_over = GameOver(gameboard, scores) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
47 return GameOverContainer(game_over, align=0, valign=0) |
139 | 48 |
49 class GameOverContainer(gui.Container): | |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
50 def __init__(self, game_over, *args, **kwargs): |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
51 gui.Container.__init__(self, *args, **kwargs) |
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
52 self.add(game_over, 0, 0) |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
53 if game_over.survived == WON: |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
54 self.splash = imagecache.load_image("images/gameover_win.png", |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
55 ["darken_center"]) |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
56 elif game_over.survived == LOST: |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
57 self.splash = imagecache.load_image("images/gameover_lose.png", |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
58 ["darken_center"]) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
59 else: |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
60 self.splash = imagecache.load_image("images/splash.png", |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
61 ["darken_center"]) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
62 |
139 | 63 def paint(self, s): |
64 pygame.display.set_caption('Game Over') | |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
65 pygame.display.get_surface().blit(self.splash, (0, 0)) |
139 | 66 gui.Container.paint(self, s) |
67 | |
68 class GameOver(gui.Table): | |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
69 |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
70 def __init__(self, gameboard, scoreboard, **params): |
139 | 71 gui.Table.__init__(self, **params) |
72 | |
73 def return_pressed(): | |
74 pygame.event.post(engine.GO_MAIN_MENU) | |
75 | |
76 def quit_pressed(): | |
77 pygame.event.post(engine.QUIT) | |
78 | |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
79 score = gameboard.cash + \ |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
80 constants.SELL_PRICE_CHICKEN * len(gameboard.chickens) + \ |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
81 constants.SELL_PRICE_EGG * gameboard.eggs |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
82 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
83 self.tr() |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
84 if gameboard.is_game_over(): |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
85 if len(gameboard.chickens) > 0: |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
86 self.survived = WON |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
87 scoreboard.submit(score, 'Player') |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
88 message = random.choice(WON_MESSAGES) |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
89 else: |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
90 self.survived = LOST |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
91 message = random.choice(LOST_MESSAGES) |
139 | 92 else: |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
93 self.survived = LEFT |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
94 message = random.choice(LEFT_MESSAGES) |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
95 self.td(gui.Label(message, color=constants.FG_COLOR), |
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
96 colspan=3) |
296
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
97 self.add_spacer() |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
98 # show the scoreboard |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
99 |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
100 for highscore in scoreboard: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
101 self.tr() |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
102 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
|
103 self.td(gui.Label('%d' % highscore.score, color=constants.FG_COLOR)) |
139 | 104 |
105 self.tr() | |
106 self.td(gui.Label("Groats : %d" % gameboard.cash, | |
107 color=constants.FG_COLOR)) | |
108 self.td(gui.Label(" Chickens : %d " % len(gameboard.chickens), | |
109 color=constants.FG_COLOR)) | |
110 self.td(gui.Label(" Eggs : %d" % gameboard.eggs, | |
111 color=constants.FG_COLOR)) | |
296
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
112 self.add_spacer() |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
113 self.tr() |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
114 self.td(gui.Label("You killed %d foxes" % gameboard.killed_foxes, |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
115 color=constants.FG_COLOR), colspan=3) |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
116 self.add_spacer() |
139 | 117 self.tr() |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
118 self.td(gui.Label("Final score : %d" % score, |
139 | 119 color=constants.FG_COLOR), colspan=3) |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
120 if scoreboard.check(score) is not None: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
121 self.tr() |
246
592bfad67488
Add 'You left' game over mode
Neil Muller <drnlmuller@gmail.com>
parents:
229
diff
changeset
|
122 if self.survived == WON: |
229
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
123 self.td(gui.Label("You made the high scores", |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
124 color=constants.FG_COLOR), colspan=3) |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
125 else: |
96d440bebdaa
Non-permenant high score table
Neil Muller <drnlmuller@gmail.com>
parents:
159
diff
changeset
|
126 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
|
127 color=constants.FG_COLOR), colspan=3) |
139 | 128 |
296
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
129 self.add_spacer(50) |
157
e3572b907028
Add splash images to game over screen.
Simon Cross <hodgestar@gmail.com>
parents:
151
diff
changeset
|
130 |
139 | 131 return_button = gui.Button("Return to Main Menu") |
132 return_button.connect(gui.CLICK, return_pressed) | |
133 | |
134 quit_button = gui.Button("Quit") | |
135 quit_button.connect(gui.CLICK, quit_pressed) | |
136 | |
137 style = { | |
138 "padding_bottom": 15, | |
139 } | |
140 td_kwargs = { | |
141 "align": 0, | |
142 "style": style, | |
143 "colspan": 3, | |
144 } | |
145 | |
146 self.tr() | |
147 self.td(return_button, **td_kwargs) | |
148 | |
149 self.tr() | |
150 self.td(quit_button, **td_kwargs) | |
296
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
151 |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
152 def add_spacer(self, height=5): |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
153 self.tr() |
010ab166c582
Add killed fox count to gameover. Tweak spacing
Neil Muller <drnlmuller@gmail.com>
parents:
295
diff
changeset
|
154 self.td(gui.Spacer(0, height), colspan=3) |