UnboundLocalError

Please tell me how to fix the UnboundLocalError
I am currently trying to make a car racing game which is still not completed.

I have linked my code’s file below and even pasted it here if you don’t wish to download it:-

import random

import pygame
pygame.init()
gray= (119, 118, 110)
black=(0,0,0)
red=(255,0,0)
import time
import random

display_width = 800
display_height = 600
game_displays = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption(‘CAR GAME’)
clock=pygame.time.Clock()
car_image=pygame.image.load(‘Car_1.jpeg’)
background_image=pygame.image.load(‘Background_1.jpeg’)
yellow_strip=pygame.image.load(‘Yellow_strip.png’)
strip=pygame.image.load(‘Strip.jpeg’)
car_width=56

def obstacle(obs_startx,obs_starty,obs):
if obs==0:
obs_pic=pygame.image.load(‘car2.jpeg’)
elif obs==1:
obs_pic=pygame.image.load(‘car3.jpeg’)
elif obs==2:
obs_pic=pygame.image.load(‘car4.jpeg’)
elif obs==3:
obs_pic=pygame.image.load(‘car5.jpeg’)
elif obs==4:
obs_pic=pygame.image.load(‘car6.jpeg’)
elif obs==5:
obs_pic=pygame.image.load(‘car7.jpeg’)
game_displays.blit(obs_pic,(obs_startx,obs_starty))

def score_system(passed,score):
font=pygame.font.SysFont(None,25)
text=font.render(“Passed”+str(passed),True,black)
score=font.render(“Score”+str(score),True,red)
game_displays.blit(text,(0,50))
game_displays.blit(score,(0,30))

def text_objects(text,font):
textsurface=font.render(text,True,black)
return textsurface,textsurface.get_rect()

def message_display(text):
largetext=pygame.font.Font(“freesansbold.ttf”,80)
textsurf,textrect=text_objects(text,largetext)
textrect.center=((display_width/2),(display_height/2))
game_displays.blit(textsurf,textrect)
pygame.display.update()
time.sleep(1)
game_loop()

def crash():
message_display(“YOU CRASHED!!”)

def background():
game_displays.blit(background_image,(0,0))
game_displays.blit(background_image,(0,200))
game_displays.blit(background_image,(0,400))
game_displays.blit(background_image,(700,0))
game_displays.blit(background_image,(700,200))
game_displays.blit(background_image,(700,400))
game_displays.blit(yellow_strip,(376,0))
game_displays.blit(yellow_strip,(376,100))
game_displays.blit(yellow_strip,(376,200))
game_displays.blit(yellow_strip,(376,300))
game_displays.blit(yellow_strip,(376,400))
game_displays.blit(yellow_strip,(376,500))
game_displays.blit(strip,(120,0))
game_displays.blit(strip,(120,100))
game_displays.blit(strip,(120,200))
game_displays.blit(strip, (677,0))
game_displays.blit(strip, (677,100))
game_displays.blit(strip, (677,200))

def car(x,y):
game_displays.blit(car_image,(x,y))#blit enters the image on the game screen

def game_loop():
x=(display_width0.47)
y=(display_height
0.8)
x_change=0
obstacle_speed=9
obs=0
y_change=0
obs_startx=random.randrange(200,(display_width-200))
obs_starty=-750
obs_width=56
obs_height=125
car_passed=0
level=0
score=0

bumped=False
while not bumped:
    for event in pygame.event.get():
        if event .type==pygame.QUIT:
            pygame.quit()
            quit()
    if event.type==pygame.KEYDOWN:#keydown just means if some one presses a key
        if event.key==pygame.K_LEFT:
            x_change=-5
        if event.key==pygame.K_RIGHT:
            x_change=5
        if event.key==pygame.K_a:
            obstacle_speed+=2
        if event.key==pygame.K_b:
            obstacle_speed-=2
    if event.type==pygame.KEYUP:#key up just means if no key is pressed so it will not move as thenx_xhange=0
        if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
            x_change=0

    x+=x_change
    game_displays.fill(gray)
    background()
    obs_starty-=(obstacle_speed/4)
    obstacle(obs_startx,obs_starty,obs)
    obs_starty+=obstacle_speed
    car(x,y)
    score_system(passed,score)

    if x>680-car_width or x<110:
        crash()
    if x > display_width - (car_width + 110) or x < 110:
        crash()
    if obs_starty > display_height:
        obs_starty = 0 - obs_height
        obs_startx = random.randrange(170, (display_width - 170))
        obs = random.randrange(0,7)
        passed=passed+1
        score=passed*10
        if int(passed) % 10 == 0:
            level = level + 1
            obstacle_speed + 2
            largetext = pygame.font.Font("freesansbold.ttf", 80)
            textsurf, textrect = text_objects("LEVEL" + str(level), largetext)
            textrect.center = ((display_width / 2), (display_height / 2))
            gamedisplays.blit(textsurf, textrect)
            pygame.display.update()
            time.sleep(3)

    if y<obs_starty+obs_height:
        if x > obs_startx and x < obs_startx + obs_width or x+car_width > obs_startx and x+car_width < obs_startx+obs_width:
            crash()

    pygame.display.update()#update() is to make the display Surface actually appear on the user's monitor.
    #If we will delete the above line only a black screen will appear
    clock.tick(60)

game_loop()
pygame.quit()
quit()import random

import pygame
pygame.init()
gray= (119, 118, 110)
black=(0,0,0)
red=(255,0,0)
import time
import random

