扭曲的属性错误

时间:2011-08-12 22:06:46

标签: python twisted pygame

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。我变得困惑和疲惫,任何人都能发现更好的方式吗?

1 个答案:

答案 0 :(得分:2)

这似乎是您的问题是您的答案的情况。删除现有的GameEngine实例代码,并将GameClientFactory更改为buildProtocol,如下所示:

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

GameEngine.__init__更改为仅接受协议,因为更容易传递它而不是将其作为另一个对象的属性然后传入其他对象。