Changeset 14:fb1bd081cc86


Ignore:
Timestamp:
Apr 3, 2011, 2:35:18 PM (9 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
rebase_source:
84ec079f4889b68122e5881e92eeb4c5e2c0e785
Message:

Add scene changing.

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/__main__.py

    r13 r14  
    33
    44from constants import SCREEN, FREQ, BITSIZE, CHANNELS, BUFFER, DEBUG
    5 from engine import Engine
     5from engine import Engine, Scene
    66
    77import pygame
     
    4848
    4949    engine = Engine()
     50    engine.change_scene(Scene())
    5051    try:
    5152        engine.run()
  • skaapsteker/engine.py

    r11 r14  
    44import pygame.time
    55import pygame.event
    6 from pygame.locals import QUIT
     6from pygame.locals import QUIT, USEREVENT
    77
    88class Engine(object):
     
    1010    def __init__(self):
    1111        self._framerate = 60
     12        self._current_scene = None
     13
     14    def change_scene(self, next_scene):
     15        if self._current_scene is not None:
     16            self._current_scene.leave()
     17        self._current_scene = next_scene
     18        self._current_scene.enter()
    1219
    1320    def run(self):
    1421        """Run the game loop dispatching events as necessary."""
     22        assert self._current_scene is not None
    1523        clock = pygame.time.Clock()
     24        surface = pygame.display.get_surface()
    1625        while True:
    1726            events = pygame.event.get()
     
    1928                if ev.type is QUIT:
    2029                    return
     30                if ChangeScene.matches(ev):
     31                    self.change_scene(ev.next_scene)
     32                    break
     33                self._current_scene.dispatch(ev)
     34            self._current_scene.draw(surface)
     35            pygame.display.flip()
    2136            clock.tick(self._framerate)
     37
     38
     39class Scene(object):
     40
     41    def __init__(self):
     42        pass
     43
     44    def post(self, ev):
     45        """Post an event to pygame's event loop."""
     46        pygame.event.post(ev)
     47
     48    def enter(self):
     49        """Enter the scene."""
     50        pass
     51
     52    def leave(self):
     53        """Exit the scene."""
     54        pass
     55
     56    def dispatch(self, ev):
     57        """Dispatch an event."""
     58        pass
     59
     60    def draw(self, surface):
     61        """Update the scene surface."""
     62        pass
     63
     64
     65class UserEvent(object):
     66
     67    utype = "UNKNOWN"
     68
     69    @classmethod
     70    def post(cls, **kws):
     71        ev = pygame.event.Event(USEREVENT, utype=cls.utype, **kws)
     72        pygame.event.post(ev)
     73
     74    @classmethod
     75    def matches(cls, ev):
     76        return ev.type is USEREVENT and ev.utype == cls.utype
     77
     78class ChangeScene(UserEvent):
     79
     80    utype = "CHANGE_SCENE"
     81
     82    @classmethod
     83    def post(cls, next_scene):
     84        super(ChangeScene, cls).post(next_scene=next_scene)
Note: See TracChangeset for help on using the changeset viewer.