1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
from __future__ import generators
# import Twisted and install
from twisted.internet import _threadedselect
_threadedselect.install()
from twisted.internet import reactor
import os
import pygame
from pygame.locals import *
try:
import pygame.fastevent as eventmodule
except ImportError:
import pygame.event as eventmodule
# You can customize this if you use your
# own events, but you must OBEY:
#
# USEREVENT <= TWISTEDEVENT < NUMEVENTS
#
TWISTEDEVENT = USEREVENT
def postTwistedEvent(func):
# if not using pygame.fastevent, this can explode if the queue
# fills up.. so that's bad. Use pygame.fastevent, in pygame CVS
# as of 2005-04-18.
eventmodule.post(eventmodule.Event(TWISTEDEVENT, iterateTwisted=func))
def helloWorld():
print "hello, world"
reactor.callLater(1, helloWorld)
reactor.callLater(1, helloWorld)
def twoSecondsPassed():
print "two seconds passed"
reactor.callLater(2, twoSecondsPassed)
def eventIterator():
while True:
yield eventmodule.wait()
while True:
event = eventmodule.poll()
if event.type == NOEVENT:
break
else:
yield event
def main():
pygame.init()
if hasattr(eventmodule, 'init'):
eventmodule.init()
screen = pygame.display.set_mode((300, 300))
# send an event when twisted wants attention
reactor.interleave(postTwistedEvent)
# make shouldQuit a True value when it's safe to quit
# by appending a value to it. This ensures that
# Twisted gets to shut down properly.
shouldQuit = []
reactor.addSystemEventTrigger('after', 'shutdown', shouldQuit.append, True)
for event in eventIterator():
if event.type == TWISTEDEVENT:
event.iterateTwisted()
if shouldQuit:
break
elif event.type == QUIT:
reactor.stop()
elif event.type == KEYDOWN and event.key == K_ESCAPE:
reactor.stop()
pygame.quit()
if __name__ == '__main__':
main()
|