Reputation: 309
Using a book to learn how to make a simple pygame Alien Invasion.I got to the part,where alien fleet needs to be drawn,but when I am trying to draw the whole fleet on x-level with asking for a Group object,it gives me the error(Full Traceback):
Traceback (most recent call last):
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'
I dont really think that error is in that line of code,thats why I checked the whole code up,but even after sitting with it for more than an hour,I couldn`t find any errors,that would lead to such error.Attributes are distrabuted correctly,everything else is working in order,except this line. Hope you to help.Providing as much I think is needed to understand the code and which can be related to the error.
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)
...
If needed, 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)
Everything else should be in order,because I didn`t change anything there from the last time when I created 1 alien. I expect to see just a fleet of alien at the top x-line for now,but it's just now working and give an error.
I am new to stack overflow,that's why there may be some inconviniences in the post.Sorry for that.Tried to do everything at my best.Already sitting there for an hour :O
Upvotes: 1
Views: 700
Reputation: 1186
The problem comes from your Alien
class:
self.alien_image = pygame.image.load('images/alien.bmp')
The draw()
method expects you to have set a image
attribute, but you named it alien_image
instead. Try replacing this line by:
self.image = pygame.image.load('images/alien.bmp')
EDIT :
You also have a similar problem with the rectangle
attribute: you named it rectangle_alien
. Replace all your self.rectangle_alien
by self.rectangle
in your Alien
class.
Upvotes: 1