So i am quite new at pygame. im creating a game about shooting the alien using a spaceship. So the spaceship will keep on shooting bullets and we have to move the spaceship around so the bullet will hit the alien and the alien will die after getting hit 5 times. The alien will keep on coming down and move left and right to make the game harder. So i tried to kill the alien by using the self.kill() but after it dissapear, it reappear again at the same spot. Why is this happening and how do i fix it?
import pygame
shoot_timer = pygame.USEREVENT + 1
pygame.time.set_timer(shoot_timer,400)
enemy_timer = pygame.USEREVENT + 2
pygame.time.set_timer(enemy_timer,4000)
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
alien = pygame.transform.rotozoom(pygame.image.load(os.path.join('Assets','enemy.png')).convert_alpha(), 1, 0.8)
self.image = alien
self.rect = self.image.get_rect(bottomleft=(randint(0, width - self.image.get_width()), randint(-200, -150)))
self.lives = 5
self.movement_index = 0
self.movement_place = [-2,2]
self.movement_a = self.movement_place[self.movement_index]
def movement(self):
self.rect.y += 1
self.movement_index += 0.005
if self.movement_index > len(self.movement_place):
self.movement_index = 0
self.movement_a = self.movement_place[int(self.movement_index)]
self.rect.x += self.movement_a
def destroy(self):
if self.lives <= 0:
self.kill()
def update(self):
self.movement()
self.destroy()
class Spaceship(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
red_spaceship = pygame.image.load(os.path.join('Assets', 'spaceship_red.png')).convert_alpha()
red_spaceship = pygame.transform.rotozoom(red_spaceship,180, 0.15)
self.image = red_spaceship
self.rect = self.image.get_rect(midbottom=(width//2,height-20))
self.bullets = []
def shoot(self):
bullet_rect = pygame.Rect(self.rect.x + self.image.get_width()//2 -1, self.rect.y - 5, 3, 15)
self.bullets.append(bullet_rect)
def player_input(self):
key = pygame.key.get_pressed()
speed = 5
if key[pygame.K_w] and self.rect.top > 60:
self.rect.y -= speed
if key[pygame.K_s] and self.rect.bottom < height:
self.rect.y += speed
if key[pygame.K_a] and self.rect.left > 0:
self.rect.x -= speed
if key[pygame.K_d] and self.rect.right < width:
self.rect.x += speed
def update(self, a):
if a == '':
self.player_input()
for bullet in self.bullets:
bullet.y -= 10
pygame.draw.rect(screen, yellow, bullet)
if bullet.bottom < -10:
self.bullets.remove(bullet)
if enemy.rect.colliderect(bullet):
self.bullets.remove(bullet)
enemy.lives -= 1
else:
self.shoot()
enemy = Enemy()
enemy_group = pygame.sprite.GroupSingle()
spaceship = Spaceship()
spaceship_group = pygame.sprite.GroupSingle()
spaceship_group.add(spaceship)
def draw_surface():
spaceship_group.draw(screen)
spaceship_group.update('')
enemy_group.draw(screen)
enemy_group.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == shoot_timer:
spaceship_group.update('a')
if event.type == enemy_timer:
enemy_group.add(enemy)
draw_surface()
pygame.display.update()
clock.tick(60)