Python Turtle绘图:线条、圆形和等边三角形
可以使用Python中的turtle库来绘制图形。
以下是使用turtle库绘制线条、圆和等边三角形的示例代码:
import turtle
# 设置线条颜色和粗细
turtle.pensize(10)
turtle.pencolor('black')
# 绘制线条
turtle.forward(100)
# 绘制圆
turtle.fillcolor('blue')
turtle.begin_fill()
turtle.circle(50)
turtle.end_fill()
# 绘制等边三角形
turtle.penup()
turtle.goto(-90, -90)
turtle.pendown()
turtle.fillcolor('white')
turtle.begin_fill()
for _ in range(3):
turtle.forward(180)
turtle.left(120)
turtle.end_fill()
turtle.done()
以上代码中,我们首先导入了turtle库,然后设置了线条的颜色和粗细。接着使用turtle的forward()函数绘制了一条线条。
然后,使用turtle的circle()函数绘制了一个半径为50的圆,并设置了填充颜色为蓝色。
最后,使用turtle的penup()和pendown()函数设置画笔的起始位置,然后使用forward()函数绘制了等边三角形的三条边,每条边长度为180,然后使用left()函数向左旋转120度。
最后,使用turtle.done()函数来显示绘制结果。
希望能帮到你!
原文地址: https://www.cveoy.top/t/topic/qxZX 著作权归作者所有。请勿转载和采集!