source: nagslang/screens/menu.py@ 325:52b5a822be26

Last change on this file since 325:52b5a822be26 was 325:52b5a822be26, checked in by Simon Cross <hodgestar@…>, 8 years ago

Load first level on play start.

File size: 1.9 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.level import Level
8from nagslang.widgets.text import TextWidget, MultiLineWidget
9
10
11class MenuScreen(Screen):
12 def setup(self):
13 # Position is hacked later
14 self.cursor = TextWidget((0, 0), u'\N{Rightwards Arrow}',
15 colour='red')
16 self.cursor_pos = 0
17 self.options = [
18 self.play,
19 self.restart,
20 self.quit,
21 ]
22 self.widgets = [
23 TextWidget((10, 10), 'Menu', fontsize=20),
24 TextWidget((40, 50), 'Play the game'),
25 TextWidget((40, 70), 'Start the game from the beginning again'),
26 TextWidget((40, 90), 'Quit'),
27 MultiLineWidget((60, 120), self.world.get_formatted_stats()),
28 self.cursor,
29 ]
30
31 def handle_event(self, ev):
32 if ev.type == pygame.locals.KEYDOWN:
33 if ev.key == pygame.locals.K_ESCAPE:
34 self.quit()
35 elif ev.key == pygame.locals.K_DOWN:
36 self.cursor_pos = (self.cursor_pos + 1) % len(self.options)
37 elif ev.key == pygame.locals.K_UP:
38 self.cursor_pos = (self.cursor_pos - 1) % len(self.options)
39 elif ev.key == pygame.locals.K_RETURN:
40 self.options[self.cursor_pos]()
41
42 def render(self, surface):
43 surface.fill(pygame.color.Color(255, 255, 255))
44 self.cursor.pos = (10, 50 + 20 * self.cursor_pos)
45 self.cursor.rect.top = self.cursor.pos[1]
46 for widget in self.widgets:
47 widget.draw(surface)
48
49 def play(self):
50 first_level = Level.list_levels()[0]
51 ScreenChange.post(first_level)
52
53 def restart(self):
54 self.world.reset()
55 self.setup()
56
57 def quit(self):
58 QuitEvent.post()
Note: See TracBrowser for help on using the repository browser.