2012-09-12 14 views
6

मैं pygame-ग्राहकों के साथ एक मुड़-सर्वर को चलाने के लिए कोशिश कर रहा हूँ:ट्विस्ट क्लाइंट pygame mainloop के भीतर?

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() 

मैं बाहर के साथ ग्राहक कोड के साथ इस सर्वर चल रहा हूँ:

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... 

और यहाँ सर्वर है रिएक्टर। कॉलरेटर() विधि और पायग्स कोड और क्लाइंट ठीक से जुड़ता है। क्या मैं रिएक्टर विधि गलत का उपयोग कर रहा हूं या क्या pygames कोड के साथ कुछ संरचनात्मक रूप से गलत है? किसी भी सहायता की सराहना की जाएगी।

तो मुझे नहीं पता कि pygames बिट के अंदर लूप कभी रिएक्टर को फिर से कॉल करने के लिए टूट जाता है या नहीं?

+0

क्या कुछ काम नहीं कर रहा है? आपकी समस्या कहां है? – sloth

+0

मैं अधिक से अधिक व्याख्या करने के लिए संपादित कर दूंगा। – tijko

उत्तर

8

आपको मोड़ का उपयोग करते समय अपना मुख्य लूप (while के साथ) लिखना चाहिए। मुड़ने के लिए मुख्य लूप को नियंत्रित करना होता है, और पायगैम इसके लिए परवाह करने के लिए पर्याप्त लचीला नहीं है (इसे अपने पाश की आवश्यकता नहीं है)।

आप सब कुछ है जो एक समारोह में अपने मुख्य पाश अंदर है रखना चाहिए, और मुड़ रिएक्टर के साथ यह shedule 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) 

इस तरह फोन करके, आप रिएक्टर रन सुनिश्चित करने और नेटवर्क की घटनाओं को संभाल सकता है।


यहाँ एक ग्राहक का एक छोटा सा काम कर उदाहरण है कि अभी पिछले लाइन प्राप्त प्रस्तुत करना होगा:

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() 

यहाँ LoopingCall का उपयोग कर एक सरल उदाहरण है, के रूप में ग्लिफ़ सुझाव दिया (मैं बाहर protocoll छोड़ दिया/कारखाने के वर्गों के रूप में वे उपरोक्त के समान हैं):

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

क्या मुझे टिक और मुख्य के बाद 'reactor.run()' कॉल करना चाहिए? मैंने सोचा कि मुझे इसे शुरू करने की ज़रूरत है या क्या यह मुख्य को रोक देगा और कभी चलने से टिकेगा? क्योंकि इसके साथ ही पायगम स्क्रीन शुरू होती है और उसके साथ पिगगम स्क्रीन बिल्कुल नहीं आती है (इसलिए मुख्य को नहीं कहा जा रहा है) – tijko

+1

बस एक बार 'मुख्य()' कॉल करें (या जो भी कोड आप प्रारंभ करने के लिए कहते हैं आपका ग्राहक)। महत्वपूर्ण हिस्सा यह है कि 'reactor.run() 'को कॉल करने से पहले रिएक्टर.callLater (0.1, टिक)' को एक बार बुलाया जाता है। – sloth

+0

मुझे अभी भी 'मुख्य()' कॉल के बाद 'reoror.ConnectTcp ('192.168.1.2', 6000, ChatClient()) और' reactor.run' चलाने की आवश्यकता है? मैं आगे बढ़ गया और कोशिश की लेकिन अब, pygames घटनाओं का जवाब नहीं दे रहे हैं? – tijko

संबंधित मुद्दे