我正在尝试使其从房间A移到房间B时,房间中的所有射弹消失。此刻,如果我向右射击3个弹丸,然后左移到另一个房间,则这些弹丸仍会出现在屏幕上,直到它们超出屏幕尺寸为止。
使用all_sprites_list.remove(projectile)
功能可以使我从一个房间移到另一个房间一次移除一颗子弹。
例如,当我使用该行代码时,如果屏幕上当前有3个项目符号,并且我从A房间移至B房间,则屏幕上的一个项目符号将被移除,而其他2个项目符号将保留。回到房间A会移出另一颗子弹,再次移到房间B会移出第三颗。
我的问题是,当我改变房间时,如何同时移除所有三个子弹,而不是游戏中移除单个子弹?这是我的事件处理-我认为不需要任何其他代码,但是请告诉我是否需要。
# ---------- MAIN PROGRAM LOOP ---------- #
while not done:
# --- Event Processing ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Controls
elif event.type == pygame.KEYDOWN:
projectile = None
if event.key == pygame.K_LEFT:
projectile = ProjectileLeft("projectile.png", 0, 0)
projectile.rect.x = player.rect.x-15
projectile.rect.y = player.rect.y+33
elif event.key == ord('a'):
player.changespeed(-7, 0)
elif event.key == ord('d'):
player.changespeed(7, 0)
elif event.key == ord('w'):
player.changespeed(0, -7)
elif event.key == ord('s'):
player.changespeed(0, 7)
if projectile:
projectile_list.add(projectile)
elif event.type == pygame.KEYUP:
if event.key == ord('a'):
player.changespeed(7, 0)
elif event.key == ord('d'):
player.changespeed(-7, 0)
elif event.key == ord('w'):
player.changespeed(0, 7)
elif event.key == ord('s'):
player.changespeed(0, -7)
all_sprites_list.update()
projectile_list.update()
这是我的游戏逻辑;
player.move(current_room.wall_list)
if player.rect.x < -15:
if current_room_no == 0:
current_room_no = 3
current_room = rooms[current_room_no]
player.rect.x = 790
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
elif current_room_no == 3:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 790
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
elif current_room_no == 2:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 790
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
else:
current_room_no = 0
current_room = rooms[current_room_no]
player.rect.x = 790
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
if player.rect.x > 801:
if current_room_no == 0:
current_room_no = 1
current_room = rooms[current_room_no]
player.rect.x = 0
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
elif current_room_no == 1:
current_room_no = 2
current_room = rooms[current_room_no]
player.rect.x = 0
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
elif current_room_no == 2:
current_room_no = 3
current_room = rooms[current_room_no]
player.rect.x = 0
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
else:
current_room_no = 0
current_room = rooms[current_room_no]
player.rect.x = 0
projectile_list.remove(projectile)
all_sprites_list.remove(projectile)
这是我脚本的结尾:
# Load background to current room
screen.blit(current_room.background_image, current_room.background_position)
# Load time to screen
Timer = font.render(output_string, True, constants.WHITE)
screen.blit(Timer, [350, 30])
# Draw all sprites in lists on screen
all_sprites_list.draw(screen)
current_room.wall_list.draw(screen)
current_room.enemy_sprites.draw(screen)
if len(projectile_list)!=0
projectile_list.draw(screen)
frame_count += 1
clock.tick(frame_rate)
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()
答案 0 :(得分:0)
注释中有些混乱,因为projectile_list
实际上不是列表,而是pygame.sprite.Group
。如果要从组中删除所有精灵,则可以调用empty方法:projectile_list.empty()
。
请注意,empty
仅将精灵从该特定组中删除。如果要从所有组中删除相同的精灵,可以遍历这些精灵并调用其kill
方法。