绘制随机彩色n边形的python代码
时间: 2023-05-20 18:03:32 浏览: 188
以下是一个绘制随机彩色n边形的Python代码:
```python
import turtle
import random
# 获取随机颜色
def get_random_color():
r = random.random()
g = random.random()
b = random.random()
return (r, g, b)
# 绘制n边形
def draw_polygon(n, size):
angle = 360 / n
for i in range(n):
turtle.forward(size)
turtle.right(angle)
# 绘制随机彩色n边形
def draw_random_polygon(n, size):
turtle.color(get_random_color())
turtle.begin_fill()
draw_polygon(n, size)
turtle.end_fill()
# 设置画布大小
turtle.setup(800, 800)
# 绘制10个随机彩色n边形
for i in range(10):
n = random.randint(3, 10)
size = random.randint(50, 200)
x = random.randint(-300, 300)
y = random.randint(-300, 300)
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
draw_random_polygon(n, size)
# 隐藏画笔
turtle.hideturtle()
# 点击关闭窗口
turtle.exitonclick()
```
希望能对你有所帮助!
阅读全文