【问题标题】:AttributeError: 'Ballons' object has no attribute 'image'AttributeError:“气球”对象没有属性“图像”
【发布时间】:2021-10-23 14:12:18
【问题描述】:

我找不到错误。尝试了几个小时。

当我运行代码时,我收到消息:

Ballons' 对象没有属性 'image'

留言详情:

Traceback(最近一次调用最后一次): 文件“c:\pythonprogramm\ballon_jagd copy.py”,第 47 行,在 ballon_sprites.draw(斯皮尔菲尔德) 文件“C:\Users\chef\AppData\Local\Programs\Python\Python39\lib\site-packages\pygame\sprite.py”,第 546 行,绘制中 surface.blits((spr.image, spr.rect) for spr in sprites) 文件“C:\Users\chef\AppData\Local\Programs\Python\Python39\lib\site-packages\pygame\sprite.py”,第 546 行,在 surface.blits((spr.image, spr.rect) for spr in sprites) AttributeError: 'Ballons' 对象没有属性 'image'

代码如下:

 class Ballons(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)     

        z_bild = pygame.image.load("Bilder/ballons/ballon1.png")       
        self.einzel_bild = z_bild
        self.rect = self.einzel_bild.get_rect()
        self.rect_x = random.randint(100,1800)
        self.rect_y = 700 
        
    def update(self):        
        self.rect_y -=5

def draw_bg():
    spielfeld.fill(bg)

#Allgemein..............................................
pygame.init()
clock = pygame.time.Clock()
bg = (50,250,50)

#Spielfeld.............................................. 
bild_breite = 1920
bild_hoehe = 1080
spielfeld = pygame.display.set_mode((bild_breite,bild_hoehe))

#Erstellung Sprite und Group

ballon_sprites = pygame.sprite.Group()
ballon = Ballons()
ballon_sprites.add(ballon)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
   
    #Zeichnen        
    draw_bg()    
    ballon_sprites.draw(spielfeld) 
    ballon_sprites.update()
    pygame.display.flip()
    clock.tick(60)nter code here
    

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    pygame.sprite.Group.draw() 使用包含的pygame.sprite.Sprites 的imagerect 属性来绘制对象。见pygame.sprite.Group.draw():

    将包含的 Sprite 绘制到 Surface 参数。这将Sprite.image 属性用于源表面,并使用Sprite.rect。 [...]

    因此Ballon 必须有一个名为image 的属性:

    class Ballons(pygame.sprite.Sprite):
        def __init__(self):
            pygame.sprite.Sprite.__init__(self)     
    
            self.image = pygame.image.load("Bilder/ballons/ballon1.png")       
            
            self.rect = self.image.get_rect()
            self.rect_x = random.randint(100,1800)
            self.rect_y = 700 
            
        def update(self):        
            self.rect_y -=5
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2021-04-19
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多