(1)利用turtle绘制红色三角形,蓝色正方形,绿色正五边形,粉色正六边形,黑色圆形图形。注意:需要设置窗口标题为4-1.py,窗口背景为白色。
时间: 2024-10-23 10:06:21 浏览: 30
turtle库绘图应用.zip_Turtle库绘制_turtle库下载_turtle库画图_turtle绘图_初学turtle画
5星 · 资源好评率100%
在Python中,我们可以使用turtle模块来绘制形状。首先,你需要导入turtle库,并设置窗口标题和背景色。然后,分别创建函数来绘制各种颜色的几何图形。这里是一个示例代码:
```python
import turtle
# 设置窗口标题和背景颜色
turtle.title("4-1.py")
turtle.bgcolor("white")
def draw_shape(color, shape):
# 创建一个turtle对象并设置填充颜色
my_turtle = turtle.Turtle()
my_turtle.fillcolor(color)
if shape == "triangle":
for _ in range(3):
my_turtle.forward(100)
my_turtle.left(120)
my_turtle.fill(True)
elif shape == "square":
for _ in range(4):
my_turtle.forward(100)
my_turtle.left(90)
my_turtle.fill(True)
elif shape == "pentagon":
angle = 360 / 5
for _ in range(5):
my_turtle.forward(100)
my_turtle.left(angle)
my_turtle.fill(True)
elif shape == "hexagon":
angle = 360 / 6
for _ in range(6):
my_turtle.forward(100)
my_turtle.left(angle)
my_turtle.fill(True)
else: # circle
radius = 100
my_turtle.circle(radius)
my_turtle.fill(True)
# 绘制每个图形
draw_shape("red", "triangle")
draw_shape("blue", "square")
draw_shape("green", "pentagon")
draw_shape("pink", "hexagon")
draw_shape("black", "circle")
# 结束绘图并显示窗口
turtle.done()
```
当你运行这段代码,你会看到一个窗口,其中包含了一个红色的三角形、蓝色的正方形、绿色的正五边形、粉色的正六边形以及黑色的圆形。
阅读全文