ツイストしたpygamesプログラムを実行していますが、pygameイベント内からデータを送信する際に問題が発生します。
最初にここにサーバーがあります:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import simplejson
class Game_Data(LineReceiver):
def __init__(self, players, clients):
self.players = players
self.clients = clients
## the connectionMade method of LineReceiver is being used to create ##
## protocol instances of each client so it can send back any data it gets ##
def connectionMade(self):
new_player = 'player_' + str(len(self.players) + 1)
self.clients.append(self)
self.players.append(new_player)
self.players = simplejson.dumps(self.players)
for client in self.clients:
client.sendLine(self.players)
## what ever data the server receives it sends right back to any clients ##
def lineReceived(self,line):
self.line = line
print self.line
for client in self.clients:
client.sendLine(self.line)
class BlackFactory(Factory):
def __init__(self):
self.players = []
self.clients = []
def buildProtocol(self, addr):
return Game_Data(self.players, self.clients)
reactor.listenTCP(6000, BlackFactory())
今クライアントのために:
import pygame
from twisted.internet.protocol import Protocol, ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet.task import LoopingCall
from twisted.internet import reactor
class BlackClientProtocol(LineReceiver):
def __init__(self, recv, host):
self.recv = recv
self.host = host
def lineReceived(self, line):
self.recv(line)
print line ## prints out as expected ##
def connectionMade(self):
self.host = self
print self.host ## prints out as expected ##
class BlackClient(ClientFactory):
def __init__(self, recv, host):
self.recv = recv
self.host = host
def buildProtocol(self, addr):
return BlackClientProtocol(self.recv, self.host)
class Client(object):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 600))
pygame.display.flip()
reactor.callLater(0.1, self.tick)
def new_line(self, line):
self.line = line
## this trial_func was to see if I could pass the BlackClient another argument ##
## to return an instance of the protocol to be called later in this class ##
def trial_func(self, host):
self.host = host
def tick(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
print self.line ## prints out as expected##
print self.host ## does not print out ???##
if __name__ == '__main__':
c = Client()
lc = LoopingCall(c.tick)
lc.start(0.1)
reactor.connectTCP('192.168.1.2', 6000, BlackClient(c.new_line, c.trial))
reactor.run()
編集:これは何が起こっているかのより明確な例です。コメントはハングアップです。pygamesがインストールされている限り、これは実行されます。pygameイベントKEYDOWN
は、クライアントがデータを送信するためのトリガーとしての単純な(エスケープキー)イベントです。