赋值前引用的局部变量 'number_dropped'

local variable 'number_dropped' referenced before assignment

我目前在 pygame 中遇到错误。当我点击我的游戏的播放按钮时,我收到一个登录错误提示:

”第 166 行,在 game_loop number_dropped() UnboundLocalError: 局部变量 'number_dropped' 在赋值前被引用

我目前不知道如何解决这个问题,如果有人能提供帮助,我们将不胜感激。我是 python 和编码的新手,所以请记住我没有什么经验。

错误出现在这段代码中:

things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        basket(x,y)
        things_dropped(dropped)

        if x > display_width - basket_width or x < 0:
            number_dropped()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            number_dropped += 1
            thing_speed += 1
            thing_width += (nd * 1.2)

        if y < thing_starty+thing_height:
            print('y crossover')

            if x > thing_startx and x < thing_startx + thing_width or x+basket_width > thing_startx and x + basket_width < thing_startx+thing_width:
               print('x crossover')
               number_dropped()
            


            

        pygame.display.update()
        clock.tick(60)

下面是完整的代码,您可能需要用它来解决问题。感谢任何回应的人以及您的时间和患者。 :D

import pygame
import time
import random
import sys


#initiates Pygame
pygame.init()

#Sets the display width and height of the GUI
display_width = 800
display_height = 600

#Shades
black = (0,0,0)
white = (255,255,255)

#Colours
red = (200,0,0)
green = (0,200,0)
blue = (0,255,0)

bright_red =(255,0,0)
bright_green = (0,255,0)
 

block_color = (53,115,225)

#Identification of the basket width
basket_width = 859

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Catch the Batch')
clock = pygame.time.Clock()

basketImg = pygame.image.load('basket.png')
basketImg = pygame.transform.scale(basketImg,(100,100))

#Counter for how many objects the player has dodged
def things_dropped(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dropped: "+str(count), True, black)
    
    gameDisplay.blit(text,(0,0))
    
#Defines what objects the player will have to try and catch
def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, block_color, [thingx, thingy, thingw, thingh])

def basket(x,y):
    gameDisplay.blit(basketImg,(x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',20)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()

#This function displays a message when you have dropped too many pinikalatas
def number_dropped():
    message_display('You dropped too many Pinikalatas, Game Over!')

def button(msg,x,y,w,h,ic,ac, action = None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y+h > mouse [1] > y:
        pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
        if click[0] == 1 and action != None:
            action()
            
    else:
        pygame.draw.rect(gameDisplay,ic,(x,y,w,h))

        smallText = pygame.font.Font("freesansbold.ttf",20)
        textSurf, textRect = text_objects(msg, smallText)
        textRect.center = ( (x+(w/2)), (y+(h/2)) )
        gameDisplay.blit(textSurf, textRect)

def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf',80)
        TextSurf, TextRect = text_objects("Catch the Batch", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Play!",350,400,100,50,green,bright_green,game_loop)
        button("X",750,0,50,50,red,bright_red,CTBquit)

        pygame.display.update()
        clock.tick(15)
        
#ctb stands for 'Catch the Batch' <-- (Title of game)
def CTBquit():
    pygame.quit()
    quit()

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 7
    thing_width = 100
    thing_height = 100

    thingCount = 1

    dropped = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type ==  pygame.QUIT:
                pygame.quit()
                quit()


            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            
                    
        x += x_change 
        gameDisplay.fill(white)


        # things(thingx, thingy, thingw, thingh, color)
        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        basket(x,y)
        things_dropped(dropped)

        if x > display_width - basket_width or x < 0:
            number_dropped()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            number_dropped += 1
            thing_speed += 1
            thing_width += (nd * 1.2)

        if y < thing_starty+thing_height:
            print('y crossover')

            if x > thing_startx and x < thing_startx + thing_width or x+basket_width > thing_startx and x + basket_width < thing_startx+thing_width:
               print('x crossover')
               number_dropped()
            


            

        pygame.display.update()
        clock.tick(60)

     
game_intro()
game_loop()
pygame.quit()
quit()

我觉得名字 number_dropped 打错了。

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            number_dropped += 1  #  <- the name number_dropped is conflicted with the function
            thing_speed += 1
            thing_width += (nd * 1.2)

我想你的意思是:

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0,display_width)
            dropped += 1  #  <----
            thing_speed += 1
            thing_width += (nd * 1.2)