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