首先定义绘制五角星的函数,将线条的颜色和线条的粗细作为参数, 如(仅供参考):def mystars(color,size) 然后,调用mystars函数,在不重复的位置绘制3个不同颜色和粗细的五角星,用Python代码写编程
时间: 2024-01-22 19:18:36 浏览: 21
可以使用Python的turtle库来绘制五角星,下面是一个示例代码:
```python
import turtle
import random
def mystars(color, size):
turtle.penup()
turtle.goto(random.randint(-300, 300), random.randint(-200, 200))
turtle.pendown()
turtle.color(color)
turtle.pensize(size)
turtle.begin_fill()
for i in range(5):
turtle.forward(100)
turtle.right(144)
turtle.end_fill()
# 绘制三个不同颜色和粗细的五角星
colors = ["red", "green", "blue"]
sizes = [1, 3, 5]
for i in range(3):
color = random.choice(colors)
size = random.choice(sizes)
mystars(color, size)
turtle.done()
```
这段代码会在窗口随机位置绘制三个五角星,每个五角星的颜色和粗细都是随机选择的。你可以根据自己的需求修改颜色和粗细的选项。
相关问题
首先定义绘制五角星的函数,将线条的颜色和线条的粗细作为参数, 如(仅供参考):def mystars(color,size) 然后,调用mystars函数,在不重复的位置绘制3个不同颜色和粗细的五角星。
好的,以下是符合要求的代码:
import turtle
import random
def mystars(color,size):
turtle.color(color)
turtle.pensize(size)
turtle.begin_fill()
for i in range(5):
turtle.forward(100)
turtle.right(144)
turtle.end_fill()
positions = [(100, 100), (-100, -100), (0, 200)]
for position in positions:
color = (random.random(), random.random(), random.random())
size = random.randint(1, 10)
turtle.penup()
turtle.goto(position)
turtle.pendown()
mystars(color, size)
turtle.done()
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)