使用turtle库绘制一个小兔子
时间: 2023-07-20 17:33:41 浏览: 181
用turtle画小兔子.py
好的,让我来给你展示一下如何使用turtle库绘制一个小兔子。
首先,我们需要导入turtle库:
```python
import turtle
```
然后,我们需要创建一个画布,并设置一些基本属性:
```python
# 创建画布
wn = turtle.Screen()
# 设置画布背景色
wn.bgcolor("white")
# 设置画笔颜色
turtle.pencolor("black")
# 设置画笔大小
turtle.pensize(2)
# 隐藏画笔
turtle.hideturtle()
```
接下来,我们就可以开始绘制小兔子了。下面是一个绘制小兔子的示例:
```python
# 绘制小兔子
turtle.penup()
turtle.goto(-50, 0)
turtle.pendown()
# 绘制身体
turtle.circle(50)
# 绘制头部
turtle.penup()
turtle.goto(0, 80)
turtle.pendown()
turtle.circle(20)
# 绘制眼睛
turtle.penup()
turtle.goto(10, 90)
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(-10, 90)
turtle.pendown()
turtle.dot(10)
# 绘制嘴巴
turtle.penup()
turtle.goto(0, 70)
turtle.pendown()
turtle.right(90)
turtle.circle(10, 180)
# 绘制耳朵
turtle.penup()
turtle.goto(20, 105)
turtle.pendown()
turtle.right(30)
turtle.forward(25)
turtle.right(120)
turtle.forward(25)
turtle.right(30)
turtle.penup()
turtle.goto(-20, 105)
turtle.pendown()
turtle.left(60)
turtle.forward(25)
turtle.right(120)
turtle.forward(25)
turtle.left(60)
# 绘制脚
turtle.penup()
turtle.goto(30, -20)
turtle.pendown()
turtle.right(30)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(60)
turtle.penup()
turtle.goto(-30, -20)
turtle.pendown()
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
# 显示画笔
turtle.showturtle()
```
最后,我们需要让画布保持显示,以便我们能够看到绘制的小兔子:
```python
# 保持画布显示
turtle.done()
```
完整的代码如下:
```python
import turtle
# 创建画布
wn = turtle.Screen()
# 设置画布背景色
wn.bgcolor("white")
# 设置画笔颜色
turtle.pencolor("black")
# 设置画笔大小
turtle.pensize(2)
# 隐藏画笔
turtle.hideturtle()
# 绘制小兔子
turtle.penup()
turtle.goto(-50, 0)
turtle.pendown()
# 绘制身体
turtle.circle(50)
# 绘制头部
turtle.penup()
turtle.goto(0, 80)
turtle.pendown()
turtle.circle(20)
# 绘制眼睛
turtle.penup()
turtle.goto(10, 90)
turtle.pendown()
turtle.dot(10)
turtle.penup()
turtle.goto(-10, 90)
turtle.pendown()
turtle.dot(10)
# 绘制嘴巴
turtle.penup()
turtle.goto(0, 70)
turtle.pendown()
turtle.right(90)
turtle.circle(10, 180)
# 绘制耳朵
turtle.penup()
turtle.goto(20, 105)
turtle.pendown()
turtle.right(30)
turtle.forward(25)
turtle.right(120)
turtle.forward(25)
turtle.right(30)
turtle.penup()
turtle.goto(-20, 105)
turtle.pendown()
turtle.left(60)
turtle.forward(25)
turtle.right(120)
turtle.forward(25)
turtle.left(60)
# 绘制脚
turtle.penup()
turtle.goto(30, -20)
turtle.pendown()
turtle.right(30)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(60)
turtle.penup()
turtle.goto(-30, -20)
turtle.pendown()
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
turtle.right(120)
turtle.forward(40)
# 显示画笔
turtle.showturtle()
# 保持画布显示
turtle.done()
```
执行上述代码,即可在画布上绘制出一个可爱的小兔子。
阅读全文