turtle画五种不同颜色的同心圆
时间: 2023-09-09 20:09:25 浏览: 324
好的,以下是使用Python的turtle库画出五个不同颜色的同心圆的代码:
```python
import turtle
# 设置背景颜色
turtle.bgcolor("black")
# 定义颜色列表
colors = ["red", "orange", "yellow", "green", "blue"]
turtle.speed(0) # 设置绘制速度为最快
# 循环绘制五个同心圆
for i in range(5):
turtle.penup() # 抬起画笔
turtle.goto(0, -i*30) # 移动到下一个圆的位置
turtle.pendown() # 落下画笔
turtle.pencolor(colors[i]) # 设置画笔颜色
turtle.pensize(2) # 设置画笔粗细
turtle.circle(30+i*30) # 绘制圆
turtle.hideturtle() # 隐藏画笔
turtle.done() # 完成绘制
```
运行代码后,会在窗口中绘制出五个不同颜色的同心圆,如下图所示:
![五个不同颜色的同心圆](https://img-blog.csdnimg.cn/20210901145557429.gif)
相关问题
用Python画五种颜色的同心圆
可以使用Python的turtle模块来画同心圆,下面是一个画五种颜色的同心圆的示例代码:
```python
import turtle
colors = ['red', 'green', 'blue', 'orange', 'purple']
radius = 100
turtle.speed(0)
for i in range(len(colors)):
turtle.penup()
turtle.goto(0, -i * radius)
turtle.pendown()
turtle.pencolor(colors[i])
turtle.circle(radius + i * radius)
turtle.done()
```
这段代码首先定义了五种颜色和半径,然后使用循环来画出五个同心圆,每个圆的半径都比前一个大一个半径的长度,并且使用不同的颜色。最后调用`turtle.done()`方法来显示画布。
turtle画五个同心圆
以下是使用turtle库画五个同心圆的示例代码:
```python
import turtle
t = turtle.Pen()
my_colors = ['red', 'green', 'yellow', 'black']
t.width(4)
t.speed(1)
for i in range(5):
t.penup()
t.goto(0, -i * 10)
t.pendown()
t.color(my_colors[i % len(my_colors)])
t.circle((i + 1) * 10)
turtle.done()
```
这段代码使用了循环来画五个同心圆,每个圆的半径逐渐增加。通过调整`my_colors`列表中的颜色,可以改变圆的颜色。`turtle.done()`用于保持窗口打开,直到手动关闭。