python turtle画猪猪侠的图像
时间: 2023-09-08 07:08:18 浏览: 256
python用turtle画出给定图片的图像、校徽等复杂图像都可以
5星 · 资源好评率100%
好的,以下是Python Turtle画猪猪侠的代码:
```python
import turtle
# 设置画笔
t = turtle.Turtle()
t.speed(0) # 设置画笔速度
# 画圆
def draw_circle(x, y, radius, color):
t.penup()
t.goto(x, y)
t.fillcolor(color)
t.pendown()
t.begin_fill()
t.circle(radius)
t.end_fill()
# 画鼻子
def draw_nose():
draw_circle(0, -70, 50, "#ff5e5e")
draw_circle(0, -90, 20, "#000000")
# 画眼睛
def draw_eyes():
draw_circle(-40, 40, 30, "#ffffff")
draw_circle(40, 40, 30, "#ffffff")
draw_circle(-40, 50, 15, "#000000")
draw_circle(40, 50, 15, "#000000")
# 画脸
def draw_face():
draw_circle(0, 0, 100, "#ff5e5e")
# 画帽子
def draw_hat():
t.penup()
t.goto(-120, 110)
t.pendown()
t.fillcolor("#000000")
t.begin_fill()
t.forward(240)
t.left(90)
t.forward(60)
t.left(90)
t.forward(240)
t.end_fill()
draw_circle(0, 170, 80, "#000000")
draw_circle(0, 200, 20, "#ffffff")
# 画猪鼻
def draw_pig_nose():
draw_circle(0, -70, 30, "#000000")
draw_circle(0, -60, 10, "#ffffff")
# 画猪耳朵
def draw_pig_ears():
t.penup()
t.goto(-70, 150)
t.pendown()
t.fillcolor("#ff5e5e")
t.begin_fill()
t.right(45)
t.forward(60)
t.right(90)
t.forward(60)
t.right(90)
t.forward(60)
t.end_fill()
t.penup()
t.goto(70, 150)
t.pendown()
t.fillcolor("#ff5e5e")
t.begin_fill()
t.left(45)
t.forward(60)
t.left(90)
t.forward(60)
t.left(90)
t.forward(60)
t.end_fill()
# 画身体
def draw_body():
t.penup()
t.goto(0, -100)
t.pendown()
t.fillcolor("#ff5e5e")
t.begin_fill()
t.forward(200)
t.right(90)
t.forward(150)
t.right(90)
t.forward(200)
t.right(90)
t.forward(150)
t.end_fill()
# 画猪猪侠
def draw_pig():
draw_body()
draw_pig_ears()
draw_pig_nose()
# 画完整的猪猪侠
def draw_piggy():
draw_face()
draw_nose()
draw_eyes()
draw_hat()
draw_pig()
# 画猪猪侠
draw_piggy()
# 隐藏画笔
t.hideturtle()
turtle.done()
```
运行代码后,会弹出一个窗口,里面会显示猪猪侠的图像。
阅读全文