Changeset 20:347667c941de


Ignore:
Timestamp:
Sep 1, 2013, 1:18:11 PM (7 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Message:

Hook up area.

Location:
nagslang
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • nagslang/engine.py

    r18 r20  
    55from nagslang import constants
    66from nagslang.screens.menu import MenuScreen
     7from nagslang.screens.area import AreaScreen
     8from nagslang.events import ScreenChange
    79
    810
     
    1214        self._clock = pygame.time.Clock()
    1315        self._fps = constants.FPS
    14         self._current_area = MenuScreen()
     16        self._world = None  # TODO: create the world
     17        self._current_screen = None
     18        self._screens = {
     19            'menu': MenuScreen,
     20            'level1': AreaScreen,
     21        }
     22        self.change_screen('menu')
     23
     24    def change_screen(self, new_screen):
     25        if self._current_screen is not None:
     26            self._current_screen.teardown()
     27        self._current_screen = self._screens[new_screen]()
     28        self._current_screen.setup(self._world)
    1529
    1630    def run(self):
     
    2034                if ev.type == pygame.locals.QUIT:
    2135                    running = False
     36                elif ScreenChange.matches(ev):
     37                    self.change_screen(ev.screen)
    2238                else:
    23                     self._current_area.handle_event(ev)
    24             self._current_area.render(self._surface)
     39                    self._current_screen.handle_event(ev)
     40            self._current_screen.render(self._surface)
    2541            pygame.display.flip()
    2642            self._clock.tick(self._fps)
  • nagslang/events.py

    r18 r20  
    11"""Events to post."""
    22
    3 from pygame import event
    4 import pygame.locals as pyl
     3import pygame
    54
    65
     
    109    @classmethod
    1110    def post(cls, **data):
    12         ev = event.Event(cls.TYPE, **data)
    13         event.post(ev)
     11        ev = pygame.event.Event(cls.TYPE, **data)
     12        pygame.event.post(ev)
     13
     14    @classmethod
     15    def matches(cls, ev):
     16        return ev.type == cls.TYPE
    1417
    1518
    1619class QuitEvent(Event):
    17     TYPE = pyl.QUIT
     20    TYPE = pygame.locals.QUIT
     21
     22
     23class UserEvent(Event):
     24    TYPE = pygame.locals.USEREVENT
     25
     26    @classmethod
     27    def post(cls, **data):
     28        super(UserEvent, cls).post(user_type=cls.__name__, **data)
     29
     30    @classmethod
     31    def matches(cls, ev):
     32        return (super(UserEvent, cls).matches(ev)
     33                and ev.user_type == cls.__name__)
     34
     35
     36class ScreenChange(UserEvent):
     37    @classmethod
     38    def post(cls, new_screen):
     39        super(ScreenChange, cls).post(screen=new_screen)
  • nagslang/screens/area.py

    r18 r20  
    11"""Display a game area."""
    22
     3import pygame
     4
    35from nagslang.screens.base import Screen
     6from nagslang.events import ScreenChange
    47
    58
    69class AreaScreen(Screen):
    710    def handle_event(self, ev):
    8         pass
     11        if ev.type == pygame.locals.KEYDOWN:
     12            if ev.key == pygame.locals.K_ESCAPE:
     13                ScreenChange.post('menu')
    914
    1015    def render(self, surface):
    11         pass
     16        surface.fill(pygame.color.Color(0, 0, 0))
  • nagslang/screens/base.py

    r18 r20  
    99    def __init__(self):
    1010        self.space = pymunk.Space()
     11
     12    def setup(self, world):
     13        """Perform setup based on the world state."""
     14        pass
     15
     16    def teardown(self):
     17        pass
    1118
    1219    def post_event(self, ev):
  • nagslang/screens/menu.py

    r19 r20  
    44
    55from nagslang.screens.base import Screen
    6 from nagslang.events import QuitEvent
     6from nagslang.events import QuitEvent, ScreenChange
    77
    88
     
    1010
    1111    def handle_event(self, ev):
    12         if (ev.type == pygame.locals.KEYDOWN
    13                 and ev.key == pygame.locals.K_ESCAPE):
    14             QuitEvent.post()
     12        if ev.type == pygame.locals.KEYDOWN:
     13            if ev.key == pygame.locals.K_ESCAPE:
     14                QuitEvent.post()
     15            elif ev.key == pygame.locals.K_1:
     16                ScreenChange.post('level1')
    1517
    1618    def render(self, surface):
Note: See TracChangeset for help on using the changeset viewer.