Reputation: 911
I am making a spaceship game where you control a spaceship and fire bullets at the enemies. I am now trying to make both the player and the enemy spaceship disappear when both the player and the enemy collide. But when I try to use pygame.sprite.spritecollide, only the enemy disappears. Why is this? Is this because spritecollide only removes the sprites from its groups? (I set dokill to True)
This is my current code:
import pygame
from pygame.locals import *
from random import randint
from tools import *
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800, 500))
class Spaceship(pygame.sprite.Sprite):
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x, self.y = x, y
self.image = pygame.image.load("spaceship.png")
self.image = pygame.transform.scale(self.image, (175, 175))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
self.rect.center = (self.x, self.y)
class Bullet(pygame.sprite.Sprite):
def __init__(self, s, x, y):
pygame.sprite.Sprite.__init__(self)
self.screen = s
self.x, self.y = x, y
self.image = pygame.image.load("bullet.png")
self.image = pygame.transform.scale(self.image, (100, 100))
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
self.y -= 5
self.rect.center = (self.x, self.y)
if self.y < 0:
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self, s, x, y, t):
pygame.sprite.Sprite.__init__(self)
self.type = t
self.screen, self.x, self.y = s, x, y
self.image = pygame.image.load("enemy.png")
self.image = pygame.transform.scale(self.image, (235, 215))
self.rect = self.image.get_rect()
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
self.score_given = get_enemy_given_score()[self.type]
def update(self):
if self.y < 0:
self.kill()
self.y += 3
self.rect.center = (self.x, self.y)
spaceship = Spaceship(screen, 400, 400)
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
clock = pygame.time.Clock()
enemy_interval = 5000 # It's in milliseconds
enemy_event = pygame.USEREVENT + 1
pygame.time.set_timer(enemy_event, enemy_interval)
score = 0
font = pygame.font.SysFont("Arial", 30)
textsurface = font.render("Score: {:,}".format(score), True, (0, 0, 0))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
bullet = Bullet(screen, spaceship.x, spaceship.y - 20)
bullets.add(bullet)
if event.type == enemy_event:
enemy = Enemy(screen, randint(-150, 750), 0, "normal")
enemies.add(enemy)
bullets.update()
key = pygame.key.get_pressed()
amount = 5
if key[pygame.K_a]:
spaceship.x -= amount
elif key[pygame.K_d]:
spaceship.x += amount
elif key[pygame.K_w]:
spaceship.y -= amount
elif key[pygame.K_s]:
spaceship.y += amount
spaceship.update()
screen.fill((255, 255, 255))
screen.blit(spaceship.image, spaceship.rect)
bullets.draw(screen)
enemies.draw(screen)
for i in enemies:
i.update()
if pygame.sprite.spritecollide(i, bullets, True):
score += i.score_given
i.kill()
if score >= 99999:
score = 99999
textsurface = font.render("Score: {:,}".format(score), True, (0, 0, 0))
screen.blit(textsurface, (590, 0))
pygame.sprite.spritecollide(spaceship, enemies, dokill=True)
pygame.display.update()
clock.tick(60)
Can anybody help me?
Upvotes: 1
Views: 122
Reputation: 211116
The spaceship
cannot "disappear", because you have just 1 instance of the Spaceship
class and you cannot remove it from a Groups (kill
). If you don't want to see the spaceship anymore, just don't draw it:
spaceship_collided = False
running = True
while running:
# [...]
if not spaceship_collided:
screen.blit(spaceship.image, spaceship.rect)
# [...]
if pygame.sprite.spritecollide(spaceship, enemies, dokill=True):
spaceship_collided = True
# [...]
A different solution would be to use pygame.sprite.GroupSingle
:
spaceship_group = pygame.sprite.GroupSingle(Spaceship(screen, 400, 400))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == MOUSEBUTTONDOWN:
if len(spaceship_group) > 0:
spaceship = spaceship_group.sprites()[0]
bullet = Bullet(screen, spaceship.x, spaceship.y - 20)
bullets.add(bullet)
# [...]
spaceship_group.draw(screen)
# [...]
spaceship_group.update()
# [...]
pygame.sprite.groupcollide(spaceship_group, enemies, True, True)
# [...]
Upvotes: 1