import pygame
import random
import sys

# 初始化 Pygame
pygame.init()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
GRAY = (50, 50, 50)
YELLOW = (255, 255, 0)

# 屏幕设置
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("2D 极速赛车")

clock = pygame.time.Clock()

# 游戏变量
car_width = 40
car_height = 70

def draw_car(x, y, color):
    """绘制一个简单的赛车形状"""
    # 车身
    pygame.draw.rect(screen, color, [x, y, car_width, car_height])
    # 车轮
    pygame.draw.rect(screen, BLACK, [x-5, y+10, 5, 15])
    pygame.draw.rect(screen, BLACK, [x+car_width, y+10, 5, 15])
    pygame.draw.rect(screen, BLACK, [x-5, y+45, 5, 15])
    pygame.draw.rect(screen, BLACK, [x+car_width, y+45, 5, 15])

def show_text(msg, size, x, y, color=WHITE):
    font = pygame.font.SysFont("SimHei", size)
    text = font.render(msg, True, color)
    screen.blit(text, (x, y))

def game_loop():
    # 玩家初始位置
    x = SCREEN_WIDTH * 0.45
    y = SCREEN_HEIGHT - car_height - 20
    x_change = 0
    
    # 敌人设置
    enemy_color = RED
    enemy_start_x = random.randrange(0, SCREEN_WIDTH - car_width)
    enemy_start_y = -600
    enemy_speed = 5
    
    score = 0
    game_over = False

    while not game_over:
        # 1. 事件处理
        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_LEFT:
                    x_change = -6
                if event.key == pygame.K_RIGHT:
                    x_change = 6
            
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        # 2. 位置更新
        x += x_change
        
        # 3. 绘制背景和马路
        screen.fill(GRAY)
        # 绘制马路中心线
        for i in range(0, SCREEN_HEIGHT, 40):
            pygame.draw.rect(screen, YELLOW, (SCREEN_WIDTH//2 - 5, i, 10, 20))

        # 4. 绘制并移动敌人
        enemy_start_y += enemy_speed
        draw_car(enemy_start_x, enemy_start_y, enemy_color)
        
        # 5. 绘制玩家
        draw_car(x, y, (0, 120, 255))

        # 6. 边界检测
        if x < 0 or x > SCREEN_WIDTH - car_width:
            game_over = True

        # 7. 敌人循环及加速
        if enemy_start_y > SCREEN_HEIGHT:
            enemy_start_y = 0 - car_height
            enemy_start_x = random.randrange(0, SCREEN_WIDTH - car_width)
            score += 1
            enemy_speed += 0.2 # 逐渐变快

        # 8. 碰撞检测
        if y < enemy_start_y + car_height:
            if (x > enemy_start_x and x < enemy_start_x + car_width) or \
               (x + car_width > enemy_start_x and x + car_width < enemy_start_x + car_width):
                game_over = True

        # 显示分数
        show_text(f"得分: {score}", 25, 10, 10)
        
        pygame.display.update()
        clock.tick(60)

    # 游戏结束界面
    screen.fill(BLACK)
    show_text("游戏结束!", 50, SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 - 50, RED)
    show_text(f"最终得分: {score}", 30, SCREEN_WIDTH//2 - 70, SCREEN_HEIGHT//2 + 20)
    show_text("按 R 重新开始 或 Q 退出", 20, SCREEN_WIDTH//2 - 100, SCREEN_HEIGHT//2 + 80)
    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_r:
                    game_loop()
                if event.key == pygame.K_q:
                    pygame.quit()
                    sys.exit()

if __name__ == "__main__":
    game_loop()