pythonpython-3.xpygamelocalunbound

i have gotten an UnboundLocalError in python3 while making a simple snake game using pygame in python


This is the Error i got in python while executing a simple snake game program that i was working on

File "C:\Users\vicky\OneDrive\Desktop\ALL PYTHON\snake game final\snake_game.py", line 147, in gameLoop if event.type == pygame.KEYDOWN: UnboundLocalError: local variable 'event' referenced before assignment

The Code of the Program is given below:

import pygame
import time
import random

pygame.init()
pygame.mixer.init()

white = (255, 255, 255)
yellow = (255, 255, 102)
black = (0, 0, 0)
red = (213, 50, 80) 
green = (0, 255, 0)
blue = (50, 153, 213)
glow_blue = (115, 194, 251)

dis_width = 800 
dis_height = 600

FPS = 30
fpsClock = pygame.time.Clock()

dis = pygame.display.set_mode([dis_width,dis_height])
pygame.display.set_caption("Snake Game ")

snake_block = 20


score_font = pygame.font.SysFont("Algerian", 35)
finish_font = pygame.font.SysFont("copper", 55)

back_image = pygame.image.load(r"C:\Users\vicky\OneDrive\Desktop\ALL PYTHON\snake game final\back_ground2.jpg")

game_finish = pygame.image.load(r"C:\Users\vicky\OneDrive\Desktop\ALL PYTHON\snake game final\finish.jpg")

last_score = pygame.font.SysFont("Algerian", 35)
finish_score = last_score.render("Your Score", True, black)

def Your_score(score):
    value = score_font.render("Your Score: " + str(score), True, red)
    dis.blit(value, [20, 20])

def our_snake(snake_block, snake_list):
    for x in snake_list:
        #dis.blit(snake_logo,[x[0], x[1], snake_block, snake_block])

        pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])

def message(msg, color):
    mesg = finish_font.render(msg, True, color)
    dis.blit(mesg, [130, 468])


def gameLoop():
    game_over = False
    game_close = False

    x1 = dis_width / 2
    y1 = dis_height / 2

    x1_change = 0
    y1_change = 0

    snake_List = []
    Length_of_snake = 1

    foodx = round(random.randrange(0, dis_width - snake_block) / 20) * 20
    foody = round(random.randrange(0, dis_height - snake_block) / 20) * 20


    while not game_over:

        while game_close == True:
            dis.blit(game_finish,[-130,0])
            message("Press C-Play Again or Q-Quit", white)
            Your_score(Length_of_snake - 1)
            pygame.display.update()

            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_q:
                        game_over = True
                        game_close = False
                    if event.key == pygame.K_c:
                        gameLoop()
                    if event.type == pygame.QUIT:
                        game_over = True

                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            pygame.quit()
                            quit()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x1_change = -snake_block
                    y1_change = 0
                elif event.key == pygame.K_RIGHT:
                    x1_change = snake_block
                    y1_change = 0
                elif event.key == pygame.K_UP:
                    y1_change = -snake_block
                    x1_change = 0
                elif event.key == pygame.K_DOWN:
                    y1_change = snake_block
                    x1_change = 0

        if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
            pygame.mixer.music.load("out.wav")
            pygame.mixer.music.play()
            game_close = True


        x1 += x1_change
        y1 += y1_change

        dis.blit(back_image,[0,0])
        pygame.draw.rect(dis, white, [foodx, foody, snake_block, snake_block])
        #pygame.draw.circle(dis, white, [foodx, foody], 15,0)
        snake_Head = []
        snake_Head.append(x1)
        snake_Head.append(y1)
        snake_List.append(snake_Head)
        if len(snake_List) > Length_of_snake:
            del snake_List[0]

        for x in snake_List[:-1]:
            if x == snake_Head:
                game_close = True

        our_snake(snake_block, snake_List)
        Your_score(Length_of_snake - 1)

        pygame.display.update()

        if x1 == foodx and y1 == foody:
            pygame.mixer.music.load("beep.wav")
            pygame.mixer.music.play()
            foodx = round(random.randrange(0, dis_width - snake_block) / 20.0) * 20.0
            foody = round(random.randrange(0, dis_height - snake_block) / 20.0) * 20.0
            Length_of_snake += 1


        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                pygame.quit()
                quit()        


        fpsClock.tick(FPS)

    pygame.quit()
    quit()


gameLoop()

Solution

  • At the end, in :

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            pygame.quit()
            quit()
    

    event is not defined.

    I think you should move this condition in the for event in pygame.event.get(): loop higher in your code.

    Also I recommend you hide you personal data by changing every occurrence of you path with as it does not add any info regarding your issue.