Python 绘制春联

本次,我们使用turtle模块绘制春联。


效果:

Python 绘制春联_第1张图片

代码详细教学:

1.导入模块

from turtle import *

2.设置属性

bgcolor("lightsalmon")
pensize(5)
setup(1400,1000)
update()

3.设置门的颜色

fillcolor("chocolate")
pencolor("brown")

4.移动到左边那扇门的左上角坐标

pu()
goto(-330,200)
seth(-90)
pd()

5.绘制左边的门

begin_fill()
for i in range(2):
    fd(560)
    left(90)
    fd(180)
    left(90)
end_fill()

6.移动到第二扇门的左上角(第一扇门的右上角)

pu()
goto(-150,200)
seth(-90)
pd()

7.绘制第二扇门

begin_fill()
for i in range(2):
    fd(560)
    left(90)
    fd(180)
    left(90)
end_fill()

8.设置门把手填充颜色

fillcolor("lightsalmon")

9.移动到两扇门的中点,向左移动40像素,右转90°(朝向上方)

pu()
goto(-150,-80)
seth(180)
fd(40)
right(90)
pd()

10.绘制左边门的把手

begin_fill()
circle(20)
end_fill()

11.移动到右边门把手画圆的起点,与第九步对称

pu()
seth(0)
fd(80)
right(90)
pd()

12.绘制右边门的把手

begin_fill()
circle(20)
end_fill()

13.设置对联的画笔和填充颜色

fillcolor("red")
pencolor("firebrick2")

14.移动到横批左下角

pu()
goto(-330,250)
seth(0)
pd()

15.绘制横批轮廓并填充

begin_fill()
for i in range(2):
    fd(360)
    left(90)
    fd(100)
    left(90)
end_fill()

16.设置文字颜色并抬笔

pencolor("black")
pu()

17.绘制横批文字

texts=["安","国","泰","民"]
for text in texts:
    fd(72)
    write(text,align="center",font=("华文行楷",60,"normal"))

18.设置对联画笔颜色

pencolor("firebrick2")

19.移动到上联右上角

goto(-380,200)
seth(180)
pd()

20.绘制上联

begin_fill()
for i in range(2):
    fd(100)
    left(90)
    fd(560)
    left(90)
end_fill()

21.设置上联文字格式

pu()
goto(-430,200)
pencolor("black")
seth(-90)

22.书写上联文字

texts="民安国泰逢盛世"
for text in texts:
    fd(80)
    write(text,align="center",font=("华文行楷",60,"normal"))

23.设置下联的属性和坐标

pencolor("firebrick2")
goto(180,200)
seth(180)
pd()

24.绘制下联

begin_fill()
for i in range(2):
    fd(100)
    left(90)
    fd(560)
    left(90)
end_fill()

25.设置下联文字格式

pu()
goto(130,200)
pencolor("black")
seth(-90)

26.书写下联文字

texts="风调雨顺颂华年"
for text in texts:
    fd(80)
    write(text,align="center",font=("华文行楷",60,"normal"))

27.设置雪人颜色和属性

pencolor("black")
pensize(2)
fillcolor("white")

28.移动到雪人头和身体的连接处

pu()
goto(400,-100)
seth(0)
pd()

29.绘制头和身体的轮廓并填充白色

begin_fill()
circle(65)
end_fill()

begin_fill()
circle(-105)
end_fill()

30.设置眼睛画笔粗细

pensize(6)

31.移动到眼睛位置

pu()
goto(355,-10)
seth(50)
pd()

32.绘制眼睛弧线

circle(-25,100)

33.移动到鼻子位置

pu()
goto(355,-40)
seth(-45)
pd()

34.设置属性

fillcolor("orange")
pensize(3)

35.绘制鼻子(挺像胡萝卜)

begin_fill()
circle(-15,90)
goto(295,-55)
goto(355,-40)
end_fill()

36.移动到身体和头部的连接处,准备画纽扣

pu()
goto(400,-100)
seth(-90)

37.绘制纽扣

colors=["red","orange","blue","green"]
for i in range(4):
    fd(42)
    dot(20,colors[i])

38.设置画笔粗细

pensize(3)

39.移动到雪人手臂和身体的连接处

goto(420,-150)
seth(-75)
pd()

40.绘制雪人的手臂和手指

fd(100)
right(20)
for i in range(3):
    fd(30)
    bk(30)
    left(20)

41.设置画笔颜色、填充颜色和画笔粗细

fillcolor("black")
pencolor("black")
pensize(3)

42.定义绘制两个八分音符的函数

