source: nagslang/screens/menu.py@ 276:3153196517fc

Last change on this file since 276:3153196517fc was 276:3153196517fc, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Move protagonist to the world

File size: 1.8 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, MultiLineWidget
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.play,
18 self.restart,
19 self.quit,
20 ]
21 self.widgets = [
22 TextWidget((10, 10), 'Menu', fontsize=20),
23 TextWidget((40, 50), 'Play the game'),
24 TextWidget((40, 70), 'Start the game from the beginning again'),
25 TextWidget((40, 90), 'Quit'),
26 MultiLineWidget((60, 120), self.world.get_formatted_stats()),
27 self.cursor,
28 ]
29
30 def handle_event(self, ev):
31 if ev.type == pygame.locals.KEYDOWN:
32 if ev.key == pygame.locals.K_ESCAPE:
33 self.quit()
34 elif ev.key == pygame.locals.K_DOWN:
35 self.cursor_pos = (self.cursor_pos + 1) % len(self.options)
36 elif ev.key == pygame.locals.K_UP:
37 self.cursor_pos = (self.cursor_pos - 1) % len(self.options)
38 elif ev.key == pygame.locals.K_RETURN:
39 self.options[self.cursor_pos]()
40
41 def render(self, surface):
42 surface.fill(pygame.color.Color(255, 255, 255))
43 self.cursor.pos = (10, 50 + 20 * self.cursor_pos)
44 self.cursor.rect.top = self.cursor.pos[1]
45 for widget in self.widgets:
46 widget.draw(surface)
47
48 def play(self):
49 ScreenChange.post('level1')
50
51 def restart(self):
52 self.world.reset()
53 self.setup()
54
55 def quit(self):
56 QuitEvent.post()
Note: See TracBrowser for help on using the repository browser.