import pygame
import random
import sys

# 初始化 Pygame
pygame.init()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (200, 0, 0)
BLUE = (0, 0, 255)
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_enemy(en_x, en_y):
    """画敌方车辆"""
    pygame.draw.rect(screen, RED, [en_x, en_y, car_width, car_height])
    # 加一点装饰让它像车
    pygame.draw.rect(screen, BLACK, [en_x+5, en_y-5, 10, 10])
    pygame.draw.rect(screen, BLACK, [en_x+25, en_y-5, 10, 10])


def draw_player(x, y):
    """画玩家车辆"""
    pygame.draw.rect(screen, BLUE, [x, y, car_width, car_height])
    # 挡风玻璃
    pygame.draw.rect(screen, WHITE, [x+5, y+10, 30, 15])


def show_score(score):
    font = pygame.font.SysFont("SimHei", 25)
    text = font.render(f"得分: {score}", True, WHITE)
    screen.blit(text, (10, 10))


def game_loop():
    # 初始位置
    x = SCREEN_WIDTH * 0.45
    y = SCREEN_HEIGHT - 100
    x_change = 0

    # 敌方车辆初始设置
    enemy_speed = 5
    enemy_x = random.randrange(0, SCREEN_WIDTH - car_width)
    enemy_y = -600

    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 = -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

        # 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_y += enemy_speed
        draw_enemy(enemy_x, enemy_y)

        # 5. 绘制玩家
        draw_player(x, y)
        show_score(score)

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

        # 7. 刷出新敌人 & 增加难度
        if enemy_y > SCREEN_HEIGHT:
            enemy_y = 0 - car_height
            enemy_x = random.randrange(0, SCREEN_WIDTH - car_width)
            score += 1
            enemy_speed += 0.2  # 越玩越快

        # 8. 碰撞检测 (矩形重叠算法)
        if y < enemy_y + car_height:
            if (x > enemy_x and x < enemy_x + car_width) or \
               (x + car_width > enemy_x and x + car_width < enemy_x + car_width):
                game_over = True

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

    # 游戏结束显示
    screen.fill(BLACK)
    font = pygame.font.SysFont("SimHei", 40)
    msg = font.render("撞车了! 游戏结束", True, RED)
    screen.blit(msg, (SCREEN_WIDTH//2 - 140, SCREEN_HEIGHT//2))
    pygame.display.update()
    pygame.time.wait(2000)
    game_loop()  # 重新开始


if __name__ == "__main__":
    game_loop()
