自校正找不到?

2024-05-15 17:55:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,它只是用来移动图像。我试着陈述自校正作为load\u png()调用的一部分,但它并不喜欢它。我认为这会起作用的原因是来自http://www.pygame.org/docs/tut/tom/games6.html,他说这应该会起作用:

def __init__(self, side):
            pygame.sprite.Sprite.__init__(self)
            self.image, self.rect = load_png('bat.png')
            screen = pygame.display.get_surface()
            self.area = screen.get_rect()
            self.side = side
            self.speed = 10
            self.state = "still"
            self.reinit()

以下是我的代码,根据pygame网站上的教程,应该可以使用:

^{pr2}$

它给了我一个错误:

Traceback (most recent call last):
File "C:\Python25\RPG.py", line 37, in <module>
screen.blit(screen, Guy.rect, Guy.rect)
AttributeError: 'goodGuy' object has no attribute 'rect'

如果你们需要我所有的代码,评论吹了,我会编辑它。在


Tags: 代码rect图像self程序getpnginit
2条回答

python或pygame没有内置的load_png函数。我想您所指的教程在某个地方手动定义了它。{{a1}你可以调用^返回的对象。完整代码如下:

self.image = pygame.image.load('bat.png')
self.rect = self.image.get_rect()

第二个问题是定义了函数_init_,但需要使用双下划线:__init__。在

此外,您还需要在实际发生错误的地方发布代码。在

你没有定义加载函数。在

在访问pygame的rect属性之前,需要创建pygame image对象。在

self.image = pygame.image.load(file)

然后可以使用

^{pr2}$

或者,您可以根据链接的示例创建load_png函数。在

相关问题 更多 >