import pygame
import random
import sys

pygame.init()
# 窗口尺寸
WIDTH = 400
HEIGHT = 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("像素鸟闯关【简单版】")

# 颜色定义
SKY_BLUE = (100, 180, 255)
GREEN = (30, 180, 60)
DARK_GREEN = (10, 110, 30)
YELLOW_BIRD = (255, 220, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

# 兼容海龟编辑器字体
font = pygame.font.Font(None, 42)
small_font = pygame.font.Font(None, 32)

# 小鸟参数【调柔和】
bird_x = 80
bird_y = HEIGHT // 2
bird_size = 24
gravity = 0.20
jump_power = -4.0
bird_speed_y = 0

# 管道设置【难度降低核心】
pipe_width = 60
pipe_gap = 170    # 空隙加大，更容易穿过
pipe_speed = 2.2  # 管道移动变慢
pipes = []  # 存储管道组 {"x":横坐标, "gap_y":间隙起点, "scored":是否已经得分}
spawn_pipe_timer = 0
spawn_interval = 2100  # 管道生成间隔变长

score = 0
game_over = False
clock = pygame.time.Clock()

# 生成一组管道（上下一对）
def create_pipe_group():
    gap_y = random.randint(120, HEIGHT - 220)
    return {
        "x": WIDTH,
        "gap_y": gap_y,
        "scored": False
    }

# 绘制小鸟
def draw_bird(x, y):
    pygame.draw.rect(screen, YELLOW_BIRD, (x, y, bird_size, bird_size))
    pygame.draw.rect(screen, BLACK, (x+16, y+6, 5, 5))
    pygame.draw.rect(screen, (255, 130, 0), (x+20, y+10, 8, 4))

# 绘制一组上下管道
def draw_pipe_group(group):
    # 上管道
    pygame.draw.rect(screen, GREEN, (group["x"], 0, pipe_width, group["gap_y"]))
    pygame.draw.rect(screen, DARK_GREEN, (group["x"], group["gap_y"] - 8, pipe_width, 8))
    # 下管道
    bottom_y = group["gap_y"] + pipe_gap
    pygame.draw.rect(screen, GREEN, (group["x"], bottom_y, pipe_width, HEIGHT - bottom_y))
    pygame.draw.rect(screen, DARK_GREEN, (group["x"], bottom_y, pipe_width, 8))

# 精准碰撞检测
def check_collision(bx, by, group):
    bird_rect = pygame.Rect(bx, by, bird_size, bird_size)
    pipe_top_rect = pygame.Rect(group["x"], 0, pipe_width, group["gap_y"])
    pipe_bottom_rect = pygame.Rect(group["x"], group["gap_y"] + pipe_gap, pipe_width, HEIGHT)
    if bird_rect.colliderect(pipe_top_rect) or bird_rect.colliderect(pipe_bottom_rect):
        return True
    return False

running = True
while running:
    now = pygame.time.get_ticks()
    clock.tick(60)
    screen.fill(SKY_BLUE)

    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if not game_over:
            if event.type == pygame.MOUSEBUTTONDOWN or (event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE):
                bird_speed_y = jump_power

    if not game_over:
        # 小鸟重力
        bird_speed_y += gravity
        bird_y += bird_speed_y

        # 碰到上下边界直接死亡
        if bird_y <= 0 or bird_y + bird_size >= HEIGHT:
            game_over = True

        # 生成管道组
        if now - spawn_pipe_timer > spawn_interval:
            pipes.append(create_pipe_group())
            spawn_pipe_timer = now

        # 更新管道
        for group in pipes:
            group["x"] -= pipe_speed

            # 碰撞检测
            if check_collision(bird_x, bird_y, group):
                game_over = True

            # 穿过管道得分（只加一次）
            if not group["scored"] and group["x"] + pipe_width < bird_x:
                score += 1
                group["scored"] = True

        # 移除飞出屏幕的管道
        pipes = [g for g in pipes if g["x"] > -pipe_width]

    # 绘制所有管道
    for g in pipes:
        draw_pipe_group(g)
    # 绘制小鸟
    draw_bird(bird_x, bird_y)

    # 分数显示
    score_text = font.render(f"Score: {int(score)}", True, WHITE)
    screen.blit(score_text, (15, 15))

    # 游戏结束界面
    if game_over:
        overlay = pygame.Surface((WIDTH, HEIGHT))
        overlay.set_alpha(160)
        overlay.fill(BLACK)
        screen.blit(overlay, (0, 0))
        text1 = font.render("GAME OVER", True, WHITE)
        text2 = font.render(f"Final Score: {int(score)}", True, WHITE)
        tip = small_font.render("关闭窗口退出", True, WHITE)
        screen.blit(text1, (WIDTH//2 - 105, HEIGHT//2 - 80))
        screen.blit(text2, (WIDTH//2 - 115, HEIGHT//2 - 10))
        screen.blit(tip, (WIDTH//2 - 110, HEIGHT//2 + 60))

    pygame.display.update()

pygame.quit()
sys.exit()