我怎样才能只删除发生碰撞的精灵?
How can I only remove sprites that have collided?
这些是我的 类 和主循环的一部分,它应该创建精灵,然后在它们碰撞时将它们移除。现在,每次单击鼠标都会删除子弹和僵尸,尽管单击鼠标只会将子弹添加到组中。也许有人知道我做错了什么。
class PlayerBullet(pygame.sprite.Sprite):
def __init__(self, x, y, mouse_x, mouse_y, ):
pygame.sprite.Sprite.__init__(self, player_bullets)
self.x = x
self.y = y
self.mouse_x = mouse_x
self.mouse_y = mouse_y
self.speed = 20
self.angle = math.atan2(y-mouse_y, x-mouse_x)
self.image = circle_image
self.rect = self.image.get_rect()
def update(self):
self.x_vel = math.cos(self.angle) * self.speed
self.y_vel = math.sin(self.angle) * self.speed
self.x -= int(self.x_vel)
self.y -= int(self.y_vel)
def main(self, display):
self.update()
display.blit(self.image, (self.x-8, self.y-7))
class Zombie(pygame.sprite.Sprite):
def __init__(self, x, y, speed):
pygame.sprite.Sprite.__init__(self, list_zombies)
self.x = x
self.y = y
self.speed = zombie_speed
self.image = pygame.image.load("Zombie.png").convert()
self.rect = self.image.get_rect()
def update(self):
self.y += 1
def main(self, display):
self.update()
display.blit(zombie_image_copy, (self.x, self.y))
主循环:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.QUIT
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
player_bullets.add(PlayerBullet(player.x, player.y, mouse_x, mouse_y))
if not list_zombies:
max_zombie_count += 1
zombie_speed += 0.5
while zombie_count < max_zombie_count:
spawnpoint_x = random.randint(50,1400)
spawnpoint_y = 0
list_zombies.add(Zombie(spawnpoint_x, spawnpoint_y, zombie_speed))
zombie_count += 1
#zombie += 1
for bullet in player_bullets:
bullet.main(display)
for zombie in list_zombies:
zombie.main(display)
for zombie in list_zombies:
for bullet in player_bullets:
if pygame.sprite.collide_rect(bullet, zombie):
bullet.kill()
zombie.kill()
pygame.Surface.get_rect.get_rect()
returns 具有 Surface 对象大小的矩形,始终从 (0, 0) 开始,因为 Surface 对象没有位置。 Surface 在屏幕上的某个位置 blit
。
因此,所有矩形都具有相同的位置 (0, 0),并且碰撞测试对所有对象的计算结果为 True
。您必须在碰撞测试之前更新矩形的位置:
for zombie in list_zombies:
zombie.rect.topleft = (zombie.x, zombie.y)
for bullet in player_bullets:
bullet.rect.topleft = (bullet.x, bullet.y)
if pygame.sprite.collide_rect(bullet, zombie):
bullet.kill()
zombie.kill()
这些是我的 类 和主循环的一部分,它应该创建精灵,然后在它们碰撞时将它们移除。现在,每次单击鼠标都会删除子弹和僵尸,尽管单击鼠标只会将子弹添加到组中。也许有人知道我做错了什么。
class PlayerBullet(pygame.sprite.Sprite):
def __init__(self, x, y, mouse_x, mouse_y, ):
pygame.sprite.Sprite.__init__(self, player_bullets)
self.x = x
self.y = y
self.mouse_x = mouse_x
self.mouse_y = mouse_y
self.speed = 20
self.angle = math.atan2(y-mouse_y, x-mouse_x)
self.image = circle_image
self.rect = self.image.get_rect()
def update(self):
self.x_vel = math.cos(self.angle) * self.speed
self.y_vel = math.sin(self.angle) * self.speed
self.x -= int(self.x_vel)
self.y -= int(self.y_vel)
def main(self, display):
self.update()
display.blit(self.image, (self.x-8, self.y-7))
class Zombie(pygame.sprite.Sprite):
def __init__(self, x, y, speed):
pygame.sprite.Sprite.__init__(self, list_zombies)
self.x = x
self.y = y
self.speed = zombie_speed
self.image = pygame.image.load("Zombie.png").convert()
self.rect = self.image.get_rect()
def update(self):
self.y += 1
def main(self, display):
self.update()
display.blit(zombie_image_copy, (self.x, self.y))
主循环:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.QUIT
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
player_bullets.add(PlayerBullet(player.x, player.y, mouse_x, mouse_y))
if not list_zombies:
max_zombie_count += 1
zombie_speed += 0.5
while zombie_count < max_zombie_count:
spawnpoint_x = random.randint(50,1400)
spawnpoint_y = 0
list_zombies.add(Zombie(spawnpoint_x, spawnpoint_y, zombie_speed))
zombie_count += 1
#zombie += 1
for bullet in player_bullets:
bullet.main(display)
for zombie in list_zombies:
zombie.main(display)
for zombie in list_zombies:
for bullet in player_bullets:
if pygame.sprite.collide_rect(bullet, zombie):
bullet.kill()
zombie.kill()
pygame.Surface.get_rect.get_rect()
returns 具有 Surface 对象大小的矩形,始终从 (0, 0) 开始,因为 Surface 对象没有位置。 Surface 在屏幕上的某个位置 blit
。
因此,所有矩形都具有相同的位置 (0, 0),并且碰撞测试对所有对象的计算结果为 True
。您必须在碰撞测试之前更新矩形的位置:
for zombie in list_zombies:
zombie.rect.topleft = (zombie.x, zombie.y)
for bullet in player_bullets:
bullet.rect.topleft = (bullet.x, bullet.y)
if pygame.sprite.collide_rect(bullet, zombie):
bullet.kill()
zombie.kill()