用Python turtle 画猫咪
时间: 2023-11-16 22:08:28 浏览: 146
Python turtle烟花秀,参考了部分代码和turtledemo程序实例.zip
好的,让我们来画一只可爱的猫咪吧!
首先,需要导入turtle库:
```python
import turtle
```
然后,我们需要创建一个画布和一个画笔,代码如下:
```python
canvas = turtle.Screen() # 创建画布
canvas.bgcolor('#F7E7CE') # 设置画布背景色为浅黄色
pen = turtle.Turtle() # 创建画笔
pen.speed(0) # 设置画笔速度为最快
pen.hideturtle() # 隐藏画笔形状
```
接下来,我们来画猫咪的身体:
```python
# 画猫咪的身体
pen.penup()
pen.goto(-50, -50)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.circle(50)
pen.end_fill()
```
然后,画猫咪的头部和耳朵:
```python
# 画猫咪的头部和耳朵
pen.penup()
pen.goto(0, 20)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(10, 60)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.right(60)
pen.forward(30)
pen.left(120)
pen.forward(30)
pen.end_fill()
pen.penup()
pen.goto(-10, 60)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.left(120)
pen.forward(30)
pen.right(120)
pen.forward(30)
pen.end_fill()
```
然后,画猫咪的眼睛和鼻子:
```python
# 画猫咪的眼睛和鼻子
pen.penup()
pen.goto(-10, 40)
pen.pendown()
pen.color('black')
pen.begin_fill()
pen.circle(5)
pen.end_fill()
pen.penup()
pen.goto(10, 40)
pen.pendown()
pen.color('black')
pen.begin_fill()
pen.circle(5)
pen.end_fill()
pen.penup()
pen.goto(0, 20)
pen.pendown()
pen.color('black')
pen.dot(10)
pen.penup()
pen.goto(0, 10)
pen.pendown()
pen.color('black')
pen.dot(5)
```
最后,画猫咪的四肢:
```python
# 画猫咪的四肢
pen.penup()
pen.goto(-30, -70)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.right(30)
pen.forward(30)
pen.right(120)
pen.forward(30)
pen.right(60)
pen.forward(40)
pen.end_fill()
pen.penup()
pen.goto(30, -70)
pen.pendown()
pen.color('#7F462C')
pen.begin_fill()
pen.left(30)
pen.forward(30)
pen.left(120)
pen.forward(30)
pen.left(60)
pen.forward(40)
pen.end_fill()
```
最后,我们需要让画笔回到原点,然后隐藏它:
```python
pen.penup()
pen.goto(0, 0)
pen.hideturtle()
```
完整代码如下:
阅读全文