import pygame
import random
import sys

# 初始化pygame
pygame.init()

# 游戏窗口设置
WIDTH, HEIGHT = 800, 400
FPS = 60
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("跑酷小游戏")
clock = pygame.time.Clock()

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (135, 206, 235)
GRAY = (200, 200, 200)
BROWN = (139, 69, 19)

# 玩家类
class Player:
    def __init__(self):
        self.width = 40
        self.height = 60
        self.x = 100
        self.y = HEIGHT - 150
        self.vel_y = 0
        self.jumping = False
        self.jump_power = 15
        self.gravity = 0.8
        self.color = GREEN
        
    def jump(self):
        if not self.jumping:
            self.vel_y = -self.jump_power
            self.jumping = True
            
    def update(self):
        # 应用重力
        self.vel_y += self.gravity
        self.y += self.vel_y
        
        # 地面碰撞检测
        if self.y >= HEIGHT - 150:
            self.y = HEIGHT - 150
            self.jumping = False
            self.vel_y = 0
            
    def draw(self, surface):
        # 绘制玩家角色
        pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
        # 眼睛
        pygame.draw.circle(surface, WHITE, (self.x + 10, self.y + 15), 5)
        pygame.draw.circle(surface, WHITE, (self.x + 30, self.y + 15), 5)
        pygame.draw.circle(surface, BLACK, (self.x + 10, self.y + 15), 2)
        pygame.draw.circle(surface, BLACK, (self.x + 30, self.y + 15), 2)
        # 嘴巴
        pygame.draw.arc(surface, BLACK, (self.x + 10, self.y + 30, 20, 10), 0, 3.14, 2)
        
    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

# 障碍物类
class Obstacle:
    def __init__(self, x):
        self.width = random.randint(20, 40)
        self.height = random.randint(30, 60)
        self.x = x
        self.y = HEIGHT - 150 - self.height
        self.speed = 5
        self.color = BROWN
        
    def update(self):
        self.x -= self.speed
        if self.x < -self.width:
            return True
        return False
        
    def draw(self, surface):
        pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
        
    def get_rect(self):
        return pygame.Rect(self.x, self.y, self.width, self.height)

# 云朵类（装饰用）
class Cloud:
    def __init__(self):
        self.x = random.randint(WIDTH, WIDTH + 200)
        self.y = random.randint(20, 150)
        self.speed = random.uniform(0.5, 1.5)
        
    def update(self):
        self.x -= self.speed
        if self.x < -100:
            return True
        return False
        
    def draw(self, surface):
        # 绘制简单的云朵
        pygame.draw.ellipse(surface, WHITE, (self.x, self.y, 40, 20))
        pygame.draw.ellipse(surface, WHITE, (self.x + 10, self.y - 10, 30, 20))
        pygame.draw.ellipse(surface, WHITE, (self.x + 20, self.y, 30, 20))

# 游戏主函数
def main():
    player = Player()
    obstacles = []
    clouds = []
    score = 0
    obstacle_timer = 0
    cloud_timer = 0
    font = pygame.font.SysFont(None, 36)
    game_over = False
    
    # 游戏主循环
    running = True
    while running:
        # 处理事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE and not game_over:
                    player.jump()
                if event.key == pygame.K_r and game_over:
                    # 重新开始游戏
                    player = Player()
                    obstacles = []
                    clouds = []
                    score = 0
                    game_over = False
        
        if not game_over:
            # 更新玩家
            player.update()
            
            # 生成障碍物
            obstacle_timer += 1
            if obstacle_timer > random.randint(60, 120):  # 1-2秒生成一个障碍物
                obstacles.append(Obstacle(WIDTH))
                obstacle_timer = 0
                
            # 生成云朵
            cloud_timer += 1
            if cloud_timer > random.randint(100, 200):
                clouds.append(Cloud())
                cloud_timer = 0
            
            # 更新障碍物
            for obstacle in obstacles[:]:
                if obstacle.update():
                    obstacles.remove(obstacle)
                    score += 1
                # 碰撞检测
                if player.get_rect().colliderect(obstacle.get_rect()):
                    game_over = True
                    
            # 更新云朵
            for cloud in clouds[:]:
                if cloud.update():
                    clouds.remove(cloud)
        
        # 绘制背景
        screen.fill(BLUE)
        
        # 绘制地面
        pygame.draw.rect(screen, GRAY, (0, HEIGHT - 150, WIDTH, 150))
        pygame.draw.rect(screen, (100, 100, 100), (0, HEIGHT - 150, WIDTH, 5))
        
        # 绘制云朵
        for cloud in clouds:
            cloud.draw(screen)
            
        # 绘制障碍物
        for obstacle in obstacles:
            obstacle.draw(screen)
            
        # 绘制玩家
        player.draw(screen)
        
        # 绘制分数
        score_text = font.render(f'分数: {score}', True, BLACK)
        screen.blit(score_text, (10, 10))
        
        # 绘制控制说明
        control_text = font.render('空格键: 跳跃', True, BLACK)
        screen.blit(control_text, (WIDTH - 150, 10))
        
        if game_over:
            # 游戏结束画面
            game_over_text = font.render('游戏结束!', True, RED)
            restart_text = font.render('按R键重新开始', True, RED)
            screen.blit(game_over_text, (WIDTH//2 - 80, HEIGHT//2 - 50))
            screen.blit(restart_text, (WIDTH//2 - 100, HEIGHT//2))
            
        # 更新显示
        pygame.display.flip()
        clock.tick(FPS)
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()