import pygame
import random

# 初始化 Pygame
pygame.init()

# 游戏常量
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 400
FPS = 60

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 50, 50)
GREEN = (50, 200, 50)
SKY_BLUE = (135, 206, 235)

# 创建窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Python 极简跑酷")
clock = pygame.time.Clock()

class Player:
    def __init__(self):
        self.width = 40
        self.height = 60
        self.x = 100
        self.y = SCREEN_HEIGHT - self.height - 20
        self.vel_y = 0
        self.jump_power = -15
        self.gravity = 0.8
        self.is_jumping = False

    def jump(self):
        if not self.is_jumping:
            self.vel_y = self.jump_power
            self.is_jumping = True

    def update(self):
        # 重力逻辑
        self.vel_y += self.gravity
        self.y += self.vel_y

        # 地面检测
        ground_y = SCREEN_HEIGHT - self.height - 20
        if self.y >= ground_y:
            self.y = ground_y
            self.vel_y = 0
            self.is_jumping = False

    def draw(self):
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))

class Obstacle:
    def __init__(self, speed):
        self.width = 30
        self.height = random.randint(40, 80)
        self.x = SCREEN_WIDTH
        self.y = SCREEN_HEIGHT - self.height - 20
        self.speed = speed

    def update(self):
        self.x -= self.speed

    def draw(self):
        pygame.draw.rect(screen, GREEN, (self.x, self.y, self.width, self.height))

def main():
    player = Player()
    obstacles = []
    score = 0
    game_speed = 7
    spawn_timer = 0
    font = pygame.font.SysFont("SimHei", 30)
    
    running = True
    game_over = False

    while running:
        # 1. 事件处理
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if game_over: # 重新开始
                        main()
                        return
                    player.jump()

        if not game_over:
            # 2. 更新逻辑
            player.update()

            # 生成障碍物 (随机间隔)
            spawn_timer -= 1
            if spawn_timer <= 0:
                obstacles.append(Obstacle(game_speed))
                spawn_timer = random.randint(40, 90)

            # 更新障碍物
            for obs in obstacles[:]:
                obs.update()
                # 碰撞检测
                player_rect = pygame.Rect(player.x, player.y, player.width, player.height)
                obs_rect = pygame.Rect(obs.x, obs.y, obs.width, obs.height)
                if player_rect.colliderect(obs_rect):
                    game_over = True
                
                # 移除屏幕外的障碍物并加分
                if obs.x + obs.width < 0:
                    obstacles.remove(obs)
                    score += 1
                    # 逐渐增加难度
                    if score % 5 == 0:
                        game_speed += 0.2

            # 3. 渲染绘制
            screen.fill(SKY_BLUE) # 背景色
            
            # 画地面
            pygame.draw.rect(screen, BLACK, (0, SCREEN_HEIGHT-20, SCREEN_WIDTH, 20))
            
            player.draw()
            for obs in obstacles:
                obs.draw()

            # 显示分数
            score_text = font.render(f"分数: {score}", True, BLACK)
            screen.blit(score_text, (10, 10))
        
        else:
            # 游戏结束界面
            over_text = font.render("游戏结束! 按空格键重新开始", True, BLACK)
            screen.blit(over_text, (SCREEN_WIDTH//2 - 150, SCREEN_HEIGHT//2))

        pygame.display.flip()
        clock.tick(FPS)

    pygame.quit()

if __name__ == "__main__":
    main()