2014-12-18 3 views
-1

작은 클라이언트가있는 작은 게임이 있는데이 게임은 서버에서 데이터를 수신합니다. 클라이언트는 모듈에 있고 게임은 다른 모듈에 있습니다. 문제는 클라이언트가 서버에서 데이터를 수신하지만 게임에서 작업을 수행하기 위해 클라이언트가 게임 (클래스 MyGame)에 액세스해야하며 다음과 같은 코드가 표시됩니다.게임 클라이언트 상호 작용

모듈 클라이언트 :

class MyClient: 
    #receives data from the server 
    #It is a task, always listening 
    def readerConnection: 
     #if a data arrives from the server... 
     if newData: 
      #call dataOnScreen of class MyGame 
      ...dataOnScreen() 

모듈 게임 :있어서 dataOnScreen이 클래스하여 MyClient에서 정의되지 않기 때문에 당연히

from client import MyClient 

class MyGame: 
    def _init_(self, client): 
     #begin to receive data from the server 
     self.c = client 

    #print data received from the server on screen 
    def dataOnScreen(self): 
     ............ 
#the game begins   
MyGame(MyClient()) 

오류가 발생한다. 다음과 같은 처리를 할 경우 는 상황이 (경기 시작 후 클라이언트를 쓰기) 잘 작동 :

class MyGame: 
    def _init_(self): 
     ......... 
    #receives data from the server 
    #It is a task, always listening 
    def readerConnection: 
     #if a data arrives from the server... 
     if newData: 
      #call dataOnScreen 
      self.dataOnScreen() 

    #print data received from the server on screen 
    def dataOnScreen(self): 
     ............ 
#the game begins   
MyGame() 

을하지만 내가 원하는 것이 아니다. 내가 원하는 것은 게임과 클라이언트를 다른 클래스에 두는 것입니다.

귀하의 도움에 감사드립니다.

+0

클라이언트와 서버가 네트워크를 통해 통신하고 싶다고 가정합니다. 이 경우 단순히 하나의 함수를 다른 함수에서 호출 할 수는 없으며, 어떤 종류의 네트워크 프로토콜을 사용하여 하나에서 다른쪽으로 통신해야합니다. –

+0

나는 그들이 서버/클라이언트 통신에 대해서는 문제가 없다고 생각한다. 실제 게임과 클라이언트 클래스의 연계/분리 문제이다 (나는 생각한다). –

답변

0

아마도 함수 나 MyGame 객체를 클라이언트에 전달하는 등의 작업을 수행하여 데이터를받을 때 수행 할 작업을 알 수 있습니다. 당신이 함수에 전달 된 경우 클라이언트에 대한 컨텍스트 또는 콜백 등의

class MyGame: 
    def _init_(self, client): 
     #begin to receive data from the server 
     self.c = client 
     client.game = self 


class MyClient: 
    #receives data from the server 
    #It is a task, always listening 
    def readerConnection: 
     #if a data arrives from the server... 
     if newData: 
      #call dataOnScreen of class MyGame 
      self.game.dataOnScreen() 

종류와 같은

뭔가.

관련 문제