用python随机颜色画风车
时间: 2023-11-19 21:00:34 浏览: 74
随机生成RGB颜色.py
以下是一个随机颜色画风车的python程序:
```python
import turtle
import random
# 设置画布和画笔
canvas = turtle.Screen()
canvas.bgcolor("white")
pen = turtle.Turtle()
pen.speed(0)
pen.pensize(3)
# 定义颜色列表
colors = ["red", "orange", "yellow", "green", "blue", "purple"]
# 循环画风车
for i in range(4):
# 随机选择颜色
color = random.choice(colors)
pen.color(color)
# 画风车的四个部分
pen.forward(100)
pen.right(90)
pen.forward(100)
pen.right(135)
pen.forward(140)
pen.right(135)
pen.forward(100)
pen.right(90)
# 隐藏画笔
pen.hideturtle()
# 点击关闭窗口
canvas.exitonclick()
```
运行上述代码,可以生成一个随机颜色的风车。每次运行程序,其颜色都会不同。
阅读全文