为什么在最后一个敌人被杀后会崩溃? [太空侵略者]

时间:2016-05-24 22:51:53

标签: python pygame

我是编码的新手,我很难弄清楚为什么我的游戏在最后一个敌人被杀之后崩溃了。我正在为我最后的学校项目制作一个太空入侵者游戏。就目前而言,我只是试图让最常见的敌人每隔一段时间射击并且它起作用,但是当最后一个敌人在顶行被杀时,我得到一个错误代码。从我的理解,这就像程序试图删除不存在的敌人激光,然后我得到错误代码----> list.remove(x):x不在列表中。请帮忙谢谢

def level1():

    # Images
    bg = pygame.image.load('spacebackground.jpg')

    bg = pygame.transform.scale(bg, (1024, 768))
    ship1 = pygame.image.load('ship1.png')
    ship1 = pygame.transform.scale(ship1, (50, 50))
    life1 = pygame.image.load('ship1.png')
    life1 = pygame.transform.scale(life1, (25, 25))
    life2 = pygame.image.load('ship1.png')
    life2 = pygame.transform.scale(life2, (25, 25))
    enemy1 = pygame.image.load('enemy1.png')
    enemy1 = pygame.transform.scale(enemy1, (50, 50))
    enemy2 = pygame.image.load('enemy2.png')
    enemy2 = pygame.transform.scale(enemy2, (50, 50))
    points = 0

    # Coordinates
    enemy_x = 50
    enemy_x2 = 50
    enemy_x3 = 50
    dx = 5
    dx2 = 10
    dx3 = 5
    shipX = 462
    shipY = 668

    # Lists
    enemies = []
    enemies_2 = []
    enemies_3 = []
    enemyLaser1 = []
    enemyLaser2 = []
    enemyLaser3 = []
    lasers = []

    # Appending enemies
    for i in range(10):
        enemies.append(pygame.Rect(enemy_x, 100, 50, 50))
        enemy_x += 65
    for i in range(10):
        enemies_2.append(pygame.Rect(enemy_x2, 150, 50, 50))
        enemy_x2 += 65
    for i in range(10):
        enemies_3.append(pygame.Rect(enemy_x3, 225, 50, 50))
        enemy_x3 += 65

    # Game Loop
    while True:
        clock.tick(60)

        # Laser Coordinates
        laserX = shipX + 23
        laserY = 658

        # Event handling
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            # Creating lasers with space bar
            if (pygame.key.get_pressed()[pygame.K_SPACE]) != 0 and len(lasers) < 3:
                lasers.append(pygame.Rect(laserX, laserY, 5, 20))

        # Ship Rect
        shipRect = pygame.Rect(shipX, shipY, 50, 50)

        # Ship Controls
        if (pygame.key.get_pressed()[pygame.K_RIGHT]) != 0:
            shipX += 20
        if (pygame.key.get_pressed()[pygame.K_LEFT]) != 0:
            shipX += -20

        # Removing enemies and lasers when collision
        for enemy in enemies:
            for laser in lasers:
                if laser.colliderect(enemy):
                    enemies.remove(enemy)
                    lasers.remove(laser)
                    points += 5
                    break
        for enemy_2 in enemies_2:
            for laser in lasers:
                if laser.colliderect(enemy_2):
                    enemies_2.remove(enemy_2)
                    lasers.remove(laser)
                    points += 10
                    break
        for enemy_3 in enemies_3:
            for laser in lasers:
                if laser.colliderect(enemy_3):
                    enemies_3.remove(enemy_3)
                    lasers.remove(laser)
                    points += 5
                    break

        # Moving laser
        for laser in lasers:
            laser.move_ip(0, -10)

        # Removing laser when off screen
        for laser in lasers:
            if laser.y <= -20:
                lasers.remove(laser)

        # Keeping ship on screen
        if shipX <= 0:
            shipX = 0
        if shipX >= 974:
            shipX = 974

        # Reversing direction when enemies reach boundary
        if len(enemies) >= 1:
            if enemies[0].x < 50:
                dx = -dx
            if enemies[-1].x > 924:
                dx = -dx
        if len(enemies_2) >= 1:
            if enemies_2[0].x < 50:
                dx2 = -dx2
            if enemies_2[-1].x > 924:
                dx2 = -dx2
        if len(enemies_3) >= 1:
            if enemies_3[0].x < 50:
                dx3 = -dx3
            if enemies_3[-1].x > 924:
                dx3 = -dx3

        # Move each enemy
        for enemy in enemies:
            enemy.x += dx
        for enemy_2 in enemies_2:
            enemy_2.x += dx2
        for enemy_3 in enemies_3:
            enemy_3.x += dx3

        # Enemy Shooting
        if len(enemies) >= 1:
            randEnemy1 = random.randrange(len(enemies))
        if len(enemies_2) >= 1:
            randEnemy2 = random.randrange(len(enemies_2))
        if len(enemies_3) >= 1:
            randEnemy3 = random.randrange(len(enemies_3))

        if len(enemies) >= 0:
            for enemy in enemies:
                if enemy == enemies[randEnemy1] and len(enemyLaser1) < 3:
                    enemyLaser1.append(pygame.Rect(enemy.x + 22.5, 120, 5, 20))

        for enemyLaser in enemyLaser1:
            enemyLaser.move_ip(0, 10)
        if enemyLaser.y >= 769:
            enemyLaser1.remove(enemyLaser)

        # Drawing
        screen.blit(bg, (0,0))
        for laser in lasers:
            pygame.draw.rect(screen, red, laser)
        for enemyLaser in enemyLaser1:
            pygame.draw.rect(screen, red, enemyLaser)

        screen.blit(ship1, shipRect)
        for enemy in enemies:
            screen.blit(enemy1, enemy)
        for enemy_2 in enemies_2:
            screen.blit(enemy2, enemy_2)
        for enemy_3 in enemies_3:
            screen.blit(enemy1, enemy_3)
        font = pygame.font.SysFont("OCR A Extended", 30)
        pointsFont = font.render("Points:"+ str(points), 1, red)
        screen.blit(life1, (5,733))
        screen.blit(life2, (33,733))
        screen.blit(pointsFont, (5, 5))

        # Update Display
        pygame.display.update()

1 个答案:

答案 0 :(得分:0)

这是问题所在:

for enemy in enemies:
        for laser in lasers:
            if laser.colliderect(enemy):
                enemies.remove(enemy)
                lasers.remove(laser)

在第一个敌人之后,你再次循环,但激光已被移除。您需要创建一个类并设置一个标志,将它们设置为活动/非活动而不是删除它们,然后再删除它们。