annotate gamelib/mainmenu.py @ 566:a8dde729000a

Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
author Neil Muller <drnlmuller@gmail.com>
date Sat, 28 Nov 2009 19:30:06 +0000
parents 8cd13b82585e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
4 from pygame.locals import KEYDOWN, K_ESCAPE, K_s, K_i
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
124
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
7 import imagecache
512
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
8 import gameboard
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
9 import gameover
512
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
10 import savegame
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
11 import loadlevel
124
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
12
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
13 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
14 """Create a main menu"""
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
15 main_menu = MainMenu(level)
139
1d73de63bd71 Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents: 124
diff changeset
16
1d73de63bd71 Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents: 124
diff changeset
17 c = MenuContainer(align=0, valign=0)
1d73de63bd71 Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents: 124
diff changeset
18 c.add(main_menu, 0, 0)
1d73de63bd71 Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents: 124
diff changeset
19
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
20 return c
139
1d73de63bd71 Add basic game over screen
Neil Muller <drnlmuller@gmail.com>
parents: 124
diff changeset
21
124
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
22 class MenuContainer(gui.Container):
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
23 def paint(self, s):
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
24 pygame.display.set_caption(constants.NAME)
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
25 splash = imagecache.load_image("images/splash.png")
69fd96eafde8 Display splash screen and replace window title.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
26 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
27 gui.Container.paint(self, s)
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
28
346
6baf8b5beb5c Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents: 312
diff changeset
29 def get_mode(self):
6baf8b5beb5c Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents: 312
diff changeset
30 return self.widgets[0].mode
6baf8b5beb5c Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents: 312
diff changeset
31
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
32 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
33 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
34 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
35 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
36 if e.key == K_ESCAPE:
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
37 pygame.event.post(constants.DO_QUIT)
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
38 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
39 elif e.key == K_s:
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
40 pygame.event.post(constants.START_DAY)
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
41 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
42 elif e.key == K_i:
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
43 pygame.event.post(constants.GO_HELP_SCREEN)
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
44 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
45 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
46
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
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
48 class MainMenu(gui.Table):
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
49 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
50 gui.Table.__init__(self, **params)
346
6baf8b5beb5c Remove the "constant" constant
Neil Muller <drnlmuller@gmail.com>
parents: 312
diff changeset
51 self.mode = None
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
52 self.level = level
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
53
6
c0abad23a055 Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents: 5
diff changeset
54 def fullscreen_toggled():
5
67b79658b047 Refactor and simplify menu.
Simon Cross <hodgestar@gmail.com>
parents: 4
diff changeset
55 pygame.display.toggle_fullscreen()
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
56
6
c0abad23a055 Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents: 5
diff changeset
57 def quit_pressed():
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
58 pygame.event.post(constants.DO_QUIT)
6
c0abad23a055 Add start, quit and toggle fullscreen buttons to menu.
Simon Cross <hodgestar@gmail.com>
parents: 5
diff changeset
59
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
60 def start_game():
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
61 pygame.event.post(constants.START_DAY)
4
e8ddbc95cbf3 Silly (and broken) first stab at menu thing.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
62
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
63 def choose_level():
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
64 def load_func(new_level):
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
65 pygame.event.post(pygame.event.Event(constants.DO_LOAD_LEVEL, level=new_level))
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
66 self.level = new_level
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
67 self.redraw()
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
68 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
69
512
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
70 def load_game():
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
71 savegame.RestoreDialog(gameboard.GameBoard.restore_game).open()
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
72
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
73 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
74 gameover.ScoreDialog(self.level).open()
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
75
179
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
76 def help_pressed():
566
a8dde729000a Add 'Save and Quit' option to quit dialog. Move game events from engine to constants to ease 'import engine' fun
Neil Muller <drnlmuller@gmail.com>
parents: 560
diff changeset
77 pygame.event.post(constants.GO_HELP_SCREEN)
179
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
78
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
79 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
80 "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
81 }
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 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
83 "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
84 "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
85 }
389
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
86
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
87 change_button = gui.Button('Choose level')
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
88 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
89 self.tr()
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
90 self.td(change_button, **td_kwargs)
463802281182 Add basic level support (level choosing needs work)
Neil Muller <drnlmuller@gmail.com>
parents: 352
diff changeset
91
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
92 self.start_button = gui.Button(level.level_name)
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
93 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
94 self.tr()
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
95 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
96
512
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
97 loadgame_button = gui.Button('Restore Game')
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
98 loadgame_button.connect(gui.CLICK, load_game)
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
99 self.tr()
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
100 self.td(loadgame_button, **td_kwargs)
b112bcf4d435 Add restore game button to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 412
diff changeset
101
179
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
102 help_button = gui.Button("Instructions")
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
103 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
104 self.tr()
179
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
105 self.td(help_button, **td_kwargs)
e2b5262c2b11 Add basic help screen
Neil Muller <drnlmuller@gmail.com>
parents: 151
diff changeset
106
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
107 scores_button = gui.Button("High Scores")
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
108 scores_button.connect(gui.CLICK, scores_pressed)
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
109 self.tr()
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
110 self.td(scores_button, **td_kwargs)
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
111
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
112 quit_button = gui.Button("Quit")
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
113 quit_button.connect(gui.CLICK, quit_pressed)
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
114 self.tr()
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
115 self.td(quit_button, **td_kwargs)
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
116
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
117 # fullscreen_toggle = gui.Button("Toggle Fullscreen")
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
118 # 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
119 # 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
120 # self.td(fullscreen_toggle, **td_kwargs)
7
99c4f2226314 Add padding, centre menu.
Simon Cross <hodgestar@gmail.com>
parents: 6
diff changeset
121
532
0667189a5973 New load level dialog.
Simon Cross <hodgestar@gmail.com>
parents: 512
diff changeset
122 def redraw(self):
555
296c73dcd286 Add high score dialog to main menu.
Simon Cross <hodgestar@gmail.com>
parents: 532
diff changeset
123 self.start_button.value = self.level.level_name