Python Forum
[PyGame] UnboundLocalError
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] UnboundLocalError
#4
I have modified your code and it seems to be working as expected. On line 60, the game just quits if a crash occurs. I would recommend replacing that with some sort of options to replay from the start or exit the program.

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

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')
    else : obs_pic=pygame.image.load('car2.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)
 
def crash():
    message_display("YOU CRASHED!!")
    time.sleep (2) 
    quit () # Change this to actions to take in the event of a crash.
 
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_width*0.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; passed = 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))
                game_displays.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()
Reply


Messages In This Thread
UnboundLocalError - by arpitmittal1221 - May-30-2021, 10:55 AM
RE: UnboundLocalError - by BashBedlam - May-30-2021, 04:27 PM
RE: UnboundLocalError - by arpitmittal1221 - May-31-2021, 06:21 AM
RE: UnboundLocalError - by BashBedlam - May-31-2021, 01:05 PM
RE: UnboundLocalError - by arpitmittal1221 - Jun-01-2021, 06:18 AM
RE: UnboundLocalError - by BashBedlam - Jun-01-2021, 01:37 PM
RE: UnboundLocalError - by arpitmittal1221 - Jun-01-2021, 02:14 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020