[Twisted-Python] jfi: twisted pygame :)

Paul Boehm typo at soniq.net
Wed Oct 9 21:48:41 EDT 2002


On Wed, Oct 09, 2002 at 05:36:42PM -0700, Brian Warner wrote:
> Cool! Could I persuade you to post a simple example of how you integrated the
> event loops? I'm just starting to get into pygame; it would make multiplayer
> stuff a lot easier if I could use twisted at the same time.

sure,
while it still needs lot of refining, this is a start:

# 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
        self.update()

    def update(self):
        self.clock.tick()
        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()

        self.game.iterate()

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


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





More information about the Twisted-Python mailing list