import turtle
import random

# 窗口设置
turtle.title("tkinteh · 是男人就上一百层")
turtle.bgcolor("#0a0f1e")
turtle.setup(500, 700)
turtle.tracer(0)

# ===== 玩家 tkinteh =====
player = turtle.Turtle()
player.shape("square")
player.color("#ffaa44")
player.shapesize(1.2, 1.2)
player.penup()
player.goto(0, -250)
player.dy = 0

# ===== 平台 =====
platforms = []

def make_platform(x, y):
    p = turtle.Turtle()
    p.shape("square")
    p.color("#2bd1fc")
    p.shapesize(0.6, 3)
    p.penup()
    p.goto(x, y)
    platforms.append(p)

# 生成初始平台（密集一点，更简单）
for i in range(12):
    make_platform(random.randint(-180, 180), -280 + i * 55)

# ===== 计分 =====
score = 0
score_turtle = turtle.Turtle()
score_turtle.hideturtle()
score_turtle.penup()
score_turtle.color("cyan")
score_turtle.goto(-180, 300)
score_turtle.write(f"楼层: {score}", font=("Courier", 18, "bold"))

# ===== 游戏状态 =====
game_over = False

# ===== 左右移动（更灵敏）=====
def left():
    x = player.xcor()
    if x > -200:
        player.setx(x - 25)

def right():
    x = player.xcor()
    if x < 200:
        player.setx(x + 25)

# ===== 碰撞检测（更宽松）=====
def check_landing():
    global score, game_over
    px = player.xcor()
    py = player.ycor()
    
    for p in platforms:
        # 宽松的碰撞范围
        if abs(px - p.xcor()) < 40 and abs(py - p.ycor()) < 25 and player.dy <= 0:
            player.dy = 7.5  # 跳跃力度舒适
            player.goto(px, p.ycor() + 22)
            
            # 计分：每上一个新平台加分
            new_score = int((py + 320) / 50)
            if new_score > score:
                score = new_score
                score_turtle.clear()
                if score >= 100:
                    score_turtle.write(f"🎉 100层！tkinteh真男人！🎉", font=("Courier", 14, "bold"))
                else:
                    score_turtle.write(f"楼层: {score}", font=("Courier", 18, "bold"))
            return True
    return False

# ===== 游戏主循环 =====
def game_loop():
    global game_over
    
    if game_over:
        return
    
    # 重力
    player.dy -= 0.45
    player.sety(player.ycor() + player.dy)
    
    # 碰撞平台
    check_landing()
    
    # 掉下去就结束
    if player.ycor() < -320:
        game_over = True
        game_over_text = turtle.Turtle()
        game_over_text.hideturtle()
        game_over_text.color("#ff6666")
        game_over_text.write("💀 GAME OVER 💀\n点击运行重来", 
                            align="center", font=("Courier", 20, "bold"))
        return
    
    # 胜利条件
    if score >= 100:
        game_over = True
        win_text = turtle.Turtle()
        win_text.hideturtle()
        win_text.color("#ffd966")
        win_text.write("🎉 恭喜 tkinteh 通关！真男人！ 🎉", 
                      align="center", font=("Courier", 16, "bold"))
        return
    
    # 摄像机跟随 + 生成新平台（让游戏无限进行）
    if player.ycor() > 80:
        offset = player.ycor() - 80
        player.sety(80)
        
        for p in platforms:
            p.sety(p.ycor() - offset)
        
        # 在顶部生成新平台
        highest = min([p.ycor() for p in platforms])
        while highest < 250:
            make_platform(random.randint(-170, 170), highest + 55)
            highest += 55
        
        # 删除掉出屏幕的平台
        for p in platforms[:]:
            if p.ycor() < -350:
                p.hideturtle()
                platforms.remove(p)
    
    turtle.update()
    turtle.ontimer(game_loop, 18)

# ===== 键盘控制 =====
turtle.listen()
turtle.onkeypress(left, "Left")
turtle.onkeypress(right, "Right")
turtle.onkeypress(left, "a")
turtle.onkeypress(right, "d")

# 标题
title = turtle.Turtle()
title.hideturtle()
title.penup()
title.color("#0ff")
title.goto(0, 330)
title.write("⚡ tkinteh · 是男人就上一百层 ⚡", align="center", font=("Courier", 14, "bold"))

game_loop()
turtle.done()