source: skaapsteker/menuscene.py @ 110:f64c38c5512d

Last change on this file since 110:f64c38c5512d was 110:f64c38c5512d, checked in by Stefano Rivera <stefano@…>, 10 years ago

Use TextChoice? data in menu screen (it's no longer hacked to death)

File size: 1.5 KB
Line 
1import pygame
2from pygame.locals import (K_ESCAPE, K_DOWN, K_UP, K_q, KEYDOWN, QUIT)
3
4from .engine import ChangeScene, Scene
5from .levelscene import LevelScene
6from .widgets.text import Text, TextChoice
7
8class MenuScene(Scene):
9    def __init__(self, cur_game=None):
10        super(MenuScene, self).__init__()
11        self.widgets.append(Text("MENU:", (50, 50), color='white', size=48))
12        self.cur_game = cur_game
13        menu_options = [
14            ('Level 1', 'level1'),
15            ('Level 2', 'level2'),
16            ('Quit', 'quit'),
17        ]
18        if cur_game is not None:
19            menu_options.insert(0, ('Resume Game', 'resume'))
20        self.choice = TextChoice(menu_options, (50, 100), color='white')
21        self.choice.callbacks.append(self.selected)
22        self.widgets.append(self.choice)
23
24    def selected(self, option, data):
25        "Callback from menu TextChoice"
26        if data.startswith('level'):
27            ChangeScene.post(LevelScene(data))
28        elif data == 'quit':
29            pygame.event.post(pygame.event.Event(QUIT))
30        elif data == 'resume':
31            self.cur_game.thaw()
32            ChangeScene.post(self.cur_game)
33
34    def draw(self, surface):
35        surface.fill(pygame.Color('black'))
36        super(MenuScene, self).draw(surface)
37
38    def dispatch(self, ev):
39        if ev.type is KEYDOWN:
40            if ev.key in(K_q, K_ESCAPE):
41                pygame.event.post(pygame.event.Event(QUIT))
42        super(MenuScene, self).dispatch(ev)
Note: See TracBrowser for help on using the repository browser.