2012-09-12 7 views
6

pygameメインループ内のツイストクライアント?私はpygameの-クライアントとツイスト・サーバを実行しようとしている

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コードとクライアントが正常に接続します。私は原子炉の方法を間違って使用しているか、構造的にpygamesのコードに何か問題がありますか?どんな助けもありがとう。

このように、パイグムビット内のループが再び反応炉を呼び出すために壊れているかどうかわかりません。

+0

何か問題はありますか?あなたの問題はどこですか? – sloth

+0

もっと詳しく説明します。 – tijko

答えて

8

あなたはでなければなりません。ツイストを使用すると、独自のメインループ(while)を書きます。ツイストはメインループを制御しなければならず、pygameは気にしないほど柔軟である(それは自身のループを必要としない)。

あなたは機能にあなたのメインループ内にあるすべてのものを入れて、この方法でreactor.CallLater()

def main(): 
    flag = 0 
    default_screen() 
    reactor.callLater(0.1, tick) 

def tick(): 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      reactor.stop() # just stop somehow 
     elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
      reactor.stop() # just stop somehow 
     elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: 
      pos = pygame.mouse.get_pos() 
      # some stuff 
    reactor.callLater(0.1, tick) 

を呼び出すことにより、ねじれた原子炉でそれをsheduleする必要があり、あなたは、原子炉の実行を確保し、ネットワークイベントを処理することができます。ここで


は、ちょうど最後の行は受け取っレンダリングするクライアントの小規模作業例です:

from twisted.internet import reactor 
from twisted.internet.protocol import ClientFactory 
from twisted.protocols.basic import LineReceiver 

import pygame 

class ChatClientProtocol(LineReceiver): 

    def __init__(self, recv): 
     self.recv = recv 

    def lineReceived(self,line): 
     self.recv(line) 

class ChatClient(ClientFactory): 
    def __init__(self, recv): 
     self.protocol = ChatClientProtocol 
     self.recv = recv 

    def buildProtocol(self, addr): 
     return ChatClientProtocol(self.recv) 

class Client(object): 

    def __init__(self): 
     self.line = 'no message' 
     pygame.init() 
     self.screen = pygame.display.set_mode((200, 200)) 
     reactor.callLater(0.1, self.tick) 

    def new_line(self, line): 
     self.line = line 

    def tick(self): 
     self.screen.fill((0,0,0)) 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       reactor.stop() # just stop somehow 
      elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
       reactor.stop() # just stop somehow 
     self.screen.blit(pygame.font.SysFont('mono', 12, bold=True).render(self.line, True, (0, 255, 0)), (20,20)) 
     pygame.display.flip() 
     reactor.callLater(0.1, self.tick) 

if __name__ == '__main__': 
    c = Client() 
    reactor.connectTCP('127.0.0.1',6000, ChatClient(c.new_line))  
    reactor.run() 

グリフは、(私はのprotocollを残し示唆されているようにここでは、LoopingCallを使った簡単な例を示します/工場クラスは上記と同じです):

from twisted.internet.task import LoopingCall 

class Client(object): 

    def __init__(self): 
     self.line = 'no message' 
     pygame.init() 
     self.screen = pygame.display.set_mode((200, 200)) 

    def new_line(self, line): 
     self.line = line 

    def tick(self): 
     self.screen.fill((0,0,0)) 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       reactor.stop() # just stop somehow 
      elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: 
       reactor.stop() # just stop somehow 
     self.screen.blit(pygame.font.SysFont('mono', 12, bold=True).render(self.line, True, (0, 255, 0)), (20,20)) 
     pygame.display.flip() 

if __name__ == '__main__': 
    c = Client() 

    lc = LoopingCall(c.tick) 
    lc.start(0.1) 
    reactor.connectTCP('127.0.0.1',6000, ChatClient(c.new_line))  
    reactor.run() 
+0

私はtickとmainの後に 'reactor.run()'を呼びますか?私はそれを始める必要があると思った、またはこれはメインとティックが今まで走っているのを防ぐでしょうか? pygame画面が出て終了するので、pygame画面が全く出ないので(メインが呼び出されていないので) – tijko

+1

'main()'を一度呼び出すか(または、あなたが初期化するために呼び出すコードあなたのクライアント)。重要な部分は 'reactor.callLater(0.1、tick)'が 'reactor.run()'を呼び出す前に一度呼び出されることです。 – sloth

+0

'main()'の後に 'reactor.ConnectTcp( '192.168.1.2'、6000、ChatClient())'と 'reactor.run'を実行する必要がありますか?私は先に行ってこれを試しましたが、今pygamesのイベントは応答していませんか? – tijko

関連する問題