Python-100-Days/Day01-15/Day01/code/peppa_pig.py

205 lines
3.4 KiB
Python
Raw Normal View History

2018-12-22 16:26:07 +08:00
"""
绘制小猪佩奇
"""
from turtle import *
2018-06-03 20:14:37 +08:00
2018-12-22 16:26:07 +08:00
def nose(x,y):
"""画鼻子"""
penup()
# 将海龟移动到指定的坐标
goto(x,y)
pendown()
# 设置海龟的方向0-东、90-北、180-西、270-南)
setheading(-30)
begin_fill()
a = 0.4
2018-06-03 20:14:37 +08:00
for i in range(120):
2018-12-22 16:26:07 +08:00
if 0 <= i < 30 or 60 <= i <90:
a = a + 0.08
# 向左转3度
left(3)
# 向前走
forward(a)
2018-06-03 20:14:37 +08:00
else:
2018-12-22 16:26:07 +08:00
a = a - 0.08
2018-06-03 20:14:37 +08:00
left(3)
forward(a)
2018-12-22 16:26:07 +08:00
end_fill()
2018-06-03 20:14:37 +08:00
penup()
setheading(90)
forward(25)
setheading(0)
forward(10)
pendown()
2018-12-22 16:26:07 +08:00
# 设置画笔的颜色(红, 绿, 蓝)
pencolor(255, 155, 192)
2018-06-03 20:14:37 +08:00
setheading(10)
begin_fill()
circle(5)
2018-12-22 16:26:07 +08:00
color(160, 82, 45)
2018-06-03 20:14:37 +08:00
end_fill()
penup()
setheading(0)
forward(20)
pendown()
2018-12-22 16:26:07 +08:00
pencolor(255, 155, 192)
2018-06-03 20:14:37 +08:00
setheading(10)
begin_fill()
circle(5)
2018-12-22 16:26:07 +08:00
color(160, 82, 45)
2018-06-03 20:14:37 +08:00
end_fill()
2018-12-22 16:26:07 +08:00
def head(x, y):
"""画头"""
color((255, 155, 192), "pink")
2018-06-03 20:14:37 +08:00
penup()
goto(x,y)
setheading(0)
pendown()
begin_fill()
setheading(180)
2018-12-22 16:26:07 +08:00
circle(300, -30)
circle(100, -60)
circle(80, -100)
circle(150, -20)
circle(60, -95)
2018-06-03 20:14:37 +08:00
setheading(161)
2018-12-22 16:26:07 +08:00
circle(-300, 15)
2018-06-03 20:14:37 +08:00
penup()
2018-12-22 16:26:07 +08:00
goto(-100, 100)
2018-06-03 20:14:37 +08:00
pendown()
setheading(-30)
2018-12-22 16:26:07 +08:00
a = 0.4
2018-06-03 20:14:37 +08:00
for i in range(60):
2018-12-22 16:26:07 +08:00
if 0<= i < 30 or 60 <= i < 90:
a = a + 0.08
2018-06-03 20:14:37 +08:00
lt(3) #向左转3度
fd(a) #向前走a的步长
else:
2018-12-22 16:26:07 +08:00
a = a - 0.08
2018-06-03 20:14:37 +08:00
lt(3)
fd(a)
end_fill()
2018-12-22 16:26:07 +08:00
def ears(x,y):
"""画耳朵"""
color((255, 155, 192), "pink")
2018-06-03 20:14:37 +08:00
penup()
2018-12-22 16:26:07 +08:00
goto(x, y)
2018-06-03 20:14:37 +08:00
pendown()
begin_fill()
setheading(100)
2018-12-22 16:26:07 +08:00
circle(-50, 50)
circle(-10, 120)
circle(-50, 54)
2018-06-03 20:14:37 +08:00
end_fill()
penup()
setheading(90)
forward(-12)
setheading(0)
forward(30)
pendown()
begin_fill()
setheading(100)
2018-12-22 16:26:07 +08:00
circle(-50, 50)
circle(-10, 120)
circle(-50, 56)
2018-06-03 20:14:37 +08:00
end_fill()
2018-12-22 16:26:07 +08:00
def eyes(x,y):
"""画眼睛"""
color((255, 155, 192), "white")
2018-06-03 20:14:37 +08:00
penup()
setheading(90)
forward(-20)
setheading(0)
forward(-95)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
2018-12-22 16:26:07 +08:00
color((255, 155, 192), "white")
2018-06-03 20:14:37 +08:00
penup()
seth(90)
forward(-25)
seth(0)
forward(40)
pendown()
begin_fill()
circle(15)
end_fill()
color("black")
penup()
setheading(90)
forward(12)
setheading(0)
forward(-3)
pendown()
begin_fill()
circle(3)
end_fill()
2018-12-22 16:26:07 +08:00
def cheek(x,y):
"""画脸颊"""
color((255, 155, 192))
2018-06-03 20:14:37 +08:00
penup()
goto(x,y)
pendown()
setheading(0)
begin_fill()
circle(30)
end_fill()
2018-12-22 16:26:07 +08:00
def mouth(x,y):
"""画嘴巴"""
color(239, 69, 19)
2018-06-03 20:14:37 +08:00
penup()
2018-12-22 16:26:07 +08:00
goto(x, y)
2018-06-03 20:14:37 +08:00
pendown()
setheading(-80)
2018-12-22 16:26:07 +08:00
circle(30, 40)
circle(40, 80)
2018-06-03 20:14:37 +08:00
2018-12-22 16:26:07 +08:00
def setting():
"""设置参数"""
2018-06-03 20:14:37 +08:00
pensize(4)
2018-12-22 16:26:07 +08:00
# 隐藏海龟
hideturtle()
colormode(255)
color((255, 155, 192), "pink")
setup(840, 500)
2018-06-03 20:14:37 +08:00
speed(10)
def main():
2018-12-22 16:26:07 +08:00
"""主函数"""
setting()
nose(-100, 100)
head(-69, 167)
ears(0, 160)
eyes(0, 140)
cheek(80, 10)
mouth(-20, 30)
2018-06-03 20:14:37 +08:00
done()
if __name__ == '__main__':
2018-12-22 16:26:07 +08:00
main()