如何在 Python turtle 中只画出半圆?
我只能使用Python turtle。我尝试了寻找资源,但没有找到仅使用Python turtle的相关资料。
我只能使用Python turtle。我尝试了寻找资源,但没有找到仅使用Python turtle的相关资料。
请参阅Python turtle参考文档中关于圆形的部分(英文链接)。 例如,对于半径为100的半圆,代码如下:
import turtle
turtle.circle(100,180)
请尝试以下方法:
import turtle
t = turtle.Pen()
t.left(90)
for x in range(180):
t.forward(1)
t.right(1)
t.right(90)
t.forward(115)
你也可以只使用圆形来完成。 turtle.circle(半径, 弧度, 步数)
例如:
turtle.circle(50,180) # - step is optional
为了完整起见,这里介绍使用海龟图形的拓印而非绘制来创建一个半圆的方法:
from turtle import Turtle, Screen
screen = Screen()
DIAMETER = 200
STAMP_SIZE = 20
BACKGROUND = screen.bgcolor()
yertle = Turtle('circle', visible=False)
yertle.penup()
yertle.shapesize(DIAMETER / STAMP_SIZE)
yertle.color('black', BACKGROUND) # drop second argument for a filled semicircle
yertle.stamp()
yertle.shape('square')
yertle.shapesize(stretch_len=(DIAMETER / 2) / STAMP_SIZE)
yertle.color(BACKGROUND)
yertle.forward(DIAMETER / 4)
yertle.stamp()
screen.exitonclick()
它有明显的缺点,但有时正是你所需要的。
import turtle
turtle.circle(100, 180) #Draws a circle with 180 degrees and 100 pixels radius(a half circle)
turtle.left(90)
turtle.forward(200)
import turtle
tom=turtle.Turtle()
tom.circle(100,180)
对于圆形,第一个数字是圆的半径,第二个数字是你想要画的弧度。如果你想画半圆,可以使用180度,就像上面的代码所示,但是你也可以画四分之一的圆,然后如果你想连接半圆,只需向左转,然后向前移动半径*2。