Reputation: 11
I'm developing a Space Invaders clone using Python 3.5.1 and PyGame. My issue is that I cannot manage to load my sprites onto the screen. I keep receiving the error:
Traceback (most recent call last): File "C:\Users\supmanigga\Documents\Space Invaders\spaceinvaders.py", line 44, in allSprites.draw(screen) File "C:\Users\supmanigga\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py", line 475, in draw self.spritedict[spr] = surface_blit(spr.image, spr.rect) AttributeError: 'Ship' object has no attribute 'image'
My code is as follows:
import pygame
import sys
width = 500
height = 700
white = (255, 255, 255)
black = (0, 0, 0)
score = 0
screen = pygame.display.set_mode([width, height])
class Ship(pygame.sprite.Sprite):
def _init_(self):
sprite.Sprite._init_(self)
self.image = pygame.image.load("player").convert()
self.rect = self.image.get_rect()
class Enemy(pygame.sprite.Sprite):
def _init_(self):
sprite.Sprite._init_(self)
self.image = pygame.image.load("enemy").convert()
self.rect = self.image.get_rect()
class Bullet(pygame.sprite.Sprite):
def _init_(self):
sprite.Sprite._init_(self)
self.image = pygame.image.load("laser").convert()
self.rect = self.image.get_rect()
player = Ship()
allSprites = pygame.sprite.Group()
allSprites.add(player)
running = True
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(black)
allSprites.draw(screen)
pygame.display.flip()
pygame.quit()
Upvotes: 1
Views: 232
Reputation: 101162
def _init_(self):
should be def __init__(self):
Otherwise, the line self.image = pygame.image.load("player").convert()
is never executed, and thus your Ship
instance will have no image
attribute.
Upvotes: 3