Import pygame-WPS Office
Import pygame-WPS Office
import random
pygame.init()
# Define colors
BLACK = (0, 0, 0)
RED = (255, 0, 0)
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 600
CAR_WIDTH = 50
CAR_HEIGHT = 100
FPS = 60
clock = pygame.time.Clock()
def draw_finish_line():
def display_message(text):
game_display.blit(text_surface, text_rect)
pygame.display.update()
pygame.time.wait(2000)
# Function to display score
def display_score(score):
def crash():
display_message("You Crashed!")
game_loop()
def win():
display_message("You Win!")
game_loop()
def game_loop():
car_x_change = 0
# Obstacle parameters
obstacle_y = -600
obstacle_speed = 7
obstacle_width = 100
obstacle_height = 100
# Initial score
score = 0
game_exit = False
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
car_x_change = -5
if event.key == pygame.K_RIGHT:
car_x_change = 5
if event.type == pygame.KEYUP:
car_x_change = 0
car_x += car_x_change
game_display.fill(WHITE)
draw_finish_line()
# Draw obstacles
obstacle_y += obstacle_speed
draw_car(car_x, car_y)
display_score(score)
# Check for collisions
crash()
obstacle_y = 0 - obstacle_height
car_x + CAR_WIDTH > obstacle_x and car_x + CAR_WIDTH < obstacle_x + obstacle_width:
crash()
win()
pygame.display.update()
clock.tick(FPS)
game_loop()
# Quit pygame
pygame.quit()
quit()