扭曲属性错误
这个问题已经给了我为什么会这样现在发生错误,我想知道如何解决这个问题。
这是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来就像是你的问题就是你的答案。删除现有的 GameEngine 实例化代码,并将
GameClientFactory
更改为具有如下所示的buildProtocol
:将
GameEngine.__init__
更改为也只需接受协议,因为将其传递给它比将其作为另一个对象的属性然后传递另一个对象更容易。This seems like a case where your question is your answer. Remove your existing
GameEngine
instantiation code and change yourGameClientFactory
to have abuildProtocol
like this: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.