我有一个敌人列表但我无法弄清楚如何将它们保留在屏幕上。 (太空侵略者)

时间:2016-05-13 13:22:40

标签: python pygame

enemy.x无法正常工作,因为选中的敌人被击毙后会从列表中消失。我尝试了其他方法,并设法让他们停下来,但他们不会回来。这是我的第一个游戏,所以我不熟悉这些可能的错误。请帮助:)

def level1():
        shipX = 462
        shipY = 668
        bg = pygame.image.load('spacebackground.png')
        ship1 = pygame.image.load('ship1.png')
        lives = pygame.image.load('ship1.png')
        lasers = []


        enemy_x = 50
        enemy_x2 = 50
        enemy_x3 = 50
        enemy1 = pygame.image.load('enemy1.png')
        enemy1 = pygame.transform.scale(enemy1, (50, 50))
        enemies = []

        for i in range(10):
            enemies.append(pygame.Rect(enemy_x, 100, 50, 50))
            enemy_x += 65
        for i in range(10):
            enemies.append(pygame.Rect(enemy_x2, 150, 50, 50))
            enemy_x2 += 65
        for i in range(10):
            enemies.append(pygame.Rect(enemy_x3, 200, 50, 50))
            enemy_x3 += 65

        #Event handling
        while True:
            clock.tick(60)

            laserX = shipX + 23
            laserY = 658
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
                if (pygame.key.get_pressed()[pygame.K_SPACE]) != 0:
                    lasers.append(pygame.Rect(laserX, laserY, 5, 20))

            #setting background

            bg = pygame.transform.scale(bg, (1024, 768))
            shipRect = pygame.Rect(shipX, shipY, 50, 50)
            ship1 = pygame.transform.scale(ship1, (50, 50))
            lives = pygame.transform.scale(lives, (25, 25))

            if (pygame.key.get_pressed()[pygame.K_RIGHT]) != 0:
                shipX += 20
            if (pygame.key.get_pressed()[pygame.K_LEFT]) != 0:
                shipX += -20
            for enemy in enemies:
                for laser in lasers:
                    if laser.colliderect(enemy):
                        enemies.remove(enemy)
                        lasers.remove(laser)
                        break

            for laser in lasers:
                laser.move_ip(0, -10)
            for laser in lasers:
                if laser.y <= -20:
                    lasers.remove(laser)

            if shipX <= 0:
                shipX = 0
            if shipX >= 974:
                shipX = 974

            screen.blit(bg, (0,0))
            for laser in lasers:
                pygame.draw.rect(screen, (red), laser)
            for enemy in enemies:
                screen.blit(enemy1, enemy.topleft)
                enemy.x += 1

            screen.blit(ship1, (shipRect))
            screen.blit(lives, (5,733))`enter code here`
            screen.blit(lives, (33,733))

            #update Display
            pygame.display.update()

1 个答案:

答案 0 :(得分:1)

您的敌人正在消失,因为您已将其从列表中enemies删除:

for enemy in enemies:
  for laser in lasers:
    if laser.colliderect(enemy):
      enemies.remove(enemy)    # <---- here
      lasers.remove(laser)
      break

一旦敌人从列表中移除,它将不再被绘制。

你想要做的是跟踪哪些敌人是活着的&#39;哪些是“死了”。您可以为包含布尔值alive的敌人创建一个类,或者维护一个布尔值的并行数组。

例如:

# Set up `enemies` list
alive = [True for e in enemies]
for enemy in enemies:
  for laser in lasers:
    if laser.colliderect(enemy):
      alive[enemies.index(enemy)] = False
      lasers.remove(laser)
      break

然后,当你渲染敌人时,只需检查以确保敌人在绘制之前还活着。然后,当你想让敌人重新出现时,简单地将值设置回true并再次绘制它。请注意,敌人仍然会在原来的位置,因此您需要将其移出屏幕,或者确保您不会检测死敌上的激光碰撞。

GLHF