view mamba/engine.py @ 13:f5846a46e9c5

Add base class for user events.
author Simon Cross <hodgestar@gmail.com>
date Sun, 11 Sep 2011 13:22:14 +0200
parents 0196455fa432
children ad2bcbf492bf
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):
        pass

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


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