import pygame
import random
# Initialize Pygame
pygame.init()
# Game Window Dimensions
WIDTH, HEIGHT = 800, 600
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Road Warrior: The Speed Chase")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# Car Settings
CAR_WIDTH = 50
CAR_HEIGHT = 100
car_x = WIDTH // 2 - CAR_WIDTH // 2
car_y = HEIGHT - CAR_HEIGHT - 10
car_speed = 5
# Game Clock
clock = pygame.time.Clock()
# Load Car Image (You can replace this with your own car image)
car_img = pygame.Surface((CAR_WIDTH, CAR_HEIGHT))
car_img.fill(RED)
# Function to draw the car
def draw_car(x, y):
window.blit(car_img, (x, y))
# Function to draw the road
def draw_road():
pygame.draw.rect(window, WHITE, (0, 0, WIDTH, HEIGHT))
pygame.draw.rect(window, BLACK, (WIDTH // 4, 0, WIDTH // 2, HEIGHT))
# Game Loop
def game_loop():
global car_x, car_y, car_speed
# Game Variables
run_game = True
x_velocity = 0
while run_game:
window.fill(WHITE)
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_game = False
# Key Press Handling for Car Movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x_velocity = -car_speed
elif keys[pygame.K_RIGHT]:
x_velocity = car_speed
else:
x_velocity = 0
# Update Car Position
car_x += x_velocity
# Prevent the car from going off-screen
if car_x < WIDTH // 4:
car_x = WIDTH // 4
if car_x > WIDTH - CAR_WIDTH - WIDTH // 4:
car_x = WIDTH - CAR_WIDTH - WIDTH // 4
# Draw Road and Car
draw_road()
draw_car(car_x, car_y)
# Update the Display
pygame.display.update()
# Frame Rate (FPS)
clock.tick(60)
pygame.quit()
quit()
# Start the Game
game_loop()