self.rect 未找到? [英] self.rect not found?
问题描述
我有一个程序,它只是用来移动图像.我尝试将 self.rect 声明为 load_png() 调用的一部分,但它根本不喜欢它.我认为这会起作用的原因来自 http://www.pygame.org/docs/tut/tom/games6.html,说这应该可行:
I have a program that is simply made to move an image around. I try to state the self.rect as part of a load_png() call, but it simply does not like it. THe reason I think this will work is from http://www.pygame.org/docs/tut/tom/games6.html, saying that this should work:
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 教程,它应该可以工作:
Here is my code, which according to the pygame tutorial from its own website, should work:
def _init_(self):
pygame.sprite.Sprite._init_(self)
self.state = 'still'
self.image = pygame.image.load('goodGuy.png')
self.rect = self.image.get_rect()
screen = pygame.display.getSurface()
它给了我这个错误:
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'
如果你们需要我所有的代码,评论吹,我会编辑它.
If you guys need all of my code, comment blew and I will edit it.
推荐答案
您没有定义 load_png 函数.
You don't have a load_png function defined.
您需要先创建 pygame 图像对象,然后才能访问其 rect 属性.
You need to create the pygame image object before you can access its rect property.
self.image = pygame.image.load(file)
然后您可以使用
self.rect = self.image.get_rect()
或者您可以根据您链接的示例创建 load_png 函数.
Or you could create the load_png function as per the example you linked.
这篇关于self.rect 未找到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!