dcad16b3d27057898a2f80e6d448b6b06ce24d38
[tabakrolletjie.git] / tabakrolletjie / scenes / menu.py
1 """ Menu scene. """
2
3 import pygame.locals as pgl
4
5 from .base import BaseScene
6 from ..events import QuitEvent, SceneChangeEvent
7 from ..loader import loader
8 from ..constants import FONTS
9 from ..widgets import TextButton
10
11
12 class MenuScene(BaseScene):
13     def enter(self, gamestate):
14         if gamestate.station is None:
15             print "Loading Station Alpha ..."
16             gamestate.set_station(loader.load_station("station-alpha.json"))
17         font_title = loader.load_font(FONTS['bold'], size=32)
18         self._title = font_title.render('A Game with a title', True,
19                                         (255, 255, 255))
20         self._menu = [
21             TextButton("Load Level", (255, 255, 255), name='load level'),
22             TextButton("Start Game (Day)", (255, 255, 255), name='start game'),
23             TextButton("Load Saved Game", (255, 255, 255), name='load game'),
24             TextButton("Help", (255, 255, 255), name='help'),
25         ]
26
27     def render(self, surface, gamestate):
28         surface.fill((0, 128, 0))
29
30         pos = ((surface.get_width() - self._title.get_width()) / 2, 50)
31         surface.blit(self._title, pos, None)
32
33         height = 150
34         for item in self._menu:
35             if not item.position:
36                 item.position = ((surface.get_width() - item.get_width()) / 2,
37                                  height)
38             item.render(surface)
39             height += item.get_height() + 30
40
41     def _get_pressed(self, ev):
42         for button in self._menu:
43             if button.pressed(ev):
44                 return button.name
45         return None
46
47     def _do_day(self):
48         from .day import DayScene
49         SceneChangeEvent.post(scene=DayScene())
50
51     def _do_load_level(self):
52         from .load_level import LoadLevelScene
53         SceneChangeEvent.post(scene=LoadLevelScene())
54
55     def _do_help(self):
56         from .help import HelpScene
57         SceneChangeEvent.post(scene=HelpScene())
58
59     def event(self, ev, gamestate):
60         if ev.type == pgl.KEYDOWN:
61             if ev.key in (pgl.K_q, pgl.K_ESCAPE):
62                 QuitEvent.post()
63             elif ev.key == pgl.K_n:
64                 from .night import NightScene
65                 SceneChangeEvent.post(scene=NightScene())
66             elif ev.key == pgl.K_d:
67                 self._do_day()
68             elif ev.key == pgl.K_l:
69                 self._do_load_level()
70             elif ev.key == pgl.K_h:
71                 self._do_help()
72         elif ev.type == pgl.MOUSEBUTTONDOWN:
73             pressed = self._get_pressed(ev)
74             if pressed == 'load level':
75                 self._do_load_level()
76             elif pressed == 'start game':
77                 self._do_day()
78             elif pressed == 'help':
79                 self._do_help()
80             elif pressed:
81                 print 'Pressed', pressed