source: nagslang/screens/menu.py@ 226:e89a43d208b9

Last change on this file since 226:e89a43d208b9 was 226:e89a43d208b9, checked in by Stefano Rivera <stefano@…>, 8 years ago

A stupid, simple menu

File size: 1.7 KB
Line 
1"""Display a menu screen."""
2
3import pygame
4
5from nagslang.screens.base import Screen
6from nagslang.events import QuitEvent, ScreenChange
7from nagslang.widgets.text import TextWidget
8
9
10class MenuScreen(Screen):
11 def setup(self):
12 # Position is hacked later
13 self.cursor = TextWidget((0, 0), u'\N{Rightwards Arrow}',
14 colour='red')
15 self.cursor_pos = 0
16 self.options = [
17 self.new_game,
18 self.load_game,
19 self.quit,
20 ]
21 self.widgets = [
22 TextWidget((10, 10), 'Menu', fontsize=20),
23 TextWidget((40, 50), 'Start new game'),
24 TextWidget((40, 70), 'Restore saved game'),
25 TextWidget((40, 90), 'Quit'),
26 self.cursor,
27 ]
28
29 def handle_event(self, ev):
30 if ev.type == pygame.locals.KEYDOWN:
31 if ev.key == pygame.locals.K_ESCAPE:
32 self.quit()
33 elif ev.key == pygame.locals.K_DOWN:
34 self.cursor_pos = (self.cursor_pos + 1) % len(self.options)
35 elif ev.key == pygame.locals.K_UP:
36 self.cursor_pos = (self.cursor_pos - 1) % len(self.options)
37 elif ev.key == pygame.locals.K_RETURN:
38 self.options[self.cursor_pos]()
39
40 def render(self, surface):
41 surface.fill(pygame.color.Color(255, 255, 255))
42 self.cursor.pos = (10, 50 + 20 * self.cursor_pos)
43 self.cursor.rect.top = self.cursor.pos[1]
44 for widget in self.widgets:
45 widget.draw(surface)
46
47 def new_game(self):
48 ScreenChange.post('level1', None)
49
50 def load_game(self):
51 raise NotImplementedError()
52
53 def quit(self):
54 QuitEvent.post()
Note: See TracBrowser for help on using the repository browser.