用turtle绘制一只老鼠
时间: 2023-11-19 20:05:34 浏览: 130
可以使用Python内置的turtle模块来实现此任务。以下是实现此任务的代码示例:
```python
import turtle
# 设置画布大小和背景色
turtle.setup(800, 600)
turtle.bgcolor("#FFE4B5")
# 设置画笔颜色和宽度
turtle.pensize(2)
# 绘制耳朵
turtle.penup()
turtle.goto(-100, 100)
turtle.pendown()
turtle.fillcolor("#A0522D")
turtle.begin_fill()
turtle.goto(-50, 200)
turtle.goto(0, 220)
turtle.goto(50, 200)
turtle.goto(100, 100)
turtle.end_fill()
# 绘制头部
turtle.penup()
turtle.goto(-100, 100)
turtle.pendown()
turtle.goto(-100, -50)
turtle.goto(0, -100)
turtle.goto(100, -50)
turtle.goto(100, 100)
# 绘制眼睛
turtle.penup()
turtle.goto(-30, 50)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.fillcolor("#483D8B")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
turtle.penup()
turtle.goto(30, 50)
turtle.pendown()
turtle.fillcolor("white")
turtle.begin_fill()
turtle.circle(20)
turtle.end_fill()
turtle.fillcolor("#483D8B")
turtle.begin_fill()
turtle.circle(10)
turtle.end_fill()
# 绘制鼻子
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.fillcolor("#A0522D")
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()
# 绘制胡须
turtle.penup()
turtle.goto(-50, -20)
turtle.pendown()
turtle.goto(-100, -40)
turtle.penup()
turtle.goto(-50, -30)
turtle.pendown()
turtle.goto(-100, -30)
turtle.penup()
turtle.goto(-50, -40)
turtle.pendown()
turtle.goto(-100, -20)
turtle.penup()
turtle.goto(50, -20)
turtle.pendown()
turtle.goto(100, -40)
turtle.penup()
turtle.goto(50, -30)
turtle.pendown()
turtle.goto(100, -30)
turtle.penup()
turtle.goto(50, -40)
turtle.pendown()
turtle.goto(100, -20)
# 绘制身体
turtle.penup()
turtle.goto(-100, -50)
turtle.pendown()
turtle.fillcolor("#A0522D")
turtle.begin_fill()
turtle.goto(-100, -200)
turtle.goto(100, -200)
turtle.goto(100, -50)
turtle.end_fill()
# 绘制尾巴
turtle.penup()
turtle.goto(100, -70)
turtle.pendown()
turtle.fillcolor("#A0522D")
turtle.begin_fill()
turtle.goto(130, -150)
turtle.goto(120, -170)
turtle.goto(90, -90)
turtle.end_fill()
# 隐藏画笔
turtle.hideturtle()
# 等待点击关闭窗口
turtle.done()
```
运行上述代码后,会在窗口中绘制出一只可爱的老鼠。结果如下图所示:
![turtle_mouse.png](https://cdn.jsdelivr.net/gh/Yilong94/blog-images/images/turtle_mouse.png)
阅读全文