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

使用Python开发Pong游戏时GUI屏幕中间出现白线的技术求助

Pong游戏屏幕中间白线问题解决

问题根源:

  • batB的penup调用缺少括号:batB.penup只是引用了方法对象,没有实际执行抬笔操作。当执行batB.goto(350, 0)时,海龟从默认初始位置(0,0)移动到目标位置,由于笔处于落下状态,就会在屏幕上画出一条从中间到右侧的白线。

修正后的代码:

import turtle

wm = turtle.Screen()
wm.title("Pong by Tharv")
wm.bgcolor("black")
# wm.setup(width=800, hieght=300)
wm.tracer(0)

# Bat A

batA = turtle.Turtle()
batA.speed(0)
batA.shape("square")
batA.shapesize(stretch_wid=5, stretch_len=1)
batA.color("white")
batA.penup()
batA.goto(-350, 0)

# Bat B

batB = turtle.Turtle()
batB.speed(0)
batB.shape("square")
batB.color("white")
batB.shapesize(stretch_wid=5, stretch_len=1)
batB.penup()  # 补上括号,执行抬笔操作
batB.goto(350, 0)


# Ball

ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, 0)


# maingameloop

while True:
    wm.update()

如果注释batB代码后仍能看到白线,大概率是之前运行程序时的界面缓存残留,重启程序并使用修正后的代码即可解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 19:45:38