用python画一个小乌龟
时间: 2023-10-23 17:12:50 浏览: 151
好的,以下是使用Python turtle库绘制一个小乌龟的代码:
```python
import turtle
# 设置画布大小
turtle.setup(600, 600)
# 创建画笔
pen = turtle.Turtle()
# 设置画笔颜色和粗细
pen.color('green')
pen.pensize(5)
# 绘制龟头
pen.fillcolor('green')
pen.begin_fill()
pen.right(45)
pen.forward(80)
pen.right(135)
pen.forward(80)
pen.right(135)
pen.forward(80)
pen.right(135)
pen.forward(80)
pen.end_fill()
# 绘制龟身
pen.fillcolor('yellow')
pen.begin_fill()
pen.right(45)
pen.forward(120)
pen.right(90)
pen.forward(120)
pen.right(90)
pen.forward(120)
pen.right(90)
pen.forward(120)
pen.end_fill()
# 绘制龟脚
pen.penup()
pen.goto(-60, -120)
pen.pendown()
pen.fillcolor('green')
pen.begin_fill()
pen.right(45)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.end_fill()
pen.penup()
pen.goto(0, -120)
pen.pendown()
pen.fillcolor('green')
pen.begin_fill()
pen.right(45)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.right(90)
pen.forward(60)
pen.end_fill()
# 隐藏画笔
pen.hideturtle()
# 显示绘画窗口
turtle.done()
```
运行以上代码后,你就可以看到一个用绿色和黄色绘制的小乌龟啦!
阅读全文