pip install pygame import pygame import sys import random import time pygame.init() WIDTH, HEIGHT = 800, 600 FPS = 60 WHITE = (255, 255, 255) screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("2.5D Racing Game") car_img = pygame.image.load("car.png") car_img = pygame.transform.scale(car_img, (50, 100)) car_x = WIDTH // 2 - 25 car_y = HEIGHT - 120 car_speed = 5 road_img = pygame.image.load("road.png") road_img = pygame.transform.scale(road_img, (WIDTH, HEIGHT)) road_y = 0 road_speed = 5 obstacle_img = pygame.image.load("obstacle_car.png") obstacle_img = pygame.transform.scale(obstacle_img, (50, 100)) obstacles = [] obstacle_speed = 7 last_spawn_time = time.time() clock = pygame.time.Clock() def draw_car(x, y): screen.blit(car_img, (x, y)) def draw_road(): global road_y road_y += road_speed if road_y >= HEIGHT: road_y = 0 screen.blit(road_img, (0, road_y)) screen.blit(road_img, (0, road_y - HEIGHT)) def spawn_obstacle(): x = random.randint(50, WIDTH - 100) y = -100 obstacles.append([x, y]) def move_obstacles(): for obs in obstacles: obs[1] += obstacle_speed screen.blit(obstacle_img, (obs[0], obs[1])) if obs[1] > HEIGHT: obstacles.remove(obs) def check_collision(): car_rect = pygame.Rect(car_x, car_y, 50, 100) for obs in obstacles: obs_rect = pygame.Rect(obs[0], obs[1], 50, 100) if car_rect.colliderect(obs_rect): return True return False def game_loop(): global car_x, road_y, last_spawn_time running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and car_x > 0: car_x -= car_speed if keys[pygame.K_RIGHT] and car_x < WIDTH - 50: car_x += car_speed screen.fill(WHITE) draw_road() draw_car(car_x, car_y) if time.time() - last_spawn_time > 1: spawn_obstacle() last_spawn_time = time.time() move_obstacles() if check_collision(): print("Game Over!") running = False pygame.display.flip() clock.tick(FPS) pygame.quit() sys.exit() if __name__ == "__main__": game_loop()