Jun-01-2021, 01:37 PM
For your knowledge, the variable
On line 146 of the new version,
The variable
Lines 106 through 117 were indented to place them inside the
Now, the call to
Here you go.
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 ![Wink Wink](https://python-forum.io/images/smilies/wink.png)
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()