使用海龟图形绘制虚线

问题描述 投票:0回答:3
from turtle import Turtle, Screen
tt_turtle_obj = Turtle()

for _ in range(15):
    tt_turtle_obj.forward(10)
    tt_turtle_obj.color("white")
    tt_turtle_obj.forward(10)
    tt_turtle_obj.color("black")

screen = Screen()
screen.exitonclick()

我用这段代码做了同样的事情。还有其他办法吗?

python line turtle-graphics python-turtle
3个回答
3
投票

您可以使用

turtle.penup()
turtle.pendown()
方法来控制海龟何时在画布上绘制以及何时不绘制。这是代码:

from turtle import Turtle, Screen
tt_turtle_obj = Turtle()

for _ in range(15):
    tt_turtle_obj.forward(10)
    tt_turtle_obj.penup()
    tt_turtle_obj.forward(10)
    tt_turtle_obj.pendown()

screen = Screen()
screen.exitonclick()

这样,您可以在任何背景上绘制虚线,并且可以在不同场景中重用代码。最后,两个代码都做了同样的事情。


0
投票

尝试使用turtle.pencolor()代替turtle.color()来改变线条的颜色。

from turtle import Turtle, Screen
tt_turtle_obj = Turtle()

for _ in range(15):
  tt_turtle_obj.forward(10)
  tt_turtle_obj.pencolor("black")
  tt_turtle_obj.forward(10)
  tt_turtle_obj.pencolor("white")

screen = Screen()
screen.exitonclick()

0
投票

您可以使用此方法绘制特定距离的虚线。 通过更改

<dis>
<size>
值,您可以设置距离和破折号大小。

from turtle import *

###doted line###
for i in range(<dis>):
    if int(i/<size>)%2==1:
        pencolor('black')
    else:
        pencolor('white')
    forward(1)
pencolor('black')
© www.soinside.com 2019 - 2024. All rights reserved.