[Twisted-Python] [WISH] pygame reactor

Brian Warner warner at lothar.com
Wed Dec 17 17:51:01 EST 2003

> My hope is, now that pyui is somewhat death or windows only, to have a
> pygame reactor maybe using pygame.event.wait() which idles till a
> pygame event comes.

Perhaps as a way of getting started, Paul Boehm posted the following code to
this list in October of 2002. Unlike Twisted, pygame isn't very quiescent, so
there isn't a way to just sleep until something needs to be done. Instead, in
this code, twisted runs the pygame update routine as frequently as necessary
to achieve the desired frame rate. This probably has implications for
response latency to joystick events, etc.. I haven't actually tried to run
this in a long time (since about November of 2002 :).


# MyGame embedded in Twisted

from pygame import time
from twisted.internet import reactor

import mygame

class PygameTimer:
    def __init__(self, game):
        self.clock = time.Clock()
        self.game = game

    def update(self):
        self.ms = self.clock.get_rawtime()

        framespeed = (1.0/100.0) * 1000
        lastspeed = self.ms
        next = framespeed - lastspeed

        print "framespeed", framespeed, "ms", self.ms, "next", next, "fps", self.clock.get_fps()


        if self.game.want_quit:
            reactor.callLater(next/1000.0*2.0, self.update)

if __name__ == "__main__":
    pt = PygameTimer(mygame.Game())

More information about the Twisted-Python mailing list