![](/img/trans.png)
[英]How to fix UnboundLocalError: local variable 'o1' referenced before assignment
[英]How to fix: “UnboundLocalError: local variable 'all_sprites' referenced before assignment”
我正在制作游戲並在游戲循環中更新精靈,但它說的是:
"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()
下面的代碼更改將解決您的問題。
# 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”
。 是的,想想if statement
game_over
為None
時會發生什么? 然后,您的代碼嘗試執行all_sprites.update()
,因此在那時您沒有創建all_sprites
變量。 這意味着您沒有在if statement
執行all_sprites = pygame.sprite.Group()
,因為game_over
為None
。 得到它了? 我認為此示例之后的所有以下代碼行都應在if statement
。 否則,您將在all_sprites.add(expl)
再次遇到相同的錯誤。 請檢查並讓我知道!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.