I'm having some trouble understanding how colliderect works with sprites. I have a good idea about it, but whenever I try to implement it into my game, I just get the error message "attributeError:'pygame.surface' object has no attribute 'rect'"
ufo_lvl_1 = pygame.image.load("ufo1.png")
bullet = pygame.image.load("bullet.png")
class Bullet(pygame.sprite.Sprite):
def __init__(self):
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
self.image = bullet
self.damage = 5
self.rect = self.image.get_rect()
def update(self):
if 1 == 1:
self.rect.x += 15
if self.rect.x >1360:
self.kill()
class ufo1(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = ufo_lvl_1
self.health = 10
self.rect = self.image.get_rect()
def update(self):
if 1==1:
self.rect.x -= 10
if self.rect.colliderect(bullet.rect):
self.health -= bullet.damage
if self.health >= 0:
self.kill()
bullet.kill()
Basically all my sprites work (excluding ufo1), but the moment I create a ufo1 sprite it crashes and I don't know how to fix it.
Thanks in advance.