gpt4 book ai didi

python - 扭曲属性错误

转载 作者:行者123 更新时间:2023-12-01 06:08:17 25 4
gpt4 key购买 nike

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

最佳答案

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

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

GameEngine.__init__ 更改为仅接受协议(protocol),因为将其传入而不是使其成为另一个对象的属性,然后传入另一个对象更容易。

关于python - 扭曲属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7046842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com