Skip to content

Instantly share code, notes, and snippets.

@RyanHope
Created February 22, 2012 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanHope/1880225 to your computer and use it in GitHub Desktop.
Save RyanHope/1880225 to your computer and use it in GitHub Desktop.
Integrate PyGame with twisted.internet's mainloop.
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"]
#!/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()
@Drvanon
Copy link

Drvanon commented Jul 4, 2012

So, where would we put the factory here?

@RyanHope
Copy link
Author

RyanHope commented Jul 4, 2012 via email

@RyanHope
Copy link
Author

Here is what I am using now: https://gist.github.com/3708020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment