Changeset 226:e89a43d208b9


Ignore:
Timestamp:
Sep 4, 2013, 4:30:08 PM (7 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
Message:

A stupid, simple menu

File:
1 edited

Legend:

Unmodified
Added
Removed
  • nagslang/screens/menu.py

    r180 r226  
    99
    1010class 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        ]
    1128
    1229    def handle_event(self, ev):
    1330        if ev.type == pygame.locals.KEYDOWN:
    1431            if ev.key == pygame.locals.K_ESCAPE:
    15                 QuitEvent.post()
    16             elif ev.key == pygame.locals.K_1:
    17                 ScreenChange.post('level1', None)
     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]()
    1839
    1940    def render(self, surface):
    2041        surface.fill(pygame.color.Color(255, 255, 255))
    21         TextWidget((10, 10), 'Menu').draw(surface)
    22         TextWidget((10, 30), 'Press 1 to start').draw(surface)
     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 TracChangeset for help on using the changeset viewer.