display_width = 800
display_height = 600
game_displays = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption(‘CAR GAME’)
clock=pygame.time.Clock()
car_image=pygame.image.load(‘Car_1.jpeg’)
background_image=pygame.image.load(‘Background_1.jpeg’)
yellow_strip=pygame.image.load(‘Yellow_strip.png’)
strip=pygame.image.load(‘Strip.jpeg’)
car_width=56

def obstacle(obs_startx,obs_starty,obs):
if obs==0:
obs_pic=pygame.image.load(‘car2.jpeg’)
elif obs==1:
obs_pic=pygame.image.load(‘car3.jpeg’)
elif obs==2:
obs_pic=pygame.image.load(‘car4.jpeg’)
elif obs==3:
obs_pic=pygame.image.load(‘car5.jpeg’)
elif obs==4:
obs_pic=pygame.image.load(‘car6.jpeg’)
elif obs==5:
obs_pic=pygame.image.load(‘car7.jpeg’)
game_displays.blit(obs_pic,(obs_startx,obs_starty))

def score_system(passed,score):
font=pygame.font.SysFont(None,25)
text=font.render(“Passed”+str(passed),True,black)
score=font.render(“Score”+str(score),True,red)
game_displays.blit(text,(0,50))
game_displays.blit(score,(0,30))

def text_objects(text,font):
textsurface=font.render(text,True,black)
return textsurface,textsurface.get_rect()

def message_display(text):
largetext=pygame.font.Font(“freesansbold.ttf”,80)
textsurf,textrect=text_objects(text,largetext)
textrect.center=((display_width/2),(display_height/2))
game_displays.blit(textsurf,textrect)
pygame.display.update()
time.sleep(1)
game_loop()

def crash():
message_display(“YOU CRASHED!!”)

def background():
game_displays.blit(background_image,(0,0))
game_displays.blit(background_image,(0,200))
game_displays.blit(background_image,(0,400))
game_displays.blit(background_image,(700,0))
game_displays.blit(background_image,(700,200))
game_displays.blit(background_image,(700,400))
game_displays.blit(yellow_strip,(376,0))
game_displays.blit(yellow_strip,(376,100))
game_displays.blit(yellow_strip,(376,200))
game_displays.blit(yellow_strip,(376,300))
game_displays.blit(yellow_strip,(376,400))
game_displays.blit(yellow_strip,(376,500))
game_displays.blit(strip,(120,0))
game_displays.blit(strip,(120,100))
game_displays.blit(strip,(120,200))
game_displays.blit(strip, (677,0))
game_displays.blit(strip, (677,100))
game_displays.blit(strip, (677,200))

def car(x,y):
game_displays.blit(car_image,(x,y))#blit enters the image on the game screen

def game_loop():
x=(display_width0.47)
y=(display_height
0.8)
x_change=0
obstacle_speed=9
obs=0
y_change=0
obs_startx=random.randrange(200,(display_width-200))
obs_starty=-750
obs_width=56
obs_height=125
car_passed=0
level=0
score=0

bumped=False
while not bumped:
    for event in pygame.event.get():
        if event .type==pygame.QUIT:
            pygame.quit()
            quit()
    if event.type==pygame.KEYDOWN:#keydown just means if some one presses a key
        if event.key==pygame.K_LEFT:
            x_change=-5
        if event.key==pygame.K_RIGHT:
            x_change=5
        if event.key==pygame.K_a:
            obstacle_speed+=2
        if event.key==pygame.K_b:
            obstacle_speed-=2
    if event.type==pygame.KEYUP:#key up just means if no key is pressed so it will not move as thenx_xhange=0
        if event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT:
            x_change=0

    x+=x_change
    game_displays.fill(gray)
    background()
    obs_starty-=(obstacle_speed/4)
    obstacle(obs_startx,obs_starty,obs)
    obs_starty+=obstacle_speed
    car(x,y)
    score_system(passed,score)

    if x>680-car_width or x<110:
        crash()
    if x > display_width - (car_width + 110) or x < 110:
        crash()
    if obs_starty > display_height:
        obs_starty = 0 - obs_height
        obs_startx = random.randrange(170, (display_width - 170))
        obs = random.randrange(0,7)
        passed=passed+1
        score=passed*10
        if int(passed) % 10 == 0:
            level = level + 1
            obstacle_speed + 2
            largetext = pygame.font.Font("freesansbold.ttf", 80)
            textsurf, textrect = text_objects("LEVEL" + str(level), largetext)
            textrect.center = ((display_width / 2), (display_height / 2))
            gamedisplays.blit(textsurf, textrect)
            pygame.display.update()
            time.sleep(3)

    if y<obs_starty+obs_height:
        if x > obs_startx and x < obs_startx + obs_width or x+car_width > obs_startx and x+car_width < obs_startx+obs_width:
            crash()

    pygame.display.update()#update() is to make the display Surface actually appear on the user's monitor.
    #If we will delete the above line only a black screen will appear
    clock.tick(60)

game_loop()
pygame.quit()
quit()

Can you tell me after running this I am getting a UnboundLocalError.
Please tell me the solution

Please don’t send us hundreds of lines of code and expect us to
carefully work through all of it looking for your error.

(It looks like you may have posted the code twice?)

Instead, copy and paste the error message, starting with the line
“Traceback …” to the end. That will show us exactly where the error
comes from.

An UnboundLocalError occurs when you have a local variable inside a
function that is not yet defined. Find the line that fails with
UnboundLocalError and check that any variables are defined.

If you post the traceback, and the function that shows the error
(nothing else) we can probably tell you how to fix it.

I don’t appreciate questions asked such way, so I just read the code and as I see it, the potential area where problem may occur: