2011-08-12 7 views
1

This questionは、このエラーが発生している理由を既に示しています。この問題を解決する方法を知りたいのですが。ここで 属性エラーがひねられている場合

は後に呼び出されるか game.spinを得るために、私は少しも見当がつかないnetworking.py

from twisted.internet import reactor 
from twisted.internet.protocol import ClientFactory 
from twisted.protocols import amp 
import sys 

from ampcommands import TransferSprite 

class GameClient(amp.AMP): 
    def download_sprite(self, sprite): 
     self.callRemote(TransferSprite, sprite) 

class GameClientFactory(ClientFactory): 
    protocol = GameClient 

    def buildProtocol(self, address): 
     proto = ClientFactory.buildProtocol(self, address) 
     self.connectedProtocol = proto 
     return proto 

def construct_factory(): 
    return GameClientFactory() 

def run(game, factory, verbose=True): 

    if verbose: 
     from twisted.python import log 
     log.startLogging(sys.stdout) 

    reactor.connectTCP("localhost", 1234, factory) 
    reactor.callWhenRunning(game.spin) 
    reactor.run() 

のコードmain.pyここ

from twisted.internet import reactor 
import pygame 

from networking import run, construct_factory 

class GameEngine(object): 
    def __init__(self, client): 
     pygame.init() 
     self.screen = pygame.display.set_mode((640, 400)) 
     self.FPS = 60 
     self.client = client.connectedProtocol 
     reactor.callWhenRunning(self.grab_all_sprites) 

    def grab_all_sprites(self): 
     with open('sprites.txt') as sprites: 
      for sprite in sprites: 
       sprite_file = self.client.download_sprite(sprite) 
       with open(r'resources\%s.png' % sprite, 'wb') as out: 
        out.write(sprite_file) 


    def spin(self): 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       reactor.stop() 

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_SPACE: 
        print "spacebar!" 

     #update the player 
     pygame.display.flip() 
     reactor.callLater((1/self.FPS), self.spin) 

if __name__ in '__main__': 
    client = construct_factory()  
    game = GameEngine(client) 
    run(game, client) 

ためのコードですGameClientFactory.connectedProtocolに接続してください。私は混乱して、疲れて誰でも良い方法を見つけることができますか?

答えて

2

これはあなたの質問があなたの答えである場合のようです。それはちょうどそれを渡すのではなく、それを別の属性を作成する方が簡単なので、あまりにも、プロトコルを受け入れるように

def buildProtocol(self, address): 
    proto = ClientFactory.buildProtocol(self, address) 
    GameEngine(proto).spin() 
    return proto 

変更GameEngine.__init__:既存のGameEngineインスタンス化コードを削除し、このようなbuildProtocolを持っているあなたのGameClientFactoryを変更そのオブジェクトを渡します。

+0

常に驚くべき答えです。 –

関連する問題