gpt4 book ai didi

python - 当我使用 pygame.sprite.spritecollide() 时,为什么只有子弹消失了?

转载 作者:行者123 更新时间:2023-12-04 01:05:02 25 4
gpt4 key购买 nike

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

这是我当前的代码:

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检测到碰撞时的敌人:

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

关于python - 当我使用 pygame.sprite.spritecollide() 时,为什么只有子弹消失了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66752240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com