pythonpygame

UnboundLocalError: local variable 'event' referenced before assignment


I am creating a game for a school assignment, but it is giving me the "UnboundLocalError", I looked up the reason for the error but didn't find nothing useful. Before I created the Juego() and intro_Juego() variables it didn't gave me the error, here is the complete error:

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
  

We are using Python 3.4.0, this is my code

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()

Solution

  • In the function 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
    

    This looks like a simple indentation problem. But the reason for the error is that the event variable is only defined if the loop is executed at least once. In this case (probably on the very first run), there were no pygame-events, so the event variable was not ever set.

    Obviously repairing the indentation will fix the issue.

    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