import pygame
import random

pygame.init()
WIDTH, HEIGHT = 480, 720
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("跳一跳 Python版")

# 颜色常量
WHITE = (255, 255, 255)
GRAY = (100, 100, 100)
BLACK = (30, 30, 30)
GREEN = (76, 175, 80)
BROWN = (121, 85, 72)
RED = (255, 80, 80)

clock = pygame.time.Clock()
font = pygame.font.Font(None, 36)

# 方块类（世界坐标）
class Block:
    def __init__(self, world_x, world_y, w, h):
        self.world_x = world_x
        self.world_y = world_y
        self.w = w
        self.h = h

    def draw(self, cam_x):
        sx = self.world_x - cam_x
        pygame.draw.rect(screen, BROWN, (sx, self.world_y, self.w, self.h))
        pygame.draw.rect(screen, GRAY, (sx, self.world_y, self.w, self.h), 2)

# 玩家
class Player:
    def __init__(self, wx, wy):
        self.world_x = wx
        self.world_y = wy
        self.r = 16
        self.vx = 0
        self.vy = 0
        self.on_ground = True
        self.power = 0
        self.max_power = 800

    def jump(self):
        if self.on_ground:
            p = self.power
            self.vx = p * 0.06
            self.vy = -p * 0.08
            self.on_ground = False
            self.power = 0

    def update(self, blocks):
        gravity = 0.6
        self.vy += gravity
        self.world_x += self.vx
        self.world_y += self.vy

        self.on_ground = False
        for b in blocks:
            if self.vy > 0:
                if (self.world_x + self.r > b.world_x and self.world_x - self.r < b.world_x + b.w):
                    if self.world_y + self.r >= b.world_y and self.world_y + self.r <= b.world_y + 22:
                        self.world_y = b.world_y - self.r
                        self.vx = 0
                        self.vy = 0
                        self.on_ground = True
                        break

    def draw(self, cam_x):
        sx = self.world_x - cam_x
        pygame.draw.circle(screen, GREEN, (int(sx), int(self.world_y)), self.r)
        if self.power > 0:
            bar_w = self.power / self.max_power * 40
            pygame.draw.rect(screen, WHITE, (sx - 20, self.world_y - 30, bar_w, 6))

# 只向右生成方块！不再左右随机！
def create_next_block(last_block):
    dist = random.randint(70, 160)
    w = random.randint(50, 80)
    nx = last_block.world_x + dist
    ny = last_block.world_y
    return Block(nx, ny, w, 60)

def reset_game():
    start = Block(0, HEIGHT // 2, 80, 60)
    blocks = [start]
    # 预先生成一串向右的方块
    cur = start
    for _ in range(10):
        cur = create_next_block(cur)
        blocks.append(cur)
    player = Player(start.world_x + start.w//2, start.world_y - 16)
    return blocks, player, 0

blocks, player, score = reset_game()
game_over = False
hold = False
land_flag = False
camera_x = 0

running = True
while running:
    screen.fill(BLACK)
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and not game_over:
            hold = True
        if event.type == pygame.MOUSEBUTTONUP and event.button == 1 and hold and not game_over:
            player.jump()
            hold = False
            land_flag = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_r and game_over:
                blocks, player, score = reset_game()
                game_over = False
                camera_x = 0

    # 蓄力
    if hold and player.on_ground and not game_over:
        player.power += 2.5
        if player.power > player.max_power:
            player.power = player.max_power

    if not game_over:
        player.update(blocks)

        # 掉落判定
        if player.world_y > HEIGHT + 100:
            game_over = True

        # 落地加分
        if player.on_ground and not land_flag:
            land_flag = True
            score += 1

        # 相机跟随人物居中
        camera_x = player.world_x - WIDTH // 2

        # =====重点：持续补充前方方块，每一帧都判断！=====
        far_block = blocks[-1]
        # 如果最远方块离玩家不够远，立刻新增
        if far_block.world_x - player.world_x < WIDTH * 2:
            new_blk = create_next_block(far_block)
            blocks.append(new_blk)

    # 绘制所有方块
    for b in blocks:
        b.draw(camera_x)
    player.draw(camera_x)

    # 文字
    score_surf = font.render(f"分数：{score}", True, WHITE)
    screen.blit(score_surf, (10, 10))

    if game_over:
        over_surf = font.render("游戏结束，按R重新开始", True, RED)
        screen.blit(over_surf, (WIDTH//2 - over_surf.get_width()//2, HEIGHT//2))

    pygame.display.flip()

pygame.quit()