本文介绍了当我使用pygame.sprite.spritecollide()时,为什么只有子弹消失了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个飞船游戏,你可以在其中控制飞船.你应该发射子弹来摧毁敌机.现在我正试图让子弹和敌人都消失.但是当我运行我的游戏时,只有子弹消失了.有人可以帮我吗?

I am making a spaceship game where you control a spaceship. You are supposed to fire bullets to destroy enemy planes. Right now I am trying to make both the bullet and the enemy disappear. But when I ran my game, only the bullets disappeared. Can anybody help me?

这是我当前的代码:

import pygame
from pygame.locals import *
from random import randint

pygame.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("C:/eqodqfe/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("C:/eqodqfe/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):
        pygame.sprite.Sprite.__init__(self)

        self.screen, self.x, self.y = s, x, y
        self.image = pygame.image.load("C:/eqodqfe/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)

    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)

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(-200, 800), 0)
            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()
        pygame.sprite.spritecollide(i, bullets, True)

    pygame.display.update()
    clock.tick(60)

有人可以帮我吗?

推荐答案

只需使用 pygame.sprite.groupcollide() 而不是 pygame.sprite.spritecollid():

for i in enemies:
    i.update()
    # pygame.sprite.spritecollide(i, bullets, True) <--- DELETE

pygame.sprite.groupcollide(enemies, bullets, True, True)

或者kill 检测到碰撞时的敌人:

Or kill the enemy when a collision is detected:

for i in enemies:
    i.update()
    if pygame.sprite.spritecollide(i, bullets, True):
        i.kill()

这篇关于当我使用pygame.sprite.spritecollide()时,为什么只有子弹消失了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 10:00