def shape1(pos):
    pu()
    goto(pos)
    seth(115)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(40)
    right(80)
    fd(60)
    right(100)
    fd(40)

    begin_fill()
    circle(-5)
    end_fill()

43.定义绘制一个八分音符的函数

def shape2(pos):
    pu()
    goto(pos)
    seth(60)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(40)
    right(180)
    circle(20,40)
    circle(-20,45)

44.定义绘制一个四分音符的函数

def shape3(pos):
    pu()
    goto(pos)
    seth(90)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(45)

45.绘制春联右上方的几个小音符,作为装饰

shape1((200,280))
shape2((300,240))
shape3((340,180))

46.绘制春联左上方的几个小音符,作为装饰

shape3((-400,320))
shape2((-480,280))
shape1((-455,380))

47.隐藏画笔并保持窗口显示

ht()
done()

呼……终于完成了!!

看看最终代码:

from turtle import *

bgcolor("lightsalmon")
pensize(5)
setup(1400,1000)
update()

fillcolor("chocolate")
pencolor("brown")

pu()
goto(-330,200)
seth(-90)
pd()

begin_fill()
for i in range(2):
    fd(560)
    left(90)
    fd(180)
    left(90)
end_fill()

pu()
goto(-150,200)
seth(-90)
pd()

begin_fill()
for i in range(2):
    fd(560)
    left(90)
    fd(180)
    left(90)
end_fill()

fillcolor("lightsalmon")

pu()
goto(-150,-80)
seth(180)
fd(40)
right(90)
pd()

begin_fill()
circle(20)
end_fill()

pu()
seth(0)
fd(80)
right(90)
pd()

begin_fill()
circle(20)
end_fill()

fillcolor("red")
pencolor("firebrick2")

pu()
goto(-330,250)
seth(0)
pd()

begin_fill()
for i in range(2):
    fd(360)
    left(90)
    fd(100)
    left(90)
end_fill()

pencolor("black")
pu()

texts=["安","国","泰","民"]
for text in texts:
    fd(72)
    write(text,align="center",font=("华文行楷",60,"normal"))

pencolor("firebrick2")

goto(-380,200)
seth(180)
pd()

begin_fill()
for i in range(2):
    fd(100)
    left(90)
    fd(560)
    left(90)
end_fill()

pu()
goto(-430,200)
pencolor("black")
seth(-90)

texts="民安国泰逢盛世"
for text in texts:
    fd(80)
    write(text,align="center",font=("华文行楷",60,"normal"))

pencolor("firebrick2")
goto(180,200)
seth(180)
pd()

begin_fill()
for i in range(2):
    fd(100)
    left(90)
    fd(560)
    left(90)
end_fill()

pu()
goto(130,200)
pencolor("black")
seth(-90)

texts="风调雨顺颂华年"
for text in texts:
    fd(80)
    write(text,align="center",font=("华文行楷",60,"normal"))

# snowman

pencolor("black")
pensize(2)
fillcolor("white")

pu()
goto(400,-100)
seth(0)
pd()

begin_fill()
circle(65)
end_fill()

begin_fill()
circle(-105)
end_fill()

pensize(6)

pu()
goto(355,-10)
seth(50)
pd()

circle(-25,100)

pu()
goto(355,-40)
seth(-45)
pd()

fillcolor("orange")
pensize(3)

begin_fill()
circle(-15,90)
goto(295,-55)
goto(355,-40)
end_fill()

pu()
goto(400,-100)
seth(-90)

colors=["red","orange","blue","green"]
for i in range(4):
    fd(42)
    dot(20,colors[i])

pensize(3)

goto(420,-150)
seth(-75)
pd()

fd(100)
right(20)
for i in range(3):
    fd(30)
    bk(30)
    left(20)

fillcolor("black")
pencolor("black")
pensize(3)

def shape1(pos):
    pu()
    goto(pos)
    seth(115)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(40)
    right(80)
    fd(60)
    right(100)
    fd(40)

    begin_fill()
    circle(-5)
    end_fill()

def shape2(pos):
    pu()
    goto(pos)
    seth(60)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(40)
    right(180)
    circle(20,40)
    circle(-20,45)

def shape3(pos):
    pu()
    goto(pos)
    seth(90)
    pd()

    begin_fill()
    circle(5)
    end_fill()

    fd(45)

shape1((200,280))
shape2((300,240))
shape3((340,180))

shape3((-400,320))
shape2((-480,280))
shape1((-455,380))

ht()
done()

就200多行,不算太多。


春节快到了,在这里祝大家阖家幸福,心想事成!

制作不易,喜欢的话记得点赞关注哦!

你可能感兴趣的:(Turtle画图,python,开发语言,后端)