source: nagslang/screens/menu.py@ 238:28d906fc2ab1

Last change on this file since 238:28d906fc2ab1 was 238:28d906fc2ab1, checked in by Neil Muller <drnlmuller@…>, 8 years ago

Add a world object to collect some stats

File size: 1.8 KB
RevLine 
[18]1"""Display a menu screen."""
2
[19]3import pygame
[18]4
5from nagslang.screens.base import Screen
[20]6from nagslang.events import QuitEvent, ScreenChange
[238]7from nagslang.widgets.text import TextWidget, MultiLineWidget
[18]8
9
10class MenuScreen(Screen):
[226]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'),
[238]26 MultiLineWidget((60, 120), self.world.get_formatted_stats()),
[226]27 self.cursor,
28 ]
[18]29
30 def handle_event(self, ev):
[20]31 if ev.type == pygame.locals.KEYDOWN:
32 if ev.key == pygame.locals.K_ESCAPE:
[226]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]()
[18]40
41 def render(self, surface):
[29]42 surface.fill(pygame.color.Color(255, 255, 255))
[226]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 new_game(self):
49 ScreenChange.post('level1', None)
50
51 def load_game(self):
52 raise NotImplementedError()
53
54 def quit(self):
55 QuitEvent.post()
Note: See TracBrowser for help on using the repository browser.