我正在尝试使用 pygame-clients 运行一个扭曲的服务器:
class ChatClientProtocol(LineReceiver):
def lineReceived(self,line):
print (line)
class ChatClient(ClientFactory):
def __init__(self):
self.protocol = ChatClientProtocol
def main():
flag = 0
default_screen()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
pos = pygame.mouse.get_pos()
# some rect.collidepoint(pos) rest of loop...
这是服务器:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users, players):
self.users = users
self.name = None
self.players = players
def connectionMade(self):
new = 'player_' + str(len(self.players) + 1)
self.players.append(new)
self.sendLine(str(self.players,))
class ChatFactory(Factory):
def __init__(self):
self.users = {} #maps instances to clients
self.players = []
def buildProtocol(self, addr):
return Chat(self.users,self.players)
reactor.listenTCP(6000, ChatFactory())
reactor.run()
我正在使用没有 reactor.CallLater() 方法和 pygames 代码的客户端代码运行此服务器,并且客户端连接良好。我使用 reactor 方法是错误的还是 pygames 代码在结构上存在问题?任何帮助,将不胜感激。
所以我不知道 pygames 位中的循环是否会再次中断以再次调用反应器?