扭曲属性错误

发布于 2024-11-29 06:16:47 字数 2337 浏览 0 评论 0原文

这个问题已经给了我为什么会这样现在发生错误,我想知道如何解决这个问题。

这是 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。我感到困惑和疲倦,有人能找到更好的方法吗?

This question has already given me the reason WHY this error is happening now I want to know how to solve this issue.

Here is the code for 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)

here is the code for 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()

I have no idea in the slightest how to get game.spin to be called AFTER the connection is made so that GameClientFactory.connectedProtocol. I'm getting confused and tired can anyone spot a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2024-12-06 06:16:47

这看起来就像是你的问题就是你的答案。删除现有的 GameEngine 实例化代码,并将 GameClientFactory 更改为具有如下所示的 buildProtocol

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

GameEngine.__init__ 更改为也只需接受协议,因为将其传递给它比将其作为另一个对象的属性然后传递另一个对象更容易。

This seems like a case where your question is your answer. Remove your existing GameEngine instantiation code and change your GameClientFactory to have a buildProtocol like this:

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

Change GameEngine.__init__ to just accept the protocol, too, since it's easier to just pass it in rather than make it an attribute of another object and then pass in that other object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文