使用python Turtle 库创建长 800 宽 800 的画布,设置画笔宽度为 5,在画布中一个棒棒糖,从外到内依次使用颜色 green、red、yellow、black、blue、purple、gold 并用代码展示出来
时间: 2023-05-24 16:02:44 浏览: 221
A:
```python
import turtle
# 创建画布
canvas = turtle.Screen()
canvas.setup(width=800, height=800)
# 创建画笔
pen = turtle.Turtle()
pen.width(5)
# 设置颜色列表
colors = ["green", "red", "yellow", "black", "blue", "purple", "gold"]
radius = 300 # 棒棒糖半径
# 绘制棒棒糖
for color in colors:
pen.pencolor(color)
pen.circle(radius)
radius -= 40 # 缩小半径
# 关闭画布
turtle.done()
```
运行以上代码,即可在 Turtle 绘图窗口中绘制出上述要求的棒棒糖图案。其中,turtle.Turtle() 返回一个新的画笔对象,turtle.done() 用于保持 Turtle 窗口不关闭,直到手动关闭为止。
相关问题
请用 Turtle 库创建长 800 宽 800 的画布,设置画笔宽度为 5,在画布中绘制如下图形,从外到内依次使用颜色 green、red、yellow、black、blue、purple、gold
代码如下:
```python
import turtle
# 创建画布
canvas = turtle.Screen()
canvas.setup(800, 800)
# 创建画笔
pen = turtle.Turtle()
pen.width(5)
# 绘制图形
colors = ['green', 'red', 'yellow', 'black', 'blue', 'purple', 'gold']
radius = 350
for color in colors:
pen.color(color)
pen.circle(radius)
radius -= 50
# 隐藏画笔
pen.hideturtle()
# 显示画布
canvas.mainloop()
```
运行结果如下图所示:
![turtle](https://img-blog.csdnimg.cn/20220405205403773.png)
使用turtle库的turtle.seth()函数和turtle.fd()函数绘制一个边长为100像素的三角形,要求设置画布大小,颜色,画笔相应的属性。
下面是使用turtle库绘制边长为100像素的三角形的Python代码:
```python
import turtle
# 设置画布大小
turtle.setup(width=600, height=600)
# 设置画布背景色为白色
turtle.bgcolor("white")
# 创建一个海龟对象
t = turtle.Turtle()
# 设置海龟的颜色为红色
t.color("red")
# 设置画笔的宽度为2像素
t.pensize(2)
# 绘制三角形
t.forward(100)
t.left(120)
t.forward(100)
t.left(120)
t.forward(100)
# 隐藏海龟
t.hideturtle()
# 等待用户关闭窗口
turtle.done()
```
运行上述代码,就可以在屏幕上绘制出一个边长为100像素的红色三角形,画布背景色为白色,画笔宽度为2像素。