如何在迭代时从组中删除一个精灵(列表)?
我希望,如果子弹触摸(幽灵),则组中的一个幽灵(列表)将消失。这是代码的一部分。 PS;很抱歉我以前的问题
import pygame, math, random, os
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
super().__init__()
self.x = x + 15
self.y = y + 25
self.fx = 10
self.fy = 10
self.direction = direction
def draw_bullet(self, screen):
screen.blit(bullet_img, (self.x, self.y))
def move(self):
if self.direction == 1:
self.x += 15
if self.direction == -1:
self.x -= 15
def off_screen(self):
return not(self.x >= 0 and self.x <= width)
def update(self):
self.rect.x += 5
class Ghost(pygame.sprite.Sprite):
def __init__(self, x, y, fx,fy):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('ghost.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.fartx = fx
self.farty = fy
lst = [0,1,2,3,4,5,6,7]
for i in range(len(lst)):
ghost = Ghost(random.randint(0,width),random.randint(0,height),random.randint(1,8),random.randint(1,8))
ghostgruppe.add(ghost)
ghosttruffet = pygame.sprite.groupcollide(ghostgruppe, bulletgruppe, True, True, pygame.sprite.collide_mask)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果Glost重叠,并且您只想删除触摸子弹的拳头幽灵,则需要将
false
传递给dokill1
pygame.sprite.groupcollide.groupcolleide() =“ https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.sprite.sprite.kill” rel =“ nofollow noreferrer”>
第一个幽灵:
请参阅 :
If the Glosts overlap and you just want to delete the fist ghost touching the bullet, you need to pass
False
to thedokill1
argument ofpygame.sprite.groupcollide()
and manuallykill()
the first ghost:See
pygame.sprite.groupcollide()
: