【问题标题】:AttributeError: 'Sprite' object has no attribute 'image'AttributeError: 'Sprite' 对象没有属性 'image'
【发布时间】:2013-01-21 23:56:08
【问题描述】:

我通过使用定义我的精灵

user = pygame.sprite.Sprite()

然后我尝试这样做:

user.image == pygame.image.load(userstationary).convert()

但我得到标题中的错误

感谢任何帮助

【问题讨论】:

  • 你到底想做什么?您收到错误是因为 Sprite 对象没有 image 属性...
  • 发布完整的回溯和完整的源代码以重现错误。

标签: python image pygame sprite


【解决方案1】:

我认为这个问题是由于对 pygame 中 Sprite 类的用途的误解。它不打算直接使用,而应该是一个基类,您可以使用它来定义自己的子类。

因此,您可以像这样定义自己的最小类:

class User(pygame.sprite.Sprite):
    def __init__(self, image_file):
        self.image = pygame.image.load(image_file).convert()
        self.rect = self.image.get_rect()

【讨论】:

    【解决方案2】:

    简单。您使用 == 而不是 =。例如,你需要输入user.image = pygame.image.load(userstationary).convert()而不是user.image == pygame.image.load(userstationary).convert()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-14
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多