用python画一只乌龟
时间: 2023-10-22 11:08:06 浏览: 154
以下是使用turtle模块绘制一只乌龟的Python代码:
```python
import turtle
# 创建画布和画笔
canvas = turtle.Screen()
pen = turtle.Turtle()
# 绘制乌龟的头部
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('yellow')
pen.begin_fill()
pen.circle(100)
pen.end_fill()
# 绘制乌龟的身体
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('green')
pen.begin_fill()
pen.right(90)
pen.forward(150)
pen.right(90)
pen.circle(50, 180)
pen.right(90)
pen.forward(150)
pen.end_fill()
# 绘制乌龟的四条腿
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('green')
pen.begin_fill()
pen.right(180)
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.end_fill()
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('green')
pen.begin_fill()
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.end_fill()
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('green')
pen.begin_fill()
pen.right(180)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.end_fill()
pen.pensize(5)
pen.pencolor('green')
pen.fillcolor('green')
pen.begin_fill()
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.right(90)
pen.forward(50)
pen.end_fill()
# 绘制乌龟的眼睛和嘴巴
pen.pensize(3)
pen.pencolor('black')
pen.fillcolor('white')
pen.begin_fill()
pen.right(180)
pen.penup()
pen.forward(70)
pen.left(90)
pen.forward(30)
pen.pendown()
pen.circle(20)
pen.end_fill()
pen.pensize(3)
pen.pencolor('black')
pen.fillcolor('black')
pen.begin_fill()
pen.penup()
pen.right(180)
pen.forward(10)
pen.left(90)
pen.forward(10)
pen.pendown()
pen.circle(5)
pen.end_fill()
pen.pensize(3)
pen.pencolor('black')
pen.fillcolor('white')
pen.begin_fill()
pen.penup()
pen.right(180)
pen.forward(50)
pen.left(90)
pen.forward(20)
pen.pendown()
pen.circle(10)
pen.end_fill()
pen.pensize(3)
pen.pencolor('black')
pen.penup()
pen.right(180)
pen.forward(20)
pen.left(90)
pen.forward(20)
pen.pendown()
pen.right(90)
pen.circle(10, 180)
# 隐藏画笔
pen.hideturtle()
canvas.exitonclick()
```
运行以上代码,即可在画布上看到一只乌龟的绘制结果。
阅读全文