~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/doc/core/examples/threadedselect/pygamedemo.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

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