Mercurial > rinkhals
annotate gamelib/mainmenu.py @ 560:8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sat, 28 Nov 2009 18:27:59 +0000 |
parents | 296c73dcd286 |
children | a8dde729000a |
rev | line source |
---|---|
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
1 """Main menu.""" |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
2 |
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
3 from pgu import gui |
560
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
4 from pygame.locals import QUIT, KEYDOWN, K_ESCAPE |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
5 import pygame |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
6 import constants |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
7
diff
changeset
|
7 import engine |
124
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
8 import imagecache |
512
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
9 import gameboard |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
10 import gameover |
512
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
11 import savegame |
532 | 12 import loadlevel |
124
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
13 |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
14 def make_main_menu(level): |
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:
141
diff
changeset
|
15 """Create a main menu""" |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
16 main_menu = MainMenu(level) |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
124
diff
changeset
|
17 |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
124
diff
changeset
|
18 c = MenuContainer(align=0, valign=0) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
124
diff
changeset
|
19 c.add(main_menu, 0, 0) |
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
124
diff
changeset
|
20 |
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:
141
diff
changeset
|
21 return c |
139
1d73de63bd71
Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents:
124
diff
changeset
|
22 |
124
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
23 class MenuContainer(gui.Container): |
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
24 def paint(self, s): |
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
25 pygame.display.set_caption(constants.NAME) |
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
26 splash = imagecache.load_image("images/splash.png") |
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
27 pygame.display.get_surface().blit(splash, (0, 0)) |
69fd96eafde8
Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
28 gui.Container.paint(self, s) |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
29 |
346
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
30 def get_mode(self): |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
31 return self.widgets[0].mode |
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
32 |
560
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
33 def event(self, e): |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
34 if gui.Container.event(self, e): |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
35 return True |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
36 if e.type is KEYDOWN: |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
37 if e.key == K_ESCAPE: |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
38 pygame.event.post(pygame.event.Event(QUIT)) |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
39 return True |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
40 elif e.key == K_s: |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
41 pygame.event.post(engine.START_DAY) |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
42 return True |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
43 elif e.key == K_i: |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
44 pygame.event.post(engine.GO_HELP_SCREEN) |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
45 return True |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
46 return False |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
47 |
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
48 |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
49 class MainMenu(gui.Table): |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
50 def __init__(self, level, **params): |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
51 gui.Table.__init__(self, **params) |
346
6baf8b5beb5c
Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents:
312
diff
changeset
|
52 self.mode = None |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
53 self.level = level |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
54 |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
55 def fullscreen_toggled(): |
5
67b79658b047
Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents:
4
diff
changeset
|
56 pygame.display.toggle_fullscreen() |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
57 |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
58 def quit_pressed(): |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
7
diff
changeset
|
59 pygame.event.post(engine.QUIT) |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
60 |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
61 def start_game(): |
11
5d58a5b13731
Extremely rudimentary game engine.
Simon Cross <hodgestar@gmail.com>
parents:
7
diff
changeset
|
62 pygame.event.post(engine.START_DAY) |
4
e8ddbc95cbf3
Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff
changeset
|
63 |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
64 def choose_level(): |
532 | 65 def load_func(new_level): |
66 pygame.event.post(pygame.event.Event(engine.DO_LOAD_LEVEL, level=new_level)) | |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
67 self.level = new_level |
532 | 68 self.redraw() |
69 loadlevel.LoadLevelDialog(level, load_func).open() | |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
70 |
512
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
71 def load_game(): |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
72 savegame.RestoreDialog(gameboard.GameBoard.restore_game).open() |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
73 |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
74 def scores_pressed(): |
560
8cd13b82585e
Allow ESC to exit high score menu and then still exit game if pressed a second time.
Simon Cross <hodgestar@gmail.com>
parents:
555
diff
changeset
|
75 gameover.ScoreDialog(self.level).open() |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
76 |
179 | 77 def help_pressed(): |
78 pygame.event.post(engine.GO_HELP_SCREEN) | |
79 | |
312
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
80 style = { |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
81 "padding_bottom": 15, |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
82 } |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
83 td_kwargs = { |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
84 "align": 0, |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
85 "style": style, |
dd1ffee5ccf5
Use different score tables fot the different modes. Refactor game modes code as a result
Neil Muller <drnlmuller@gmail.com>
parents:
311
diff
changeset
|
86 } |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
87 |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
88 change_button = gui.Button('Choose level') |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
89 change_button.connect(gui.CLICK, choose_level) |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
90 self.tr() |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
91 self.td(change_button, **td_kwargs) |
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
92 |
532 | 93 self.start_button = gui.Button(level.level_name) |
94 self.start_button.connect(gui.CLICK, start_game) | |
389
463802281182
Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents:
352
diff
changeset
|
95 self.tr() |
532 | 96 self.td(self.start_button, **td_kwargs) |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
97 |
512
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
98 loadgame_button = gui.Button('Restore Game') |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
99 loadgame_button.connect(gui.CLICK, load_game) |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
100 self.tr() |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
101 self.td(loadgame_button, **td_kwargs) |
b112bcf4d435
Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
412
diff
changeset
|
102 |
179 | 103 help_button = gui.Button("Instructions") |
104 help_button.connect(gui.CLICK, help_pressed) | |
6
c0abad23a055
Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents:
5
diff
changeset
|
105 self.tr() |
179 | 106 self.td(help_button, **td_kwargs) |
107 | |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
108 scores_button = gui.Button("High Scores") |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
109 scores_button.connect(gui.CLICK, scores_pressed) |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
110 self.tr() |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
111 self.td(scores_button, **td_kwargs) |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
112 |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
113 quit_button = gui.Button("Quit") |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
114 quit_button.connect(gui.CLICK, quit_pressed) |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
115 self.tr() |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
116 self.td(quit_button, **td_kwargs) |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
117 |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
118 # fullscreen_toggle = gui.Button("Toggle Fullscreen") |
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
119 # fullscreen_toggle.connect(gui.CLICK, fullscreen_toggled) |
352
79fe97f8e8f5
Disable full screen toggling since it doesn't appear to work on Windows and Mac.
Simon Cross <hodgestar@gmail.com>
parents:
346
diff
changeset
|
120 # self.tr() |
79fe97f8e8f5
Disable full screen toggling since it doesn't appear to work on Windows and Mac.
Simon Cross <hodgestar@gmail.com>
parents:
346
diff
changeset
|
121 # self.td(fullscreen_toggle, **td_kwargs) |
7 | 122 |
532 | 123 def redraw(self): |
555
296c73dcd286
Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents:
532
diff
changeset
|
124 self.start_button.value = self.level.level_name |