Python 乌龟,删除行
Python turtle, delete line
你好,我试着学习如何在 python 中使用 turtle,我做了以下代码来绘制我的首字母 (V T),但我不知道如何去除黑线。
import turtle
def draw_myname():
window = turtle.Screen()
window.bgcolor("red")
#Create the V letter - Draw V
vita = turtle.Turtle()
vita.shape("turtle")
vita.color("yellow")
vita.speed(2)
vita.right(75)
vita.forward(100)
vita.left(150)
vita.forward(100)
vita.right(75)
#Create the T letter - Draw T
vita = turtle.Turtle()
vita.goto(100,0)
vita.shape("turtle")
vita.color("blue")
vita.forward(5)
vita.forward(100)
vita.back(50)
vita.right(90)
vita.forward(100)
window.exitonclick()
draw_myname()
您必须设置 turtle.up()
使其在移动时不画线。在移动之前添加 vita.up()
,当您准备绘制时添加 vita.down()
。
#Create the T letter - Draw T
vita = turtle.Turtle()
vita.up() #add this
vita.goto(100,0)
vita.down() #add this
vita.shape("turtle")
vita.color("blue")
vita.forward(5)
vita.forward(100)
vita.back(50)
vita.right(90)
vita.forward(100)
使用函数penup()
。或者,在您的情况下,vita.penup()
。
除了已发布的其他答案之外,另一种方法可能是:
您不必重新定义海龟(为海龟命名、更改其形状和其他内容),但您必须更改其颜色。
还有,如果你想把黑线擦掉,我用的方法是把海龟的颜色改成背景色,然后把黑线重叠起来。
使用 python 3.8,有乌龟。
我的经验是获取背景,设置画笔颜色后,使用color(col), returns col 两种情况:
foreground, background = color()
foreground = pencolor()
background = fillcolor()
我在“擦除一条线”方面的唯一成功,例如,用相同颜色的较细线替换较粗的线是使用以下代码,明确使用“白色”作为背景:
side = 100
def move_x():
""" Change current and subsequent lines to a thinner line
"""
foreground = pencolor()
background = "white"
print(f"foreground:{foreground} background:{background}")
color(background) # Erase previous line
backward(side)
color(foreground)
t = width()
t -= 1
if t < 1:
t = 1
width(t)
forward(side)
你好,我试着学习如何在 python 中使用 turtle,我做了以下代码来绘制我的首字母 (V T),但我不知道如何去除黑线。
import turtle
def draw_myname():
window = turtle.Screen()
window.bgcolor("red")
#Create the V letter - Draw V
vita = turtle.Turtle()
vita.shape("turtle")
vita.color("yellow")
vita.speed(2)
vita.right(75)
vita.forward(100)
vita.left(150)
vita.forward(100)
vita.right(75)
#Create the T letter - Draw T
vita = turtle.Turtle()
vita.goto(100,0)
vita.shape("turtle")
vita.color("blue")
vita.forward(5)
vita.forward(100)
vita.back(50)
vita.right(90)
vita.forward(100)
window.exitonclick()
draw_myname()
您必须设置 turtle.up()
使其在移动时不画线。在移动之前添加 vita.up()
,当您准备绘制时添加 vita.down()
。
#Create the T letter - Draw T
vita = turtle.Turtle()
vita.up() #add this
vita.goto(100,0)
vita.down() #add this
vita.shape("turtle")
vita.color("blue")
vita.forward(5)
vita.forward(100)
vita.back(50)
vita.right(90)
vita.forward(100)
使用函数penup()
。或者,在您的情况下,vita.penup()
。
除了已发布的其他答案之外,另一种方法可能是:
您不必重新定义海龟(为海龟命名、更改其形状和其他内容),但您必须更改其颜色。
还有,如果你想把黑线擦掉,我用的方法是把海龟的颜色改成背景色,然后把黑线重叠起来。
使用 python 3.8,有乌龟。 我的经验是获取背景,设置画笔颜色后,使用color(col), returns col 两种情况:
foreground, background = color()
foreground = pencolor()
background = fillcolor()
我在“擦除一条线”方面的唯一成功,例如,用相同颜色的较细线替换较粗的线是使用以下代码,明确使用“白色”作为背景:
side = 100
def move_x():
""" Change current and subsequent lines to a thinner line
"""
foreground = pencolor()
background = "white"
print(f"foreground:{foreground} background:{background}")
color(background) # Erase previous line
backward(side)
color(foreground)
t = width()
t -= 1
if t < 1:
t = 1
width(t)
forward(side)