【发布时间】:2018-08-14 08:28:53
【问题描述】:
我已输入代码self.rect = pygame.Rect(x, y, 100, 100)。有一个AttributeError: type object 'MobRow1' has no attribute 'rect'。为什么我把rect放进去后,这个错误一直弹出来。
我试过self.rect = pygame.Rect(self.x, self.y, 100, 100) 还是不行。
class MobRow1(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(badguy_file).convert_alpha()
self.image = pygame.transform.scale(self.image, (50, 50))
self.x = x
self.y = y
self.rect = pygame.Rect(x, y, 100, 100)
self.mask = pygame.mask.from_surface(self.image)
def update(self):
self.rect.y += self.speedy
self.rect.x += self.speedx
【问题讨论】:
-
请创建一个MCVE 并将完整的错误回溯添加到您的问题中。
-
这里肯定少了一段代码。您的函数
update是否在脚本中的任何位置调用? -
@ValBerthe 它没有在任何地方调用。你想让我给你看一下所有的代码吗?
-
@MaxCOSSETTO 请先添加错误回溯
-
@ValBerthe Traceback(最近一次调用最后):文件“C:\Users\maxco\AppData\Local\Programs\Python\Python36\codingandcaring github\Basketball 2.2.3.py”,第 143 行,在
MobRow1.update(screen_rect) 文件“C:\Users\maxco\AppData\Local\Programs\Python\Python36\codingandcaring github\Basketball 2.2.3.py”中,第 140 行,在更新 self.rect 中。 y += self.speedy AttributeError: 'pygame.Rect' 对象没有属性 'rect'
标签: python attributes pygame sprite