usr bin env python coding utf8 import pygame gc from pygame locals imp

 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
#!/usr/bin/env python
#-*- coding: utf8 -*-
import pygame, gc
from pygame.locals import *
from twisted.protocols.basic import LineOnlyReceiver
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet import reactor, threads
def pygame_loop():
pygame.init()
pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
reactor.stop()
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
elif event.type == KEYDOWN and event.key == K_SPACE:
print "space"
# send something
pass
class MyProtocol(LineOnlyReceiver):
def connectionMade(self):
print "connectionMade"
# print gc.get_referrers(self)
def lineReceived(self, line):
print line
class MyFactory(ReconnectingClientFactory):
protocol = MyProtocol
def clientConnectionFailed(self, connector, reason):
print "Connection failed - goodbye!"
reactor.stop()
def clientConnectionLost(self, connector, reason):
print "Connection lost - goodbye!"
reactor.stop()
threads.deferToThread(pygame_loop)
factory = MyFactory()
reactor.connectTCP("78.85.128.251", 1915, factory)
reactor.run()