import pygame
import random
import sys

pygame.init()
W, H = 400, 600
screen = pygame.display.set_mode((W, H))
pygame.display.set_caption("趣味赛车小游戏")
clock = pygame.time.Clock()

# 配色
SKY_DAY = (50,120,50)
SKY_NIGHT = (20,20,60)
ROAD = (80,80,80)
LINE = (255,220,0)
CAR_COLOR = (220,30,30)
ENEMY_CAR = (30,80,220)
GOLD = (255,210,0)
SHIELD_COLOR = (0,255,255)
ROCK = (70,70,70)

# 玩家车辆参数
car_w, car_h = 30,60
car_x = W//2 - car_w//2
car_y = H - 100
move_speed = 5

# 车道标线
line_list = []
for i in range(8):
    line_list.append([W//2-3, i*120,6,40])

# 各类物体列表
enemy_list = []
gold_list = []
item_list = []
rock_list = []
base_speed = 4
max_speed = 23
score = 0
shield_time = 0
is_night = False

# ======修复字体：改用默认渲染，避开SysFont系统字体BUG======
try:
    font = pygame.font.Font(None, 36)
except:
    font = pygame.font.SysFont("arial",36)

# 生成各类物体函数
def add_car():
    lanes = [60, W//2-car_w//2, W-90]
    x = random.choice(lanes)
    enemy_list.append([x, -80, car_w, car_h])

def add_gold():
    lanes = [65, W//2, W-85]
    x = random.choice(lanes)
    gold_list.append([x, -40,12,12])

def add_shield():
    lanes = [65, W//2, W-85]
    x = random.choice(lanes)
    item_list.append([x,-40,15,15])

def add_rock():
    lanes = [60, W//2-car_w//2, W-90]
    x = random.choice(lanes)
    rock_list.append([x,-60,35,35])

add_car()
running = True

while running:
    # 昼夜背景
    bg_color = SKY_NIGHT if is_night else SKY_DAY
    screen.fill(bg_color)
    pygame.draw.rect(screen,ROAD,(50,0,W-100,H))

    # 随机昼夜切换
    if random.random() < 0.001:
        is_night = not is_night

    # 速度随分数提升
    now_speed = min(base_speed + score//20, max_speed)
    line_spd = base_speed + score//30

    # 绘制车道线
    for line in line_list:
        pygame.draw.rect(screen,LINE,line)
        line[1] += line_spd
        if line[1]>H:
            line[1] = -40

    # 随机刷新道具、障碍、敌车
    if random.random()<0.015:
        add_gold()
    if random.random()<0.005:
        add_shield()
    if random.random()<0.008:
        add_rock()
    if random.random()<0.008 and len(enemy_list)<5:
        add_car()

    # 敌方小车
    for car in enemy_list[:]:
        pygame.draw.rect(screen,ENEMY_CAR,car)
        car[1] += now_speed
        if random.random()<0.002:
            car[1] += 6
        if car[1]>H:
            enemy_list.remove(car)
            score +=10

    # 金币绘制
    for g in gold_list[:]:
        pygame.draw.rect(screen,GOLD,g)
        g[1] += now_speed
        if g[1]>H:
            gold_list.remove(g)

    # 护盾道具
    for it in item_list[:]:
        pygame.draw.rect(screen,SHIELD_COLOR,it)
        it[1] += now_speed
        if it[1]>H:
            item_list.remove(it)

    # 石头障碍
    for rk in rock_list[:]:
        pygame.draw.rect(screen,ROCK,rk)
        rk[1] += now_speed
        if rk[1]>H:
            rock_list.remove(rk)

    # 护盾外圈特效
    if shield_time>0:
        shield_time -=1
        pygame.draw.rect(screen,SHIELD_COLOR,(car_x-3,car_y-3,car_w+6,car_h+6),2)

    # 玩家小车
    pygame.draw.rect(screen,CAR_COLOR,(car_x,car_y,car_w,car_h))

    # 退出事件
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            running = False

    # 左右按键移动
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and car_x>52:
        car_x -= move_speed
    if keys[pygame.K_RIGHT] and car_x<W-50-car_w:
        car_x += move_speed

    player_rect = pygame.Rect(car_x,car_y,car_w,car_h)

    # 拾取金币加分
    for g in gold_list[:]:
        if player_rect.colliderect(pygame.Rect(g)):
            score +=25
            gold_list.remove(g)
    # 拾取护盾
    for it in item_list[:]:
        if player_rect.colliderect(pygame.Rect(it)):
            shield_time = 300
            item_list.remove(it)

    # 碰撞检测
    crash = False
    for obj in enemy_list+rock_list:
        if player_rect.colliderect(pygame.Rect(obj)):
            if shield_time<=0:
                crash = True
                break
    if crash:
        print(f"撞毁！得分：{score}")
        running = False

    # 显示分数
    scr_text = font.render(f"Score:{score}",True,(255,255,255))
    screen.blit(scr_text,(10,10))

    pygame.display.update()
    clock.tick(60)

pygame.quit()
sys.exit()