UnboundLocalError:在赋值之前引用的局部变量"event"(PYGAME)



我正在为学校作业创建一个游戏,但它给了我"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()

在函数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

最新更新