【发布时间】:2020-05-14 22:06:49
【问题描述】:
我正在为学校作业创建一个游戏,但它给了我“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()
【问题讨论】:
-
缩进出现错误的 if 语句。它们目前不属于
for循环。 -
请注意,
event在循环终止后保持设置,但只有如果它首先设置,如果pygame.event.get()返回空则不会可迭代的。但是根据其余的代码,我认为您不希望这些语句发生在 循环之后,而是 in。