STEP Five – A
import time
import random
STEP Five – B
To restructure the code, replace all the code from
## Start point for Step FIVE-B ##
until the end of the file with the following
##########################################################
## ##
## Replace this with Code from Step EIGHT-A ##
## ##
##########################################################
##########################################################
## ##
## Replace this with Code from Step SIX-A ##
## ##
##########################################################
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',115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def crash():
message_display('You Crashed')
def game_loop():
x = (display_width * 0.45)
y = (display_height * 0.8)
x_change = 0
##########################################################
## ##
## Replace this with Code from Step SIX-B ##
## ##
##########################################################
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
gameDisplay.fill(white)
##########################################################
## ##
## Replace this with Code from Step SIX-C ##
## ##
##########################################################
car(x,y)
##########################################################
## ##
## Replace this with Code from Step EIGHT-C ##
## ##
##########################################################
if x > display_width - car_width or x < 0:
crash()
##########################################################
## ##
## Replace this with Code from Step SEVEN ##
## ##
##########################################################
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
quit()
Now run your code
Now, if you crash into the edge, the game will re-start after 2 seconds