Created
February 22, 2012 00:42
-
-
Save RyanHope/1880225 to your computer and use it in GitHub Desktop.
Integrate PyGame with twisted.internet's mainloop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
def _update( callback, fps, reactor, clock ): | |
if not callback(): | |
reactor.stop() | |
else: | |
delay = 1.0 / fps - ( clock.tick() / 1000.0 - ( 20.0 / fps ) / fps ) | |
if delay < 0: | |
reactor.callLater( 0 , _update, callback, fps, reactor, clock ) | |
else: | |
reactor.callLater( delay , _update, callback, fps, reactor, clock ) | |
print clock.get_fps() | |
def install( callback, fps = 30, reactor = None ): | |
if reactor is None: | |
from twisted.internet import reactor | |
if callback: | |
_update( callback, fps, reactor, pygame.time.Clock() ) | |
__all__ = ["install"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from twisted.internet import reactor | |
from random import random | |
import pygamesupport | |
import time | |
def doWork(): | |
time.sleep( random() / 50 ) | |
return True | |
if __name__ == '__main__': | |
pygamesupport.install( doWork, fps = 30 ) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is what I am using now: https://gist.github.com/3708020