class Enemy(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, health, bullet):
super().__init__()
print("Created a new sprite:", id(self))
self.width = width
self.height = height
self.image = pygame.Surface([self.width, self.height])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.change_x = 0
self.change_y = 0
self.bullet = bullet
self.health = health
self.grid = None
self.alive = True
def update(self):
self.rect.x += self.change_x
if self.rect.x >= SCREEN_WIDTH - self.width:
self.change_x *= -1
elif self.rect.x <= 0:
self.change_x *= -1
self.rect.y += self.change_y
if self.rect.y >= SCREEN_HEIGHT:
self.change_y *= -1
elif self.rect.y <= 0:
self.change_y *= -1
bullet_hit_list = pygame.sprite.spritecollide(self, self.bullet, True)
for bullet in bullet_hit_list:
self.health -= 1
if self.health <= 0:
self.alive = False
else:
self.alive = True
----Main Program----
----Creates the enemies----
width = 20
height = 20
margin = 5
for row in range(3):
for column in range(10):
baddie = Enemy((margin + width) * column + margin, (margin + height) * row + margin,
width, height, 3, bullet_list)
baddie_sprite_list.add(baddie)
----Inside while loop----
----Draws the enemies----
if baddie.alive == True:
baddie_sprite_list.draw(screen)
I am having an issue and was wondering if I could get some help. If more info is necessary please let me know.
I am trying to make a basic space shooter game using only primitive shapes for the time being.
Everything works fine until the player shoots an enemy sprite. Unless it is the last sprite rendered nothing happens. When the last sprite is shot 3 times(which is the amount of health each instance of enemy should have -1 for each bullet) all the enemy sprites disappear, however if you shoot at where they were the bullets still disappear like they hit an object.
Thank you for any help in advance.
Added full game below if needed.