view mamba/engine.py @ 25:b38411d253e3

Typo
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 11 Sep 2011 14:22:39 +0200
parents 30d4f3e62bcf
children 047273a63054
line wrap: on
line source

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

from mamba.widgets.base import Container
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 __init__(self):
        self.container = Container()

    def dispatch(self, ev):
        self.container.event(ev)

    def draw(self, surface):
        self.container.draw(surface)


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