import pygame
import random
import sys

# --- 1. 初始化 Pygame ---
pygame.init()

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

# 屏幕设置
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("极速狂飙 - 驾驶小游戏")

clock = pygame.time.Clock()

# --- 2. 游戏对象设置 ---
player_width = 50
player_height = 80

# 字体设置
font = pygame.font.SysFont("SimHei", 30)

def draw_text(text, x, y, color=WHITE):
    img = font.render(text, True, color)
    screen.blit(img, (x, y))

def game_loop():
    # 玩家初始位置
    x = (SCREEN_WIDTH * 0.45)
    y = (SCREEN_HEIGHT * 0.8)
    x_change = 0
    
    # 障碍物设置
    enemy_width = 50
    enemy_height = 80
    enemy_startx = random.randrange(0, SCREEN_WIDTH - enemy_width)
    enemy_starty = -600
    enemy_speed = 5
    
    score = 0
    game_exit = False

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

        # 更新玩家位置
        x += x_change
        
        # --- 4. 逻辑处理 ---
        screen.fill(GRAY) # 绘制公路背景
        
        # 绘制公路中心线
        for i in range(0, SCREEN_HEIGHT, 40):
            pygame.draw.rect(screen, YELLOW, (SCREEN_WIDTH//2 - 5, i, 10, 20))

        # 绘制并更新障碍物
        enemy_starty += enemy_speed
        # 绘制敌方车辆 (红色矩形代替)
        pygame.draw.rect(screen, RED, [enemy_startx, enemy_starty, enemy_width, enemy_height])
        
        # 绘制玩家车辆 (蓝色矩形代替)
        pygame.draw.rect(screen, BLUE, [x, y, player_width, player_height])

        # 边界检测
        if x < 0 or x > SCREEN_WIDTH - player_width:
            game_over(score)

        # 障碍物循环与计分
        if enemy_starty > SCREEN_HEIGHT:
            enemy_starty = 0 - enemy_height
            enemy_startx = random.randrange(0, SCREEN_WIDTH - enemy_width)
            score += 1
            enemy_speed += 0.2 # 逐渐加速

        # 碰撞检测
        if y < enemy_starty + enemy_height:
            if x > enemy_startx and x < enemy_startx + enemy_width or \
               x + player_width > enemy_startx and x + player_width < enemy_startx + enemy_width:
                game_over(score)

        draw_text(f"得分: {score}", 10, 10)
        
        pygame.display.update()
        clock.tick(60)

def game_over(score):
    while True:
        screen.fill(BLACK)
        draw_text("游戏结束!", SCREEN_WIDTH//2 - 60, SCREEN_HEIGHT//3, RED)
        draw_text(f"最终得分: {score}", SCREEN_WIDTH//2 - 70, SCREEN_HEIGHT//2)
        draw_text("按 R 重新开始 或 Q 退出", SCREEN_WIDTH//2 - 150, SCREEN_HEIGHT*0.7)
        
        pygame.display.update()
        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()