AttributeError:'Alien'对象没有属性'image'(我正在调用Group对象)

时间:2019-06-26 13:13:19

标签: python pygame

使用一本书来学习如何制作一个简单的pygame外星人入侵。我已经到了需要绘制外星人舰队的部分,但是当我试图通过请求一个小组在x级别上绘制整个舰队时对象,它给了我错误(完整回溯):

回溯(最近通话最近一次):

File "alien_invasion.py", line 35, in <module>

   run_game()

File "alien_invasion.py", line 34, in run_game

gf.update_screen(our_settings,screen,ship,bullets,aliens)

File "C:\Users\root\Desktop\python_work\alien_invasion\game_functions.py", line 53, in update_screen

aliens.draw(screen) #Make the drawn screen visible

File "C:\Users\root\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\pygame\sprite.py", line 476, in draw

self.spritedict[spr] = surface_blit(spr.image, spr.rect)

AttributeError: 'Alien' object has no attribute 'image'

我真的不认为该代码行中存在错误,这就是为什么我要检查整个代码,但是即使坐了一个多小时,我也找不到任何错误,这会导致这样的错误错误。属性已正确分配,除此行外,其他所有命令均正常工作。 希望您能提供帮助。我认为需要提供尽可能多的信息才能理解代码,并且可能与错误有关。

import pygame
from pygame.sprite import Group
...
import game_functions as gf
from alien import Alien

def run_game():
...
    pygame.init()
    our_settings = Settings()
    screen = pygame.display.set_mode((our_settings.screen_width , 
    our_settings.screen_high))
    pygame.display.set_caption('Alien Invasion')

    ship = Ship(our_settings,screen)
    bullets = Group()
    aliens = Group()

    gf.create_fleet(our_settings,screen,aliens)
    while True:
        gf.ckeck_events(our_settings,screen,ship,bullets)
        ship.cotinuos_update() #updates ship`s position through each passing
        gf.update_bullets(bullets)
        gf.update_screen(our_settings,screen,ship,bullets,aliens)
run_game()
...

game_functions.py:

...
import pygame
from alien import Alien
...
def update_screen(our_settings,screen,ship,bullets,aliens):
    '''Updates images at the display.'''
    #Redraw the screen each time
    #Value of the background(red,green,blue)
    screen.fill(our_settings.screen_color)
    #Redraw all bullets before the ship and aliens
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    #Crate ship on the background
    ship.draw()
    aliens.draw(screen)                #ERROR HERE!!!
    #Make the drawn screen visible
    pygame.display.flip()
...
def create_fleet(our_settings,screen,aliens):
    '''Create fleet of aliens'''
    alien = Alien(our_settings,screen)
    alien_width = alien.rectangle_alien.width
    x_we_can_use = our_settings.screen_width - (2 * alien_width)
    #at the left and at the right we have empy space = 2 aliens width
    alien_in_row = int(x_we_can_use / (2 * alien_width))
    #each alien has empty space with the size of 1 alien
    for number_of_the_alien in range(alien_in_row):
        alien = Alien(our_settings,screen)
        alien.x = alien_width + 2*alien_width * number_of_the_alien
        alien.rectangle_alien.x = alien.x
        aliens.add(alien)
...

如果需要, Alien.py:

...
import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    '''Defines the single alien of the fleet'''
    def __init__(self,our_settings,screen):
        '''Initialize alien and create the starting position'''
        super().__init__()
        self.our_settings = our_settings
        self.screen = screen 

        #Create the image of the alien and make him rect.
        self.alien_image = pygame.image.load('images/alien.bmp')
        self.rectangle_alien = self.alien_image.get_rect()

        #New alien near top left of the screen
        self.rectangle_alien.x = self.rectangle_alien.width
        self.rectangle_alien.y = self.rectangle_alien.height

        #Store exact postiion
        self.x = float(self.rectangle_alien.x)

     def create_alien(self):
        '''Draw the alien at the current location'''
        self.screen.blit(self.alien_image, self.rectangle_alien)

其他所有事情都应该井井有条,因为自上次创建1个外星人以来,我什么都没做。 我希望目前在x线的顶部仅看到一群外星人,但它现在正在工作并给出错误。

我是堆栈溢出的新手,这就是为什么帖子中可能会有一些不便之处。很抱歉。尝试尽我所能。已经坐在那里一个小时了:O

1 个答案:

答案 0 :(得分:1)

问题来自您的Alien类:

self.alien_image = pygame.image.load('images/alien.bmp')

draw()方法希望您设置了image属性,但您却将其命名为alien_image。尝试通过以下方式替换此行:

self.image = pygame.image.load('images/alien.bmp')

编辑:

您对rectangle属性也有类似的问题:您将其命名为rectangle_alien。将self.rectangle_alien类中的所有self.rectangle替换为Alien