view mamba/engine.py @ 19:6d195a3a4557

Somewhere for levels to live.
author Simon Cross <hodgestar@gmail.com>
date Sun, 11 Sep 2011 13:59:25 +0200
parents ad2bcbf492bf
children 30d4f3e62bcf
line wrap: on
line source

"""Game engine and top-level game loop."""

import pygame.event
import pygame.display
from pygame.locals import QUIT, USEREVENT


class Engine(object):
    def __init__(self):
        self._habitat = None

    def set_habitat(self, habitat):
        self._habitat = habitat

    def run(self):
        """Game loop."""
        get_events = pygame.event.get
        flip = pygame.display.flip
        surface = pygame.display.get_surface()
        while True:
            events = get_events()
            for ev in events:
                if ev.type is QUIT:
                    return
                self._habitat.dispatch(ev)
            self._habitat.draw(surface)
            flip()


class Habitat(object):
    def dispatch(self, ev):
        pass

    def draw(self, surface):
        pass


class UserEvent(object):

    utype = "UNKNOWN"

    @classmethod
    def post(cls, **kws):
        ev = pygame.event.Event(USEREVENT, utype=cls.utype, **kws)
        pygame.event.post(ev)

    @classmethod
    def matches(cls, ev):
        return ev.type is USEREVENT and ev.utype == cls.utype