Python 局部变量';丢弃的数量';分配前参考

Python 局部变量';丢弃的数量';分配前参考,python,windows,64-bit,Python,Windows,64 Bit,我目前在pygame中有一个错误。当我单击游戏的play(播放)按钮时,我收到一个日志错误,上面写着: 第166行,在游戏循环中 已删除的数字() UnboundLocalError:赋值前引用的局部变量“number\u dropped” 我目前还不知道如何解决这个问题,如果有人能帮忙,我将不胜感激。我不熟悉python和编码,所以请记住我没有什么经验 我使用的应用程序是空闲python,版本3.4.0 该错误出现在代码的这一部分: things(thing_startx, thing_s

我目前在pygame中有一个错误。当我单击游戏的play(播放)按钮时,我收到一个日志错误,上面写着:

第166行,在游戏循环中 已删除的数字() UnboundLocalError:赋值前引用的局部变量“number\u dropped”

我目前还不知道如何解决这个问题,如果有人能帮忙,我将不胜感激。我不熟悉python和编码,所以请记住我没有什么经验

  • 我使用的应用程序是空闲python,版本3.4.0
该错误出现在代码的这一部分:

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)
东西(东西开始,东西开始,东西宽度,东西高度,黑色)
物体开始速度+=物体速度
篮子(x,y)
掉下来的东西
如果x>显示宽度-篮宽度或x<0:
已删除的数字()
如果事物开始>显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)
丢弃的数量+=1
速度+=1
物体宽度+=(nd*1.2)
如果y<物体起点+物体高度:
打印('y交叉')
如果x>thing\u startx和xthing\u startx和x+basket\u width
下面是整个代码,您可能需要这些代码来解决问题。感谢所有做出回应的人以及您的时间和患者: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()
导入pygame
导入时间
随机输入
导入系统
#启动Pygame
pygame.init()
#设置GUI的显示宽度和高度
显示宽度=800
显示高度=600
#阴影
黑色=(0,0,0)
白色=(255255)
#颜色
红色=(200,0,0)
绿色=(0200,0)
蓝色=(0255,0)
鲜红色=(255,0,0)
亮绿色=(0255,0)
块颜色=(53115225)
#篮子宽度的识别
篮子宽度=859
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('Catch the Batch')
clock=pygame.time.clock()
basketImg=pygame.image.load('basket.png')
basketImg=pygame.transform.scale(basketImg,(100100))
#计算玩家躲闪了多少物体
def掉落的东西(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“删除:”+str(计数),真,黑色)
blit(文本,(0,0))
#定义玩家必须尝试和捕捉的对象
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,块颜色,[thingx,thingy,thingw,thingh])
def篮(x,y):
游戏显示.blit(basketImg,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',20)
TextSurf,TextRect=text\u对象(text,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
#当您掉落过多的Pinikalata时,此功能将显示一条消息
def编号_已删除():
信息显示('你掉了太多的Pinikalata,游戏结束!')
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(游戏显示,ic,(x,y,w,h))
smallText=pygame.font.font(“freesansbold.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
blit(textSurf,textRect)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font('freesansbold.ttf',80)
TextSurf,TextRect=text\u对象(“捕获批”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
按钮(“玩!”,350400100,50,绿色,亮绿色,游戏循环)
按钮(“X”,750,0,50,50,红色,亮红色,CTBquit)
pygame.display.update()
时钟滴答(15)
#ctb代表“捕获批次”显示宽度-篮宽度或x<0:
已删除的数字()
如果事物开始>显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)
丢弃的数量+=1
速度+=1
物体宽度+=(nd*1.2)
如果y<物体起点+物体高度:
打印('y交叉')
如果x>thing\u startx和xthing\u startx和x+basket\u width
我认为你掉下来的名字
号码是个打字错误

if thing\u starty>显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)
下降次数+=1#显示高度:
物体起点=0-物体高度
thing\u startx=random.randrange(0,显示宽度)

dropped+=1#很抱歉,我意识到了一些错误,例如nd,我现在已将其更改为dropped,但仍然存在在赋值之前引用数字_dropped()的问题。天哪,谢谢你,你的反应相当快