source: nagslang/screens/menu.py@ 252:73d6abf5ba22

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

Save game state

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 = [
[252]17 self.play,
18 self.restart,
[226]19 self.quit,
20 ]
21 self.widgets = [
22 TextWidget((10, 10), 'Menu', fontsize=20),
[252]23 TextWidget((40, 50), 'Play the game'),
24 TextWidget((40, 70), 'Start the game from the beginning again'),
[226]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
[252]48 def play(self):
[226]49 ScreenChange.post('level1', None)
50
[252]51 def restart(self):
52 self.world.reset()
53 self.setup()
[226]54
55 def quit(self):
56 QuitEvent.post()
Note: See TracBrowser for help on using the repository browser.