Posts: 4
Threads: 1
Joined: May 2021
May-30-2021, 10:55 AM
(This post was last modified: May-30-2021, 12:17 PM by Larz60+.)
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_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
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
Larz60+ write May-30-2021, 12:17 PM:Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Attached Files
Graphics.py (Size: 5.07 KB / Downloads: 325)
Posts: 371
Threads: 2
Joined: Jan 2021
On line 130, you try to use passed in a call to score_system () before it is defined.
On line 148, you try to call gamedisplays before it is defined.
On line 35, obs_pic if referenced before it is assigned.
If you crash, the quit () on line 109 stops pygame but falls through to line 110 generating an error that event is not defined because pygame has already quit.
Posts: 4
Threads: 1
Joined: May 2021
May-31-2021, 06:21 AM
(This post was last modified: May-31-2021, 06:21 AM by arpitmittal1221.)
(May-30-2021, 04:27 PM)BashBedlam Wrote: On line 130, you try to use passed in a call to score_system () before it is defined.
On line 148, you try to call gamedisplays before it is defined.
On line 35, obs_pic if referenced before it is assigned.
If you crash, the quit () on line 109 stops pygame but falls through to line 110 generating an error that event is not defined because pygame has already quit.
Thanks a ton
Please help me solve it.
Attached Files
Graphics.py (Size: 5.07 KB / Downloads: 139)
Posts: 371
Threads: 2
Joined: Jan 2021
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()
Posts: 4
Threads: 1
Joined: May 2021
(May-31-2021, 01:05 PM)BashBedlam Wrote: 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() Thanks a ton for helping me out
I think the changes that you have made are alright but it now closes after it has crashed once unlike before when we could play for ever
Please fix it
Also, could you tell me what had you and will you change just for my knowledge
Thanks a ton again!
Posts: 371
Threads: 2
Joined: Jan 2021
For your knowledge, the variable passed was use before it was defined so I defined it on line 98.
On line 146 of the new version, gamedisplay.update() was changed to game_display.update ()
The variable obs_pic was set to default to car2.jpeg on line 33.
Lines 106 through 117 were indented to place them inside the for loop started at line 102.
Now, the call to game_loop () was put back into the crash function in order not to stop the game after a crash. In my first revision, I recommended that you replace that with some kind of menu or something instead of just automatically starting over, but you're the boss Here you go.
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)
game_loop ()
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()
print ('Here')
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()
Posts: 4
Threads: 1
Joined: May 2021
Jun-01-2021, 02:14 PM
(This post was last modified: Jun-01-2021, 02:14 PM by arpitmittal1221.)
(Jun-01-2021, 01:37 PM)BashBedlam Wrote: For your knowledge, the variable passed was use before it was defined so I defined it on line 98.
On line 146 of the new version, gamedisplay.update() was changed to game_display.update ()
The variable obs_pic was set to default to car2.jpeg on line 33.
Lines 106 through 117 were indented to place them inside the for loop started at line 102.
Now, the call to game_loop () was put back into the crash function in order not to stop the game after a crash. In my first revision, I recommended that you replace that with some kind of menu or something instead of just automatically starting over, but you're the boss Here you go.
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)
game_loop ()
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()
print ('Here')
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() Thanks for the help
Actually, the game is incomplete and that is the reason I haven't created a menu bar so far
I will create it now
Thats all for now .
If I need help will reply here only.
Thank You
|