python的turtle绘制喜羊羊
时间: 2024-05-09 11:13:22 浏览: 168
Python的turtle库是一种基于Python语言的图形化绘图工具,可以用它来绘制各种形状的图案,包括喜羊羊。要绘制喜羊羊,可以按照以下步骤进行:
1. 导入turtle库
```
import turtle
```
2. 定义一个画笔
```
pen = turtle.Turtle()
```
3. 绘制喜羊羊的头部
```
pen.pensize(4)
pen.color('black', 'white')
pen.begin_fill()
pen.circle(100)
pen.end_fill()
```
4. 绘制喜羊羊的眼睛
```
pen.penup()
pen.goto(-40, 120)
pen.pendown()
pen.color('black', 'black')
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(40, 120)
pen.pendown()
pen.begin_fill()
pen.circle(20)
pen.end_fill()
pen.penup()
pen.goto(-30, 130)
pen.pendown()
pen.color('white', 'white')
pen.begin_fill()
pen.circle(10)
pen.end_fill()
pen.penup()
pen.goto(50, 130)
pen.pendown()
pen.begin_fill()
pen.circle(10)
pen.end_fill()
```
5. 绘制喜羊羊的鼻子
```
pen.penup()
pen.goto(0, 90)
pen.pendown()
pen.color('black', 'red')
pen.begin_fill()
pen.circle(30)
pen.end_fill()
pen.penup()
pen.goto(-10, 80)
pen.pendown()
pen.color('white', 'white')
pen.begin_fill()
pen.circle(10)
pen.end_fill()
```
6. 绘制喜羊羊的嘴巴
```
pen.penup()
pen.goto(-30, 60)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
for i in range(120):
pen.forward(1)
pen.right(3)
for i in range(60):
pen.forward(1)
pen.right(1.5)
for i in range(120):
pen.forward(1)
pen.right(3)
for i in range(60):
pen.forward(1)
pen.right(1.5)
pen.end_fill()
```
7. 绘制喜羊羊的身体和四肢
```
# 身体
pen.penup()
pen.goto(-150, -100)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
for i in range(2):
pen.forward(300)
pen.left(90)
pen.forward(200)
pen.left(90)
pen.end_fill()
# 四肢
for i in range(2):
pen.penup()
pen.goto(-150 + i * 300, -100)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.circle(50, 180)
pen.forward(100)
pen.circle(-50, 180)
pen.end_fill()
# 脚
for i in range(2):
pen.penup()
pen.goto(-150 + i * 300, -200)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
pen.circle(30)
pen.end_fill()
# 尾巴
pen.penup()
pen.goto(-100, -200)
pen.pendown()
pen.color('black', 'white')
pen.begin_fill()
for i in range(4):
pen.forward(50)
pen.right(90)
pen.forward(50)
pen.left(90)
for i in range(2):
pen.forward(50)
pen.right(90)
pen.forward(100)
pen.right(90)
for i in range(2):
pen.forward(50)
pen.left(90)
pen.forward(100)
pen.left(90)
# 头顶毛发
for i in range(2):
pen.penup()
if i == 0:
x, y = -20, 180
angle = 45
length = 70
width = 15
color = 'orange'
else:
x, y = 20, 180
angle = -45
length = -70
width = -15
color = 'yellow'
pen.goto(x, y)
pen.setheading(angle)
pen.pendown()
pen.pensize(abs(width))
pen.color(color, color)
for j in range(abs(width)):
pen.forward(abs(length))
if width > 0:
length -= 2
else:
length += 2
pen.right(angle / abs(angle) * (90 / abs(width)))
```
8. 最后,隐藏画笔并展示绘制结果
```
pen.hideturtle()
turtle.done()
```
阅读全文