使用turtle库绘制一只老虎
时间: 2023-07-20 07:38:31 浏览: 178
好的,让我来为您展示如何使用turtle库绘制一只老虎。
首先,我们需要导入turtle库并创建一个画布:
```python
import turtle
canvas = turtle.Screen()
canvas.title("老虎")
canvas.bgcolor("white")
```
接下来,我们可以创建一个turtle对象并设置其属性:
```python
tiger = turtle.Turtle()
tiger.color("black", "orange")
tiger.pensize(3)
tiger.speed(10)
```
现在,我们可以开始绘制老虎了。以下是绘制老虎的代码:
```python
# 绘制身体
tiger.begin_fill()
tiger.penup()
tiger.goto(-100, -50)
tiger.pendown()
tiger.goto(100, -50)
tiger.goto(100, 50)
tiger.goto(-100, 50)
tiger.goto(-100, -50)
tiger.end_fill()
# 绘制头部
tiger.begin_fill()
tiger.penup()
tiger.goto(100, 50)
tiger.pendown()
tiger.circle(-50)
tiger.end_fill()
# 绘制眼睛
tiger.penup()
tiger.goto(70, 80)
tiger.pendown()
tiger.begin_fill()
tiger.circle(10)
tiger.end_fill()
tiger.penup()
tiger.goto(60, 80)
tiger.pendown()
tiger.color("white")
tiger.begin_fill()
tiger.circle(5)
tiger.end_fill()
tiger.penup()
tiger.goto(-70, 80)
tiger.pendown()
tiger.begin_fill()
tiger.circle(10)
tiger.end_fill()
tiger.penup()
tiger.goto(-60, 80)
tiger.pendown()
tiger.color("white")
tiger.begin_fill()
tiger.circle(5)
tiger.end_fill()
# 绘制鼻子和嘴巴
tiger.penup()
tiger.goto(0, 50)
tiger.pendown()
tiger.color("black", "black")
tiger.begin_fill()
tiger.circle(20)
tiger.end_fill()
tiger.penup()
tiger.goto(0, 30)
tiger.pendown()
tiger.color("white")
tiger.begin_fill()
tiger.circle(10)
tiger.end_fill()
# 绘制四肢
tiger.penup()
tiger.goto(-100, -50)
tiger.pendown()
tiger.goto(-120, -100)
tiger.goto(-70, -100)
tiger.goto(-100, -50)
tiger.penup()
tiger.goto(100, -50)
tiger.pendown()
tiger.goto(120, -100)
tiger.goto(70, -100)
tiger.goto(100, -50)
```
最后,我们可以调用turtle库的done()方法来保持窗口打开状态,直到我们手动关闭它。
```python
turtle.done()
```
这样,一只可爱的老虎就完成了!
阅读全文