I have a game where the player has to dodge asteroids coming towards it, and I would like to incorporate a life system. Unfortunately, when I tried this, it still killed my player immediately. I printed out the player's lives as a test, and it is going from 3 to -7 whenever an asteroid hits it. How would I make it so that only one life is subtracted?
Here is my code:
import pygame, random
from pygame.locals import (
RLEACCEL,
K_UP,
K_DOWN,
K_SPACE,
K_ESCAPE,
KEYDOWN,
QUIT,
)
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.image.load("ship.png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect()
self.lives = 3
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Asteroid(pygame.sprite.Sprite):
def __init__(self):
super(Asteroid, self).__init__()
self.surf = pygame.image.load("asteroid.png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(5,20)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
self.kill()
def remove(self):
self.kill()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
player = Player()
enemies = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
elif event.type == ADDENEMY:
new_Asteroid = Asteroid()
enemies.add(new_Asteroid)
all_sprites.add(new_Asteroid)
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
enemies.update()
screen.fill((0,0,0))
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
if pygame.sprite.spritecollideany(player, enemies):
player.lives -= 1
print(player.lives)
if player.lives == 0:
player.kill()
running = False
pygame.display.flip()
clock.tick(30)
UPDATE: After trying out the first answer, here is the code for colliding:
if pygame.sprite.spritecollideany(player, enemies): # Check if collided
if not new_Asteroid.collided_with_player: # Check if the asteroid has already collided with the Player
# Substract a life, set the asteroids collided player to True so it does not substract more than one, and play some sfx
player.lives -= 1
new_Asteroid.collided_with_player = True
move_sound.stop()
hit_sound.play()
# If there are no lives left
if player.lives == 0:
heart1.kill()
player.kill()
running = False
(In the asteroid class I have the collided_with_player set to False by default)