UnboundLocalError:分配前引用的局部变量“事件”(PYGAME)

2024-10-02 14:21:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为学校作业创建一个游戏,但它给了我“UnboundLocalError”我查找了错误原因,但在装箱Juego()和intro_Juego()变量之前没有发现任何有用的内容。它没有给我错误,下面是完整的错误:

Traceback (most recent call last):
  File "C:/Users/OBW/Desktop/Ament/PRog/Pygame/fly jumper.py", line 129, in <module>
    intro_Juego()
  File "C:/Users/OBW/Desktop/Ament/PRog/Pygame/fly jumper.py", line 20, in intro_Juego
    Juego()
  File "C:/Users/OBW/Desktop/Ament/PRog/Pygame/fly jumper.py", line 84, in Juego
    if event.type == pygame.KEYDOWN:
UnboundLocalError: local variable 'event' referenced before assignment

我们使用的是Python 3.4.0,这是我的代码

import pygame
from random import randint

negro = (0,0,0)
blanco = (255,255,255)
rojo = (255,0,0)
verde = (0,255,0)

pygame.init()
tamano = 700,500
pantalla = pygame.display.set_mode(tamano)
pygame.display.set_caption("Fly Jumper ")

def intro_Juego():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    Juego()
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
        pantalla.fill(blanco)
        l = pygame.font.SysFont(None,40)
        text = l.render("Presione la tecla ESPACIO para iniciar",True,negro)
        pantalla.blit(text,[150,245])

        l2 = pygame.font.SysFont(None,40)
        text2 = l2.render("ESC para salir",True,negro)
        pantalla.blit(text2,[0,0])
        pygame.display.update()

def nightnightdeepshit():
    nightnight = True
    while nightnight:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    Juego()
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
        pantalla.fill(blanco)
        msgs = pygame.font.SysFont(None,25)
        textos = msgs.render("JAJAJA perdiste, presiona ESPACIO para volevr a empezar",True,rojo)
        pantalla.blit(textos,[200,245])
        pygame.display.update()


def Juego():
    terminar = False
    reloj = pygame.time.Clock()

    def bola(x,y):
        pygame.draw.circle(pantalla,negro,[x,y],20)

    def odecul(xloc,yloc,xtamano,ytamano):
        pygame.draw.rect(pantalla,verde,[xloc,yloc,xtamano,ytamano])
        pygame.draw.rect(pantalla,verde,[xloc,int(yloc+ytamano+espeso),xtamano,500])

    def puts(puntos):
        msgp = pygame.font.SysFont(None,40)
        texto2 = msgp.render("Puntos"+str(puntos),True,negro)
        pantalla.blit(texto2,[0,0])

    x = 350
    y = 250
    x_velocidad = 0
    y_velocidad = 0
    piso = 480
    xloc = 700
    yloc = 0
    xtamano = 70
    ytamano = randint(0,350)
    espeso = 150
    culvel = 2.5
    puntos = 0

    while not terminar:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                terminar = True
        if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    y_velocidad = -8
        if event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    y_velocidad = 5




        pantalla.fill(blanco)
        odecul(xloc,yloc,xtamano,ytamano)
        bola(x,y)
        puts(puntos)
        y += y_velocidad
        xloc -= culvel

        if y > piso:
            nightnightdeepshit()


        if x+20 > xloc and y-20 < ytamano and x-15 < xtamano+xloc:
            nightnightdeepshit()


        if x+20 > xloc and y+20 > ytamano+espeso and  y-15 < xtamano+xloc:
            nightnightdeepshit()



        if xloc < -80:
            xloc = 700
            ytamano = randint(0,350)

        if x > xloc and x < xloc+3:
            puntos = (puntos+1)


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

    pygame.quit()
intro_Juego()
Juego()

Tags: keyineventtrueifdeftypepygame
1条回答
网友
1楼 · 发布于 2024-10-02 14:21:38

在函数Juego()中:

while not terminar:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            terminar = True
    if event.type == pygame.KEYDOWN:              # <  HERE
        if event.key == pygame.K_SPACE:
            y_velocidad = -8
    if event.type == pygame.KEYUP:                # <  AND HERE
        if event.key == pygame.K_SPACE:
            y_velocidad = 5

这看起来像一个简单的缩进问题。但是错误的原因是event变量仅在循环至少执行一次时定义。在本例中(可能在第一次运行时),没有pygame事件,因此event变量从未设置过

显然,修复压痕将解决问题

while not terminar:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            terminar = True
        if event.type == pygame.KEYDOWN:            
            if event.key == pygame.K_SPACE:
                y_velocidad = -8
        if event.type == pygame.KEYUP:              
            if event.key == pygame.K_SPACE:
                y_velocidad = 5

相关问题 更多 >