File size: 4,875 Bytes
2d429d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Constants
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
ROAD_WIDTH = 400
CAR_WIDTH, CAR_HEIGHT = 50, 100
FPS = 60

# Colors
WHITE = (255, 255, 255)
GRAY = (50, 50, 50)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)

# Initialize screen
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Car Game")

# Clock
clock = pygame.time.Clock()

# Font
font = pygame.font.Font(None, 36)

# Car setup
player_car = pygame.Rect(WINDOW_WIDTH // 2 - CAR_WIDTH // 2, WINDOW_HEIGHT - CAR_HEIGHT - 20, CAR_WIDTH, CAR_HEIGHT)
car_speed = 8

# Traffic cars
traffic = []
TRAFFIC_INTERVAL = 2000  # Time in milliseconds
last_traffic_spawn = pygame.time.get_ticks()

# Dashed road lines
line_height = 40
line_spacing = 20
lines = [(WINDOW_WIDTH // 2 - 5, y) for y in range(0, WINDOW_HEIGHT, line_height + line_spacing)]

# Score
score = 0
high_score = 0

def draw_text(surface, text, x, y, color):
    """Draw text on the screen."""
    text_surface = font.render(text, True, color)
    surface.blit(text_surface, (x, y))

# Game loop
running = True
game_over = False
start_game = False

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if not start_game:
        # Start screen
        screen.fill(BLACK)
        draw_text(screen, "Welcome to Car Game!", WINDOW_WIDTH // 2 - 150, WINDOW_HEIGHT // 2 - 50, WHITE)
        draw_text(screen, "Press SPACE to Start", WINDOW_WIDTH // 2 - 150, WINDOW_HEIGHT // 2, WHITE)
        pygame.display.flip()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            start_game = True

    elif not game_over:
        # Controls
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w] or keys[pygame.K_UP]:
            player_car.y -= car_speed
        if keys[pygame.K_s] or keys[pygame.K_DOWN]:
            player_car.y += car_speed
        if keys[pygame.K_a] or keys[pygame.K_LEFT]:
            player_car.x -= car_speed
        if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
            player_car.x += car_speed

        # Keep car within the road boundaries
        player_car.x = max(WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, min(player_car.x, WINDOW_WIDTH // 2 + ROAD_WIDTH // 2 - CAR_WIDTH))
        player_car.y = max(0, min(player_car.y, WINDOW_HEIGHT - CAR_HEIGHT))

        # Spawn traffic cars
        current_time = pygame.time.get_ticks()
        if current_time - last_traffic_spawn > TRAFFIC_INTERVAL:
            num_cars = random.randint(1, 3)  # Randomly spawn 1 to 3 cars
            for _ in range(num_cars):
                traffic_x = random.randint(WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, WINDOW_WIDTH // 2 + ROAD_WIDTH // 2 - CAR_WIDTH)
                speed = random.randint(3, 8)  # Random speed for each car
                traffic.append({'rect': pygame.Rect(traffic_x, -CAR_HEIGHT, CAR_WIDTH, CAR_HEIGHT), 'speed': speed})
            last_traffic_spawn = current_time

        # Move traffic cars
        for car in traffic:
            car['rect'].y += car['speed']
        traffic = [car for car in traffic if car['rect'].y < WINDOW_HEIGHT]

        # Check collisions
        for car in traffic:
            if player_car.colliderect(car['rect']):
                game_over = True
                break

        # Update score
        score += 1

    # Drawing everything
    screen.fill(BLACK)  # Background
    pygame.draw.rect(screen, GRAY, (WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, 0, ROAD_WIDTH, WINDOW_HEIGHT))  # Road

    # Draw road lines
    for i, (x, y) in enumerate(lines):
        pygame.draw.rect(screen, YELLOW, (x, y, 10, line_height))
        lines[i] = (x, (y + 5) % (WINDOW_HEIGHT + line_height + line_spacing) - line_spacing)

    # Draw traffic cars
    for car in traffic:
        pygame.draw.rect(screen, BLUE, car['rect'])

    # Draw player car
    pygame.draw.rect(screen, RED, player_car)

    # Draw score
    if start_game:
        draw_text(screen, f"Score: {score}", 10, 10, WHITE)
    if game_over:
        draw_text(screen, "GAME OVER", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 40, WHITE)
        draw_text(screen, f"Your Score: {score}", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2, WHITE)
        draw_text(screen, "Press R to Restart", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 + 40, WHITE)

    # Update the display
    pygame.display.flip()

    # Restart or quit
    if game_over:
        keys = pygame.key.get_pressed()
        if keys[pygame.K_r]:
            game_over = False
            player_car.x = WINDOW_WIDTH // 2 - CAR_WIDTH // 2
            player_car.y = WINDOW_HEIGHT - CAR_HEIGHT - 20
            traffic.clear()
            score = 0

    # Cap the frame rate
    clock.tick(FPS)

# Quit Pygame
pygame.quit()
sys.exit()