Spaces:
Sleeping
Sleeping
| 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() |