使用turtle库,分别绘制正方形、五边形、六边形和八边形,并分别使用红、绿、蓝、黄四种颜色。用Python分别进行高水平和简单编译
时间: 2023-07-04 19:09:07 浏览: 139
Python如何使用turtle库绘制图形
高水平编译代码如下:
```python
import turtle
# 定义函数绘制多边形
def draw_polygon(n, color):
turtle.color(color)
for i in range(n):
turtle.forward(50)
turtle.left(360/n)
# 绘制正方形、五边形、六边形和八边形
turtle.penup()
turtle.goto(-200, 0)
turtle.pendown()
draw_polygon(4, "red")
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
draw_polygon(5, "green")
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
draw_polygon(6, "blue")
turtle.penup()
turtle.goto(100, 0)
turtle.pendown()
draw_polygon(8, "yellow")
turtle.done()
```
简单编译代码如下:
```python
import turtle
# 绘制正方形
turtle.color("red")
for i in range(4):
turtle.forward(50)
turtle.left(90)
# 绘制五边形
turtle.penup()
turtle.goto(-100, 0)
turtle.pendown()
turtle.color("green")
for i in range(5):
turtle.forward(50)
turtle.left(72)
# 绘制六边形
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.color("blue")
for i in range(6):
turtle.forward(50)
turtle.left(60)
# 绘制八边形
turtle.penup()
turtle.goto(100, 0)
turtle.pendown()
turtle.color("yellow")
for i in range(8):
turtle.forward(50)
turtle.left(45)
turtle.done()
```
阅读全文