Skip to content

Instantly share code, notes, and snippets.

@RyanHope
Created September 12, 2012 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RyanHope/3708020 to your computer and use it in GitHub Desktop.
Save RyanHope/3708020 to your computer and use it in GitHub Desktop.
PyGame / Twisted integration with fixed fps screen updating and fast as possible event processing.
#!/usr/bin/env python
from twisted.internet import reactor
from twisted.internet.task import LoopingCall, Cooperator
import pygame
import pygame.font
pygame.display.init()
pygame.font.init()
count = 0
screen = pygame.display.set_mode((300, 300), 0)
font = pygame.font.Font(pygame.font.match_font("", True), 72)
def update_screen():
global count, screen, font
screen.fill((0, 0, 0))
fs = font.render(str(count), True, (255, 255, 255))
fss = fs.get_rect()
fss.center = (150, 150)
screen.blit(fs, fss)
pygame.display.flip()
count += 1
if count == 1000:
count = 0
def process_event():
while True:
for e in pygame.event.get():
if e.type != pygame.NOEVENT:
print e
yield
# Locked to fixed FPS
lc = LoopingCall(update_screen)
lc.start(1.0 / 30)
# Process events as fast as possible with out being greedy
coop = Cooperator()
coop.coiterate(process_event())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment