• 技术文章 >Python技术 >Python基础教程

    Python绘图项目之海绵宝宝

    小妮浅浅小妮浅浅2021-08-12 14:42:43原创7461

    工具使用

    开发工具:pycharm

    开发环境:python3.7, Windows10

    使用工具包:turtle

    项目思路解析

    1、明确turtle基本配置。在确定画框的高度以及画笔的大小,还有对应画框标题和画笔运行的速度。

    1

    2

    3

    4

        screensize(800, 600, 'white')

        pensize(3)

        title('海绵宝宝')

    speed(19)

    2、首先调整画笔位置,找到对应原点位置。

    1

    2

    3

    4

    5

    def go_to(x, y):

        penup()

        goto(x, y)

        pendown()

    go_to(0, 0)

    3、逐步画出海绵宝宝的身体部件。

    画出海绵宝宝的头部海绵,选取对应宽度,坐标大小可自行调整 定位的函数可以重复使用,海绵宝宝周围为波浪线,添加上对应的弧度,添加上黄色。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    def head():

        go_to(-200, 180)

        fillcolor('yellow')

        begin_fill()

        seth(-30)

        for _ in range(6):

            circle(36, 60)

            circle(-36, 60)

        seth(-125)

        for _ in range(5):

            circle(40,60)

            circle(-40,60)

        seth(-210)

        for _ in range(4):

            circle(45,60)

            circle(-45,60)

        seth(65)

        for _ in range(5):

            circle(40,60)

            circle(-40,60)

        end_fill()

    给海绵宝宝添加上面部表情,眼睛的数据多为圆弧,鼻子为小倒勾尺寸可自行调整。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    def eye():

        # 眼白

        go_to(14, -5)

        fillcolor('#f0f0f0')

        begin_fill()

        circle(65, 360)

        end_fill()

        begin_fill()

        go_to(13,12)

        seth(98)

        circle(-65,360)

        end_fill()

        #眼球

        go_to(-10,20)

        fillcolor('blue')

        begin_fill()

        circle(20,360)

        end_fill()

        go_to(-22,20)

        fillcolor('black')

        begin_fill()

        circle(7,360)

        end_fill()

        go_to(40,15)

        fillcolor('blue')

        begin_fill()

        circle(-20, 360)

        end_fill()

        go_to(53,15)

        fillcolor('black')

        begin_fill()

        circle(-7,360)

        end_fill()

        #睫毛

        go_to(-95,65)

        left(20)

        forward(40)

        go_to(-50,87)

        right(25)

        forward(32)

        go_to(0,70)

        right(25)

        forward(40)

        go_to(40, 75)

        left(35)

        forward(40)

        go_to(90, 87)

        right(18)

        forward(30)

        go_to(120, 70)

        right(25)

        forward(40)

    def nose():

        fillcolor('yellow')

        go_to(0, -7)

        begin_fill()

        right(50)

        circle(-60, 30)

        color('yellow')

        goto(15,-40)

        end_fill()

        color('black')

        go_to(0, -7)

        seth(-75)

        forward(30)

        go_to(30,-7)

        seth(-105)

    面部完成之后开始完善身体的一些结构增加小白衬衫和海绵宝宝的手臂。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    def body():

        go_to(-170,-180)

        seth(-120)

        circle(150, 30)

        seth(0)

        forward(40)

        seth(100)

        forward(35)

        seth(-80)

        forward(100)

        fillcolor('brown')

        begin_fill()

        seth(0)

        forward(300)

        seth(80)

        forward(110)

        seth(-100)

        forward(65)

        seth(180)

        forward(315)

        go_to(-118,-400)

        end_fill()

        go_to(-170,-255)

        fillcolor('yellow')

        begin_fill()

        seth(-75)

        forward(80)

        seth(0)

        forward(17)

        seth(105)

        forward(85)

        end_fill()

        go_to(200, -170)

        seth(-60)

        circle(-150,30)

        seth(-180)

        forward(45)

        begin_fill()

        seth(0)

        forward(20)

        seth(-100)

        forward(85)

        seth(180)

        forward(20)

    end_fill()

    最后一步给添加海绵宝宝的红色红领巾

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    def tie():

        go_to(-50,-225)

        seth(-40)

        forward(40)

        seth(30)

        forward(52)

        go_to(30,-225)

        seth(-30)

        forward(40)

        seth(40)

        forward(45)

        fillcolor('red')

        go_to(0, -240)

        begin_fill()

        seth(-60)

        forward(10)

        seth(0)

        forward(30)

        seth(60)

        forward(15)

        go_to(30,-225)

        end_fill()

        go_to(4,-250)

        begin_fill()

        seth(-100)

        forward(80)

        seth(0)

        forward(55)

        seth(100)

        forward(80)

        end_fill()

    以上就是Python绘图项目之海绵宝宝的画法,希望对大家有所帮助。更多Python学习指路:python基础教程

    专题推荐:python绘图
    上一篇:python链表实现左移和右移 下一篇:python如何快速建立生成器

    相关文章推荐

    • Python布尔索引的使用• Python位置索引的介绍• Python yield关键字的应用限制• Python yield实现迭代器协议• python列表删除项目的方法• Python删除列表中的非字母字符• python创建实例中类属性的变化• python默认参数的使用注意• python变长参数的使用注意• python变长参数的使用场景• python建立文本文件的两种途径• python多行读写的操作• python如何解决黏包问题• python链表的乘法问题• python链表实现左移和右移

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网