import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 窗口设置
WIDTH, HEIGHT = 400, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("简易驾驶小游戏")

# 颜色定义
BLACK = (0, 0, 0)
GRAY = (60, 60, 60)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 220, 0)

# 车辆玩家参数
car_width = 50
car_height = 90
car_x = WIDTH // 2 - car_width // 2
car_y = HEIGHT - car_height - 20
car_speed = 7

# 障碍物
obs_width = 50
obs_height = 90
obs_list = []
obs_speed = 6
spawn_timer = 0

# 分数字体【修复关键点！】
try:
    font = pygame.font.Font(pygame.font.get_default_font(), 45)
    over_font = pygame.font.Font(pygame.font.get_default_font(), 70)
    tip_font = pygame.font.Font(pygame.font.get_default_font(), 45)
except:
    # 兜底方案，极端环境兼容
    font = pygame.font.SysFont("simhei", 45)
    over_font = pygame.font.SysFont("simhei", 70)
    tip_font = pygame.font.SysFont("simhei", 45)

clock = pygame.time.Clock()
FPS = 60

def draw_player(x, y):
    # 绘制玩家小车
    pygame.draw.rect(screen, GREEN, (x, y, car_width, car_height))
    pygame.draw.rect(screen, YELLOW, (x+8, y+10, 34, 22))

def draw_obstacle(obs):
    x, y = obs
    pygame.draw.rect(screen, RED, (x, y, obs_width, obs_height))

def show_score(score):
    text = font.render(f"分数: {score}", True, WHITE)
    screen.blit(text, (10, 10))

def game_over():
    text = over_font.render("游戏结束", True, RED)
    screen.blit(text, (WIDTH//2 - 120, HEIGHT//2 - 50))
    tip = tip_font.render("按 Q 退出", True, WHITE)
    screen.blit(tip, (WIDTH//2 - 90, HEIGHT//2 + 30))
    pygame.display.update()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    pygame.quit()
                    sys.exit()

score = 0
# 主循环
running = True
while running:
    clock.tick(FPS)
    screen.fill(GRAY)

    # 车道白线
    for y in range(0, HEIGHT, 80):
        pygame.draw.rect(screen, WHITE, (WIDTH//2 - 3, y, 6, 40))

    # 事件监听
    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 > 20:
        car_x -= car_speed
    if keys[pygame.K_RIGHT] and car_x < WIDTH - car_width - 20:
        car_x += car_speed

    # 生成障碍物
    spawn_timer += 1
    if spawn_timer > 45:
        ox = random.randint(20, WIDTH - obs_width - 20)
        obs_list.append([ox, -obs_height])
        spawn_timer = 0

    # 更新障碍物位置
    for o in obs_list[:]:
        o[1] += obs_speed
        if o[1] > HEIGHT:
            obs_list.remove(o)
            score += 1

    # 碰撞检测
    player_rect = pygame.Rect(car_x, car_y, car_width, car_height)
    for o in obs_list:
        obs_rect = pygame.Rect(o[0], o[1], obs_width, obs_height)
        if player_rect.colliderect(obs_rect):
            game_over()

    # 绘制所有元素
    draw_player(car_x, car_y)
    for o in obs_list:
        draw_obstacle(o)
    show_score(score)

    pygame.display.update()

pygame.quit()