import turtle
import random
import time

# -------------------------- 游戏基础设置 --------------------------
# 窗口
wn = turtle.Screen()
wn.title("贪吃蛇游戏")
wn.bgcolor("black")
wn.setup(width=600, height=600)
wn.tracer(0)  # 关闭自动刷新，提高流畅度

# 蛇头
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("green")
head.penup()
head.goto(0, 0)
head.direction = "stop"  # 初始静止

# 食物
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)

# 蛇身体
segments = []

# 分数
score = 0
high_score = 0

# 分数显示
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("得分: 0  最高分: 0", align="center", font=("Arial", 24, "normal"))

# -------------------------- 控制方向函数 --------------------------
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_left():
    if head.direction != "right":
        head.direction = "left"

def go_right():
    if head.direction != "left":
        head.direction = "right"

# -------------------------- 移动函数 --------------------------
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)

# -------------------------- 键盘绑定 --------------------------
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

# -------------------------- 主游戏循环 --------------------------
while True:
    wn.update()

    # 撞墙判断
    if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290:
        time.sleep(1)
        head.goto(0, 0)
        head.direction = "stop"

        # 隐藏身体
        for seg in segments:
            seg.goto(1000, 1000)

        segments.clear()
        score = 0
        pen.clear()
        pen.write(f"得分: {score}  最高分: {high_score}", align="center", font=("Arial", 24, "normal"))

    # 吃到食物
    if head.distance(food) < 20:
        # 随机新食物
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        food.goto(x, y)

        # 增加身体
        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("lightgreen")
        new_segment.penup()
        segments.append(new_segment)

        # 加分
        score += 10
        if score > high_score:
            high_score = score

        pen.clear()
        pen.write(f"得分: {score}  最高分: {high_score}", align="center", font=("Arial", 24, "normal"))

    # 身体跟着头移动
    for i in range(len(segments) - 1, 0, -1):
        x = segments[i-1].xcor()
        y = segments[i-1].ycor()
        segments[i].goto(x, y)

    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x, y)

    move()

    # 撞到自己
    for seg in segments:
        if seg.distance(head) < 20:
            time.sleep(1)
            head.goto(0, 0)
            head.direction = "stop"

            for seg in segments:
                seg.goto(1000, 1000)

            segments.clear()
            score = 0
            pen.clear()
            pen.write(f"得分: {score}  最高分: {high_score}", align="center", font=("Arial", 24, "normal"))

    time.sleep(0.1)

wn.mainloop()