You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Python Turtle库:Pong游戏开场界面绘制时隐藏画笔移动轨迹问题求助

Fixing Turtle Trail Issue in Pong Intro Screen

Hey there! Let's sort out that unwanted turtle trail you're seeing when moving the pen from the center to (0, -50) in your Pong opening screen.

The Root Cause

Even though you used pen.hideturtle(), the turtle pen is still in a "down" state by default. This means any movement will leave a visible trail on the screen, even if the turtle itself is hidden.

The Solution

You just need to lift the pen up before moving it, so it doesn't draw while repositioning. Here's your modified code with the fix highlighted:

import turtle
wn = turtle.Screen()
wn.title("pong by @FakeTony")
wn.bgcolor("black")
wn.setup(width=800, height=600)
wn.tracer(0)

# Entry board
pen = turtle.Turtle()
pen.speed(0)
pen.color("white")
pen.hideturtle()
pen.penup()  # This lifts the pen, preventing trails during movement
pen.goto(0, -50)
pen.write("Created by FakeTony", align="center", font=("Courier", 24, "normal"))
pen.goto(0, 0)
pen.write("Welcome To Pong", align="center", font=("Courier", 24, "normal"))

while True:
    wn.update()

How It Works

  • pen.penup() tells the turtle to lift its pen off the canvas. Any subsequent goto() calls won't draw lines.
  • Since the write() method doesn't rely on the pen being down to render text, you don't need to call pen.pendown() here. The text will still appear perfectly at the positions you specify.

This should eliminate that unwanted trail between your two text elements!

内容的提问来源于stack exchange,提问作者TonyofIDK

火山引擎 最新活动