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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pygame
|
| 2 |
+
import sys
|
| 3 |
+
import random
|
| 4 |
+
|
| 5 |
+
# Initialize Pygame
|
| 6 |
+
pygame.init()
|
| 7 |
+
|
| 8 |
+
# Constants
|
| 9 |
+
WINDOW_WIDTH, WINDOW_HEIGHT = 800, 600
|
| 10 |
+
ROAD_WIDTH = 400
|
| 11 |
+
CAR_WIDTH, CAR_HEIGHT = 50, 100
|
| 12 |
+
FPS = 60
|
| 13 |
+
|
| 14 |
+
# Colors
|
| 15 |
+
WHITE = (255, 255, 255)
|
| 16 |
+
GRAY = (50, 50, 50)
|
| 17 |
+
BLACK = (0, 0, 0)
|
| 18 |
+
RED = (255, 0, 0)
|
| 19 |
+
YELLOW = (255, 255, 0)
|
| 20 |
+
BLUE = (0, 0, 255)
|
| 21 |
+
GREEN = (0, 255, 0)
|
| 22 |
+
PURPLE = (128, 0, 128)
|
| 23 |
+
WINDOW_COLOR = (200, 200, 200)
|
| 24 |
+
HEADLIGHT_COLOR = (255, 255, 100)
|
| 25 |
+
WHEEL_COLOR = (30, 30, 30)
|
| 26 |
+
|
| 27 |
+
# Initialize screen
|
| 28 |
+
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
|
| 29 |
+
pygame.display.set_caption("Car Game")
|
| 30 |
+
|
| 31 |
+
# Clock
|
| 32 |
+
clock = pygame.time.Clock()
|
| 33 |
+
|
| 34 |
+
# Font
|
| 35 |
+
font = pygame.font.Font(None, 36)
|
| 36 |
+
|
| 37 |
+
# Car setup
|
| 38 |
+
player_car = pygame.Rect(WINDOW_WIDTH // 2 - CAR_WIDTH // 2, WINDOW_HEIGHT - CAR_HEIGHT - 20, CAR_WIDTH, CAR_HEIGHT)
|
| 39 |
+
car_speed = 8
|
| 40 |
+
|
| 41 |
+
# Traffic cars
|
| 42 |
+
traffic = []
|
| 43 |
+
TRAFFIC_INTERVAL = 2000 # Time in milliseconds
|
| 44 |
+
last_traffic_spawn = pygame.time.get_ticks()
|
| 45 |
+
|
| 46 |
+
# Dashed road lines
|
| 47 |
+
line_height = 40
|
| 48 |
+
line_spacing = 20
|
| 49 |
+
lines = [(WINDOW_WIDTH // 2 - 5, y) for y in range(0, WINDOW_HEIGHT, line_height + line_spacing)]
|
| 50 |
+
|
| 51 |
+
# Score
|
| 52 |
+
score = 0
|
| 53 |
+
high_score = 0
|
| 54 |
+
|
| 55 |
+
TRAFFIC_COLORS = [BLUE, GREEN, PURPLE]
|
| 56 |
+
|
| 57 |
+
def draw_text(surface, text, x, y, color):
|
| 58 |
+
"""Draw text on the screen."""
|
| 59 |
+
text_surface = font.render(text, True, color)
|
| 60 |
+
surface.blit(text_surface, (x, y))
|
| 61 |
+
|
| 62 |
+
def draw_car(surface, car_rect, color, detailed=False):
|
| 63 |
+
"""Draw a car with details."""
|
| 64 |
+
pygame.draw.rect(surface, color, car_rect) # Base rectangle for the car
|
| 65 |
+
# Adding headlights
|
| 66 |
+
pygame.draw.rect(surface, YELLOW, (car_rect.x + 5, car_rect.y, 10, 10))
|
| 67 |
+
pygame.draw.rect(surface, YELLOW, (car_rect.x + car_rect.width - 15, car_rect.y, 10, 10))
|
| 68 |
+
# Adding wheels
|
| 69 |
+
pygame.draw.rect(surface, BLACK, (car_rect.x + 5, car_rect.y + car_rect.height - 10, 10, 10))
|
| 70 |
+
pygame.draw.rect(surface, BLACK, (car_rect.x + car_rect.width - 15, car_rect.y + car_rect.height - 10, 10, 10))
|
| 71 |
+
|
| 72 |
+
# Game loop
|
| 73 |
+
running = True
|
| 74 |
+
game_over = False
|
| 75 |
+
start_game = False
|
| 76 |
+
|
| 77 |
+
while running:
|
| 78 |
+
for event in pygame.event.get():
|
| 79 |
+
if event.type == pygame.QUIT:
|
| 80 |
+
running = False
|
| 81 |
+
|
| 82 |
+
if not start_game:
|
| 83 |
+
# Start screen
|
| 84 |
+
screen.fill(BLACK)
|
| 85 |
+
draw_text(screen, "Welcome to Car Game!", WINDOW_WIDTH // 2 - 150, WINDOW_HEIGHT // 2 - 50, WHITE)
|
| 86 |
+
draw_text(screen, "Press P to Start", WINDOW_WIDTH // 2 - 150, WINDOW_HEIGHT // 2, WHITE)
|
| 87 |
+
pygame.display.flip()
|
| 88 |
+
|
| 89 |
+
keys = pygame.key.get_pressed()
|
| 90 |
+
if keys[pygame.K_p]:
|
| 91 |
+
start_game = True
|
| 92 |
+
|
| 93 |
+
elif not game_over:
|
| 94 |
+
# Controls
|
| 95 |
+
keys = pygame.key.get_pressed()
|
| 96 |
+
if keys[pygame.K_w] or keys[pygame.K_UP]:
|
| 97 |
+
player_car.y -= car_speed
|
| 98 |
+
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
|
| 99 |
+
player_car.y += car_speed
|
| 100 |
+
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
|
| 101 |
+
player_car.x -= car_speed
|
| 102 |
+
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
|
| 103 |
+
player_car.x += car_speed
|
| 104 |
+
|
| 105 |
+
# Keep car within the road boundaries
|
| 106 |
+
player_car.x = max(WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, min(player_car.x, WINDOW_WIDTH // 2 + ROAD_WIDTH // 2 - CAR_WIDTH))
|
| 107 |
+
player_car.y = max(0, min(player_car.y, WINDOW_HEIGHT - CAR_HEIGHT))
|
| 108 |
+
|
| 109 |
+
# Spawn traffic cars
|
| 110 |
+
current_time = pygame.time.get_ticks()
|
| 111 |
+
if current_time - last_traffic_spawn > TRAFFIC_INTERVAL:
|
| 112 |
+
num_cars = random.randint(1, 3) # Randomly spawn 1 to 3 cars
|
| 113 |
+
for _ in range(num_cars):
|
| 114 |
+
traffic_x = random.randint(WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, WINDOW_WIDTH // 2 + ROAD_WIDTH // 2 - CAR_WIDTH)
|
| 115 |
+
speed = random.randint(3, 8) # Random speed for each car
|
| 116 |
+
color = random.choice(TRAFFIC_COLORS) # Random color for each car
|
| 117 |
+
traffic.append({'rect': pygame.Rect(traffic_x, -CAR_HEIGHT, CAR_WIDTH, CAR_HEIGHT), 'speed': speed, 'color': color})
|
| 118 |
+
last_traffic_spawn = current_time
|
| 119 |
+
|
| 120 |
+
# Move traffic cars
|
| 121 |
+
for car in traffic:
|
| 122 |
+
car['rect'].y += car['speed']
|
| 123 |
+
traffic = [car for car in traffic if car['rect'].y < WINDOW_HEIGHT]
|
| 124 |
+
|
| 125 |
+
# Check collisions
|
| 126 |
+
for car in traffic:
|
| 127 |
+
if player_car.colliderect(car['rect']):
|
| 128 |
+
game_over = True
|
| 129 |
+
break
|
| 130 |
+
|
| 131 |
+
# Update score
|
| 132 |
+
score += 1
|
| 133 |
+
|
| 134 |
+
# Drawing everything
|
| 135 |
+
screen.fill(BLACK) # Background
|
| 136 |
+
pygame.draw.rect(screen, GRAY, (WINDOW_WIDTH // 2 - ROAD_WIDTH // 2, 0, ROAD_WIDTH, WINDOW_HEIGHT)) # Road
|
| 137 |
+
|
| 138 |
+
# Draw road lines
|
| 139 |
+
for i, (x, y) in enumerate(lines):
|
| 140 |
+
pygame.draw.rect(screen, YELLOW, (x, y, 10, line_height))
|
| 141 |
+
lines[i] = (x, (y + 5) % (WINDOW_HEIGHT + line_height + line_spacing) - line_spacing)
|
| 142 |
+
|
| 143 |
+
# Draw traffic cars
|
| 144 |
+
for car in traffic:
|
| 145 |
+
draw_car(screen, car['rect'], car['color'], detailed=True)
|
| 146 |
+
|
| 147 |
+
# Draw player car
|
| 148 |
+
draw_car(screen, player_car, RED, detailed=True)
|
| 149 |
+
|
| 150 |
+
# Draw score
|
| 151 |
+
if start_game:
|
| 152 |
+
draw_text(screen, f"Score: {score}", 10, 10, WHITE)
|
| 153 |
+
if game_over:
|
| 154 |
+
draw_text(screen, "GAME OVER", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 40, WHITE)
|
| 155 |
+
draw_text(screen, f"Your Score: {score}", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2, WHITE)
|
| 156 |
+
draw_text(screen, "Press R to Restart", WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 + 40, WHITE)
|
| 157 |
+
|
| 158 |
+
# Update the display
|
| 159 |
+
pygame.display.flip()
|
| 160 |
+
|
| 161 |
+
# Restart or quit
|
| 162 |
+
if game_over:
|
| 163 |
+
keys = pygame.key.get_pressed()
|
| 164 |
+
if keys[pygame.K_r]:
|
| 165 |
+
game_over = False
|
| 166 |
+
player_car.x = WINDOW_WIDTH // 2 - CAR_WIDTH // 2
|
| 167 |
+
player_car.y = WINDOW_HEIGHT - CAR_HEIGHT - 20
|
| 168 |
+
traffic.clear()
|
| 169 |
+
score = 0
|
| 170 |
+
|
| 171 |
+
# Cap the frame rate
|
| 172 |
+
clock.tick(FPS)
|
| 173 |
+
|
| 174 |
+
# Quit Pygame
|
| 175 |
+
pygame.quit()
|
| 176 |
+
sys.exit()
|