2011-08-12 103 views
1

This question已經給了我原因爲什麼這個錯誤正在發生現在我想知道如何解決這個問題。扭曲的屬性錯誤

這裏是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) 

這裏的代碼是爲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() 

代碼我絲毫不知道怎麼去game.spin被調用後,連接使GameClientFactory.connectedProtocol。我感到困惑和疲憊,任何人都可以找到更好的方法?

回答

2

這似乎是你的問題是你的答案的情況。刪除現有GameEngine實例代碼,改變你的GameClientFactorybuildProtocol這樣的:

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

變化GameEngine.__init__只是接受協議,也因爲它更容易只是通過它,而不是讓它的另一個屬性對象,然後傳入其他對象。

+0

一如既往的一個驚人的答案。 –