source: skaapsteker/menuscene.py @ 109:982f34d0aac0

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

Add optional data to identify TextChoice? options

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        if cur_game is None:
14            self.choice = TextChoice(("level1", "level2", "Quit"), (50, 100), color='white')
15        else:
16            self.choice = TextChoice(("level1", "level2", "Resume Game", "Quit"), (50, 100), color='white')
17        self.choice.callbacks.append(self.selected)
18        self.widgets.append(self.choice)
19
20    def selected(self, option, data):
21        "Callback from menu TextChoice"
22        if option == 0:
23            ChangeScene.post(LevelScene('level1'))
24        elif option == 1:
25            ChangeScene.post(LevelScene('level2'))
26        elif option == len(self.choice.options) - 1:
27            pygame.event.post(pygame.event.Event(QUIT))
28        elif option == 2 and self.cur_game is not None:
29            self.cur_game.thaw()
30            ChangeScene.post(self.cur_game)
31
32    def draw(self, surface):
33        surface.fill(pygame.Color('black'))
34        super(MenuScene, self).draw(surface)
35
36    def dispatch(self, ev):
37        if ev.type is KEYDOWN:
38            if ev.key in(K_q, K_ESCAPE):
39                pygame.event.post(pygame.event.Event(QUIT))
40        super(MenuScene, self).dispatch(ev)
Note: See TracBrowser for help on using the repository browser.