如何解决:“ UnboundLocalError:分配前引用了本地变量'all_sprites'”

时间:2019-08-04 04:54:30

标签: python python-3.x pygame

我正在制作游戏并在游戏循环中更新精灵,但这说明:

"File "/Users//PycharmProjects/Pygame3/Shoot 'em Up.py", line 505, in game_loop
    all_sprites.update()
UnboundLocalError: local variable 'all_sprites' referenced before assignment"

我认为这意味着将其设置为全局变量,但这没有用。也许我只是错误地声明了它?我为游戏循环上方的所有子画面提供了变量,但这也不起作用。有谁知道如何解决这个问题?谢谢!

all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
powerups = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    newmob()
score = 0

def game_loop():  # Game loop

    game_over = False

    running = True

    while running:

        # keeps loop running at the right speed
        clock.tick(FPS)
        # Process input (events)
        for event in pygame.event.get():
            # print(event)  # prints all events (mouse motions, keys pressed, etc.)
            # checks for closing window
            if event.type == pygame.QUIT:
                running = False

        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_p]:
            pause()

        # shows game over screen and resets stats and graphics
        if game_over:
            game_over_screen()
            game_over = False
            all_sprites = pygame.sprite.Group()
            mobs = pygame.sprite.Group()
            bullets = pygame.sprite.Group()
            powerups = pygame.sprite.Group()
            player = Player()
            all_sprites.add(player)
            for i in range(8):
                newmob()
            score = 0

        # Update
        all_sprites.update()

        # checks to see if a bullet hits a mob and spawns powerups
        hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
        for hit in hits:
            score += 51 - hit.radius
            expl_sound.play()
            expl = Explosion(hit.rect.center, 'lg')
            all_sprites.add(expl)
            if random.random() > 0.95:
                pow = Pow(hit.rect.center)
                all_sprites.add(pow)
                powerups.add(pow)
            newmob()

        # checks to see if a mob hits a player
        hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
        for hit in hits:
            player.shield -= hit.radius * 2
            expl = Explosion(hit.rect.center, 'sm')
            all_sprites.add(expl)
            newmob()
            if player.shield <= 0:
                player_die_sound.play()
                death_explosion = Explosion(player.rect.center, 'player')
                all_sprites.add(death_explosion)
                player.hide()
                player.lives -= 1
                player.shield = 100

        # if the player has died and the explosion has finished playing
        if player.lives == 0 and not death_explosion.alive():
            game_over = True

        # checks to see if the player hit a powerup
        hits = pygame.sprite.spritecollide(player, powerups, True)
        for hit in hits:
            if hit.type == 'shield':
                player.shield += random.randrange(10, 30)
                shield_sound.play()
                if player.shield >= 100:
                    player.shield = 100
            if hit.type == 'gun':
                player.powerup()
                power_sound.play()

        # Draw / render
        screen.fill(BLACK)
        screen.blit(background, background_rect)
        all_sprites.draw(screen)
        draw_text(screen, str(score), 18, WIDTH / 2, 10, WHITE)
        draw_shield_bar(screen, 5, 5, player.shield)
        draw_lives(screen, WIDTH - 100, 5, player.lives, player_mini_img)

        # *after* drawing everything, flip the display
        pygame.display.flip()

1 个答案:

答案 0 :(得分:1)

下面的代码更改将解决您的问题。

# shows game over screen and resets stats and graphics
if game_over:
    game_over_screen()
    game_over = False
    all_sprites = pygame.sprite.Group()
    mobs = pygame.sprite.Group()
    bullets = pygame.sprite.Group()
    powerups = pygame.sprite.Group()
    player = Player()
    all_sprites.add(player)
    for i in range(8):
        newmob()
    score = 0

    # Update
    all_sprites.update()  # <-- this line should be in this if statement.

错误明显提到了local variable 'all_sprites' referenced before assignment”。是的,想想当game_over中的Noneif statement时会发生什么?然后,您的代码尝试执行all_sprites.update(),因此在那时您没有创建all_sprites变量。这意味着您没有执行all_sprites = pygame.sprite.Group()中的if statement,因为game_overNone。得到它了?我认为此示例之后的以下所有代码行都应在您的if statement中。否则,您将在all_sprites.add(expl)中再次遇到相同的错误。请检查并让我知道!