import pygame
import sys
import random
import math

# Initialize Pygame
pygame.init()

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
CAR_IMAGE_PATH = 'C:/Users/rajes/Downloads/vector car.png'  # Path to your car image
BACKGROUND_COLOR = (50, 168, 82)
ROAD_COLOR = (50, 50, 50)
LANE_MARKER_COLOR = (255, 255, 255)
CAR_SPEED_INCREMENT = 0.2
CAR_MAX_SPEED = 5
TURN_SPEED = 5
SPAWN_INTERVAL = 1500  # Time (in milliseconds) between obstacle spawns

# Setup screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Endless Road Car Game')

# Load car image
car_image = pygame.image.load(CAR_IMAGE_PATH)
car_image = pygame.transform.scale(car_image, (50, 30))  # Adjust car size

# Clock to control frame rate
clock = pygame.time.Clock()

# Event for spawning obstacles
SPAWN_OBSTACLE = pygame.USEREVENT + 1
pygame.time.set_timer(SPAWN_OBSTACLE, SPAWN_INTERVAL)

# Car class
class Car:
    def __init__(self):
        self.image = car_image
        self.rect = self.image.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT - 100))
        self.angle = 0
        self.speed = 0

    def update(self, keys):
        # Acceleration and deceleration
        if keys[pygame.K_UP]:
            self.speed += CAR_SPEED_INCREMENT
        elif keys[pygame.K_DOWN]:
            self.speed -= CAR_SPEED_INCREMENT
        else:
            # Apply friction when not accelerating
            self.speed *= 0.95

        # Clamp speed
        self.speed = max(0, min(self.speed, CAR_MAX_SPEED))

        # Steering
        if keys[pygame.K_LEFT]:
            self.angle += TURN_SPEED
        if keys[pygame.K_RIGHT]:
            self.angle -= TURN_SPEED

        # Update position
        self.rect.x += self.speed * math.sin(math.radians(self.angle))
        self.rect.y -= self.speed * math.cos(math.radians(self.angle))

        # Keep the car within screen boundaries
        if self.rect.left < 150:  # Left boundary for road
            self.rect.left = 150
        if self.rect.right > SCREEN_WIDTH - 150:  # Right boundary for road
            self.rect.right = SCREEN_WIDTH - 150

    def draw(self):
        rotated_image = pygame.transform.rotate(self.image, -self.angle)  # Rotate image based on angle
        rotated_rect = rotated_image.get_rect(center=self.rect.center)
        screen.blit(rotated_image, rotated_rect.topleft)

# Obstacle class
class Obstacle:
    def __init__(self):
        self.width = random.randint(50, 100)
        self.height = 20
        self.rect = pygame.Rect(random.randint(150, SCREEN_WIDTH - 150 - self.width), -self.height, self.width, self.height)

    def update(self):
        self.rect.y += 5  # Move obstacles down
        if self.rect.top > SCREEN_HEIGHT:
            return True  # Return True if the obstacle is off-screen
        return False

    def draw(self):
        pygame.draw.rect(screen, (200, 0, 0), self.rect)

# Function to draw the curved road
def draw_road():
    # Draw a U-shaped road using arcs
    pygame.draw.rect(screen, ROAD_COLOR, (150, 0, SCREEN_WIDTH - 300, SCREEN_HEIGHT))
    pygame.draw.arc(screen, ROAD_COLOR, (150, 0, SCREEN_WIDTH - 300, 400), math.radians(180), math.radians(360), 10)  # Draw the upper arc
    pygame.draw.arc(screen, ROAD_COLOR, (150, 200, SCREEN_WIDTH - 300, 400), math.radians(0), math.radians(180), 10)  # Draw the lower arc

# Initialize the car and obstacles
car = Car()
obstacles = []

# Main game loop
game_over = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == SPAWN_OBSTACLE and not game_over:
            obstacles.append(Obstacle())

    # Fill the screen with background color
    screen.fill(BACKGROUND_COLOR)

    # Draw the road
    draw_road()

    if not game_over:
        # Get key presses for car control
        keys = pygame.key.get_pressed()
        car.update(keys)

        # Update obstacles
        for obstacle in obstacles[:]:
            if obstacle.update():
                obstacles.remove(obstacle)
            if car.rect.colliderect(obstacle.rect):
                game_over = True

    # Draw the car and obstacles
    car.draw()
    for obstacle in obstacles:
        obstacle.draw()

    # Display game over message
    if game_over:
        font = pygame.font.Font(None, 74)
        game_over_text = font.render("Game Over", True, (255, 0, 0))
        screen.blit(game_over_text, (SCREEN_WIDTH // 2 - game_over_text.get_width() // 2, SCREEN_HEIGHT // 2))

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    clock.tick(60)
. the car is horizontal fix it to face road like a normal car do. make a open world with boundaries on screen i wanna drive car like a real car with my keyboard. the car movments should look like real car in 2d world