You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Turtle模块文本绘制问题:分数显示重叠

解决Turtle模块分数显示重叠问题

使用Python的turtle模块编写贪吃蛇时,屏幕顶部的分数显示出现重叠:显示1时正常,但后续分数会相互覆盖,尝试过.clear()和.undo()方法无效。

原代码

main.py

new_food = Food()
cords = new_food.food()
score_food = Food()
score_food.write_score(0)
while is_on:
    my_screen.update()
    time.sleep(.01)
    is_on = snake.move()

    if snake.head.distance(cords) < 15:
        score_food.clear()
        score = new_food.increase_score(score)

        new_food = Food()
        cords = new_food.food()

food.py

from turtle import Turtle
import random


class Food(Turtle):
    def __init__(self):
        super().__init__()

    def food(self):
        self.shape('circle')
        self.shapesize(.5, .5)
        self.color('blue')
        x = random.randint(-270, 270)
        y = random.randint(-280, 280)
        while x % 10 != 0:
            x += 1
        while y % 10 != 0:
            y += 1
        self.teleport(x, y)
        return x, y

    def increase_score(self, score):
        score += 1
        self.clear()
        self.write_score(score)
        return score

    def write_score(self, score):
        self.clear()
        self.color('white')
        self.penup()  # Lift the pen to prevent drawing lines
        self.goto(0, 250)  # Move to the desired position
        self.hideturtle()
        self.write(f"Score is: {score}", align="center", font=("Arial", 24, "normal"))

问题根源

  1. 职责混乱:Food类同时负责食物生成和分数显示,违反单一职责原则,导致逻辑混乱。
  2. 实例冲突:你创建了score_food用于显示分数,但每次得分时调用的是new_food(食物实例)的increase_score方法,相当于用新的Turtle实例绘制分数,而旧的score_food实例的文本未被正确清理,多个实例在同一位置叠加绘制,造成重叠。

修复方案

拆分类职责,单独创建分数显示类,确保始终用同一个实例更新分数。

修改后的代码

food.py(仅保留食物生成逻辑)

from turtle import Turtle
import random


class Food(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('circle')
        self.shapesize(.5, .5)
        self.color('blue')
        self.penup()
        self.speed("fastest")
        self.refresh()

    def refresh(self):
        x = random.randint(-270, 270)
        y = random.randint(-280, 280)
        while x % 10 != 0:
            x += 1
        while y % 10 != 0:
            y += 1
        self.teleport(x, y)

新增scoreboard.py(专门处理分数显示)

from turtle import Turtle


class Scoreboard(Turtle):
    def __init__(self):
        super().__init__()
        self.score = 0
        self.color('white')
        self.penup()
        self.goto(0, 250)
        self.hideturtle()
        self.update_scoreboard()

    def update_scoreboard(self):
        self.clear()
        self.write(f"Score is: {self.score}", align="center", font=("Arial", 24, "normal"))

    def increase_score(self):
        self.score += 1
        self.update_scoreboard()

main.py(调整实例调用逻辑)

from scoreboard import Scoreboard

food = Food()
scoreboard = Scoreboard()
while is_on:
    my_screen.update()
    time.sleep(.01)
    is_on = snake.move()

    if snake.head.distance(food) < 15:
        scoreboard.increase_score()
        food.refresh()

修复说明

  • 拆分后Scoreboard实例唯一,每次调用increase_score都会先清除自身之前的文本,再绘制新分数,彻底避免重叠。
  • Food类专注于食物生成,用refresh方法更新位置,简化逻辑。
  • main.py不再重复创建Food实例,减少资源浪费,逻辑更清晰。

内容的提问来源于stack exchange,提问作者Eishan Baba singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:35:57