import pygame
import sys
import random

# 初始化配置
pygame.init()
SCREEN_W = 500
SCREEN_H = 720
screen = pygame.display.set_mode((SCREEN_W, SCREEN_H))
pygame.display.set_caption("2D公路赛车")
clock = pygame.time.Clock()
font = pygame.font.SysFont("simhei", 22)

# 颜色
GRASS = (30, 110, 30)
ROAD = (55, 55, 55)
YELLOW_LINE = (255, 215, 0)
PLAYER_CAR = (220, 20, 20)
ENEMY_CAR = (20, 20, 220)

# 道路设置
road_width = 320
road_x_start = (SCREEN_W - road_width) // 2
line_w = 7
line_h = 50
line_gap = 110

# 车道虚线初始化
line_group = []
for n in range(9):
    line_group.append([SCREEN_W//2 - line_w//2, -n * line_gap])

# 玩家车辆
p_car_w = 38
p_car_h = 65
p_x = SCREEN_W//2 - p_car_w//2
p_y = SCREEN_H - 130
speed = 6
max_speed = 15
min_speed = 3

# 敌方车辆列表
enemy_list = []
enemy_w = 36
enemy_h = 62
spawn_rate = 1800  # 生成敌车间隔(毫秒)
last_spawn = pygame.time.get_ticks()

score = 0

def spawn_enemy():
    """随机在车道生成敌方车"""
    lanes = [road_x_start+30, road_x_start+road_width//2-enemy_w//2, road_x_start+road_width-30-enemy_w]
    ex = random.choice(lanes)
    ey = -enemy_h
    es = random.randint(7, 11)
    enemy_list.append([ex, ey, es])

running = True
while running:
    screen.fill(GRASS)
    # 画马路
    pygame.draw.rect(screen, ROAD, (road_x_start, 0, road_width, SCREEN_H))

    # 更新车道线
    for line in line_group:
        line[1] += speed
        if line[1] > SCREEN_H:
            line[1] = -line_gap
        pygame.draw.rect(screen, YELLOW_LINE, (line[0], line[1], line_w, line_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 p_x > road_x_start + 5:
        p_x -= 6
    if keys[pygame.K_RIGHT] and p_x+p_car_w < road_x_start+road_width -5:
        p_x +=6
    # 加减速
    if keys[pygame.K_UP] and speed < max_speed:
        speed +=0.3
    if keys[pygame.K_DOWN] and speed > min_speed:
        speed -=0.3

    # 路边越界复位
    if p_x <= road_x_start or p_x+p_car_w >= road_x_start+road_width:
        p_x = SCREEN_W//2 - p_car_w//2
        speed = 6

    # 定时生成敌方车辆
    now = pygame.time.get_ticks()
    if now - last_spawn > spawn_rate:
        spawn_enemy()
        last_spawn = now

    # 更新敌方车位置+碰撞检测
    player_rect = pygame.Rect(p_x, p_y, p_car_w, p_car_h)
    for car in enemy_list[:]:
        car[1] += car[2]
        car_rect = pygame.Rect(car[0], car[1], enemy_w, enemy_h)
        # 相撞重置游戏
        if player_rect.colliderect(car_rect):
            p_x = SCREEN_W//2 - p_car_w//2
            speed =6
            enemy_list.clear()
            score = 0
        # 开出屏幕删除
        if car[1] > SCREEN_H:
            enemy_list.remove(car)
            score += int(speed*10)
        pygame.draw.rect(screen, ENEMY_CAR, car_rect, border_radius=6)

    # 绘制玩家车
    pygame.draw.rect(screen, PLAYER_CAR, (p_x,p_y,p_car_w,p_car_h), border_radius=7)

    # 显示分数和车速
    text1 = font.render(f"分数:{score}", True, (255,255,255))
    text2 = font.render(f"车速:{round(speed,1)}", True, (255,255,255))
    screen.blit(text1, (15,15))
    screen.blit(text2, (15,45))

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

pygame.quit()
sys.exit()