用代码画一只懒羊羊

文章目录

  • 简介
  • 代码
  • 运行效果
  • 备注

简介

心血来潮,想用 Python 的 Turtle 库做点绘画工作,于是画了一只可爱的懒羊羊,如下:
用代码画一只懒羊羊_第1张图片
绘画过程可在下面平台查看:

抖音:
用代码画一只懒羊羊
b站:
用代码画一只懒羊羊_哔哩哔哩_bilibili

代码

# coding=gbk

import turtle

def plotLine(points, pencolor=None, width=None, speed=None):
    '''
    功能:画折线
    参数:
    - points : 一系列点,用列表或元组表示
    - pencolor : 画笔颜色,默认不变
    - width : 画笔宽度,默认不变
    - speed : 绘制速度,默认不变
    '''
    # 记录旧参数
    oldpencolor = turtle.pencolor()
    oldwidth = turtle.width()
    oldspeed = turtle.speed()

    # 修改新参数
    if pencolor is not None:
        turtle.pencolor(pencolor)
    if width is not None:
        turtle.width(width)
    if speed is not None:
        turtle.speed(speed)
    
    # 绘制折线
    turtle.up()
    turtle.goto(points[0])
    turtle.down()
    for point in points[1:]:
        turtle.goto(point)
    
    # 恢复旧参数
    turtle.pencolor(oldpencolor)
    turtle.width(oldwidth)
    turtle.speed(oldspeed)


def plotPoly(points, fill=False, pencolor=None, fillcolor=None,
             width=None, speed=None):
    '''
    功能:绘制封闭多边形
    '''
    # 保存旧参数
    oldfillcolor = turtle.fillcolor()

    # 更新新参数
    if fillcolor is not None:
        turtle.fillcolor(fillcolor)

    # 绘制封闭多边形
    points_plotline = list(points) + [points[0]]
    if fill:
        turtle.begin_fill()
        plotLine(points_plotline, pencolor, width, speed)
        turtle.end_fill()
    else:
        plotLine(points_plotline, pencolor, width, speed)

    # 恢复旧参数
    turtle.fillcolor(oldfillcolor)

# 设置一些参数
turtle.setup(680, 680)
# turtle.speed(100)

# 绘画

# 脸部轮廓
points = [
    (-131, 76), (-121, 85), (-112, 94), (-105, 105),
    (-113, 109), (-105, 106), (-97, 106), (-89, 106), (-83, 110), 
    (-77, 115), (-69, 120), (-60, 118), (-53, 117), (-44, 117), 
    (-35, 120), (-26, 126), (-17, 122), (-10, 117), (-3, 114), 
    (6, 111), (16, 110), (26, 112), (32, 115), (36, 117), 
    (42, 112), (47, 107), (53, 102), (61, 98), (71, 96), 
    (80, 96), (89, 97), (98, 99), (104, 92), (108, 86), 
    (114, 78), (121, 71), (128, 66), (139, 62), (138, 53), 
    (138, 42), (139, 32), (143, 25), (149, 17), (154, 9), 
    (157, 6), (154, 2), (152, -1), (147, -5), (143, -12), 
    (140, -20), (138, -28), (137, -38), (139, -46), (139, -52), 
    (132, -57), (124, -63), (120, -70), (115, -76), (113, -86), 
    (109, -96), (100, -103), (94, -111), (90, -119), (89, -125), 
    (90, -131), (94, -137), (97, -142),
    (93, -149), (87, -150), (71, -153), (55, -156), (42, -156), 
    (28, -157), (12, -157), (-2, -155), (-16, -153), (-27, -151), 
    (-37, -147), (-50, -144), (-61, -140), (-71, -137), (-82, -133), 
    (-91, -126), (-102, -119), (-112, -111), (-120, -102), (-127, -94), 
    (-131, -89), (-136, -79), (-139, -71), (-141, -60), (-143, -49), 
    (-141, -38), (-140, -27), (-138, -17), (-135, -8), (-132, 1), 
    (-131, 11), (-132, 22), (-130, 33), (-129, 44), (-126, 55), 
    (-123, 67), (-121, 76), (-121, 85)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         width=2, fillcolor=(1, 0.82, 0.71))

# 左胳膊
points = [
    (93, -147), (97, -143), (102, -138), (111, -134), (119, -131), 
    (130, -128), (143, -126), (149, -120), (158, -117), (166, -113), 
    (175, -110), (177, -103), (182, -95), (191, -89), (199, -87), 
    (204, -85), (210, -86), (214, -88), (217, -93), (221, -96), 
    (224, -100), (224, -104), (223, -108), (225, -112), (226, -118), 
    (224, -124), (219, -128), (215, -129), (210, -127), (205, -123), 
    (208, -118), (203, -114), (203, -110), (209, -109), (213, -105),
    (214, -130),
    (206, -135), (202, -137), (197, -135), (192, -132), (187, -129), 
    (186, -132), (180, -136), (170, -140), (162, -144), (151, -149), 
    (139, -155), (128, -159), (118, -161), (110, -162), (104, -163), 
    (99, -160), (94, -156), (93, -150), (95, -147)
    ]
plotPoly(points, True, pencolor=(1, 0.82, 0.71),
         fillcolor=(1, 0.82, 0.71), width=2)
plotLine(points[:35], pencolor=(0.67, 0.5, 0.22), width=2)
plotLine(points[36:], pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (197, -119), (202, -116), (202, -111), (202, -106), (202, -102), 
    (207, -105), (214, -104), (217, -101), (218, -99), (217, -96), 
    (216, -93), (212, -91), (207, -88), (201, -86)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (223, -111), (219, -114), (215, -117), (210, -117), (205, -117), 
    (203, -114)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)

# 右胳膊
points = [
    (-126, -98), (-133, -94), (-141, -90), (-146, -88), (-151, -85), 
    (-154, -78), (-157, -72), (-162, -68), (-168, -64), (-179, -61), 
    (-184, -59), (-189, -61), (-192, -64), (-196, -69), (-199, -73), 
    (-201, -78), (-199, -85), (-201, -93), (-200, -98), (-196, -102), 
    (-190, -102), (-188, -106), (-184, -108), (-179, -110), (-171, -108), 
    (-163, -103), (-165, -106), (-157, -111), (-149, -116), (-140, -120), 
    (-131, -125), (-118, -133), (-110, -136), (-99, -139), (-97, -136), 
    (-97, -133), (-95, -126), (-98, -123), (-104, -118), (-111, -112), 
    (-119, -103)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 0.82, 0.71), width=2)
points = [
    (-178, -62), (-185, -63), (-193, -67), (-194, -74), (-191, -79), 
    (-186, -79), (-180, -78), (-179, -85), (-179, -88), (-178, -91), 
    (-175, -93)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (-192, -79), (-189, -83), (-185, -85), (-180, -83), (-179, -89), 
    (-183, -93), (-187, -91), (-192, -90), (-198, -86)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (-185, -94), (-181, -97), (-187, -102)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)

# 肚子
points = [
    (-95, -127), (-97, -135), (-96, -142), (-104, -147), (-109, -152), 
    (-112, -156), (-116, -165), (-119, -173), (-121, -182), (-120, -188), 
    (-117, -198), (-114, -208), (-118, -211), (-122, -218), (-123, -228), 
    (-122, -238), (-109, -261), (-103, -266), (-103, -275), (-97, -281), 
    (-91, -289), (-83, -292), (-74, -297), (-64, -299), (-55, -298), 
    (-46, -295), (-40, -301), (-35, -306), (-27, -310), (-16, -312), 
    (-8, -311), (2, -307), (6, -303), (13, -300), (20, -304), 
    (26, -309), (34, -314), (45, -315), (55, -314), (62, -310), 
    (70, -305), (77, -296), (89, -295), (96, -291), (103, -286), 
    (111, -277), (113, -269), (116, -258), (116, -251), (113, -242), 
    (119, -238), (122, -231), (125, -221), (125, -208), (123, -197), 
    (116, -187), (116, -177), (117, -168), (117, -163), (111, -162), 
    (102, -163), (95, -157), (93, -149), (87, -150), (78, -150), 
    (72, -154), (60, -154), (56, -156), (54, -165), (56, -175), 
    (54, -184), (56, -196), (53, -211), (55, -223), (54, -231), 
    (51, -239), (44, -243), (37, -245), (34, -246), (20, -246), 
    (5, -246), (-33, -241), (-56, -237), (-80, -233), (-90, -230), 
    (-94, -227), (-96, -219), (-98, -210), (-95, -201), (-95, -194), 
    (-91, -181), (-88, -163), (-82, -147), (-77, -134), (-86, -131)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 1, 1), width=2)

# 衣服
points = [
    (56, -156), (54, -165), (56, -175), 
    (54, -184), (56, -196), (53, -211), (55, -223), (54, -231), 
    (51, -239), (44, -243), (37, -245), (34, -246), (20, -246), 
    (5, -246), (-33, -241), (-56, -237), (-80, -233), (-90, -230), 
    (-94, -227), (-96, -219), (-98, -210), (-95, -201), (-95, -194), 
    (-91, -181), (-88, -163), (-82, -147), (-77, -134),
    (-71, -138), (-61, -141), (-51, -145), (-40, -147), (-31, -150), 
    (-20, -152), (-9, -155), (1, -156), (12, -157), (22, -157), 
    (34, -158), (46, -156)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(0.97, 0.81, 0.31), width=2)
points = [(-96, -212), (52, -227), (52, -234), (-95, -220)]
plotPoly(points, True, pencolor=(1, 1, 1),
         fillcolor=(1, 1, 1), width=1)
points = [(-91, -228), (45, -240)]
plotLine(points, pencolor=(1, 1, 1), width=2)

# 羊腿(右)
points = [
    (-107, -234), (-106, -237), (-110, -239), (-114, -238), (-120, -238), 
    (-123, -237), (-128, -239), (-134, -240), (-142, -245), (-142, -235), 
    (-144, -231), (-147, -224), (-148, -220), (-151, -215), (-157, -209), 
    (-164, -204), (-171, -204), (-177, -204), (-183, -205), (-188, -206), 
    (-191, -209), (-195, -215), (-198, -219), (-199, -224), (-199, -231), 
    (-199, -237), (-198, -244), (-198, -249), (-195, -257), (-193, -262), 
    (-190, -267), (-186, -274), (-181, -281), (-176, -284), (-172, -285), 
    (-163, -286), (-157, -282), (-152, -278), (-149, -273), (-146, -268), 
    (-139, -264), (-129, -261), (-123, -261), (-111, -259), (-101, -260), 
    (-98, -265), (-98, -260), (-101, -258), (-101, -255), (-102, -254), 
    (-103, -249), (-100, -245), (-104, -241)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 0.82, 0.71), width=2)
points = [
    (-188, -224), (-187, -219), (-186, -215), (-185, -212), (-182, -206)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (-142, -244), (-142, -249), (-143, -256), (-144, -262), (-145, -267)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)

# 羊腿(左)
points = [
    (115, -264), (123, -266), (130, -268), (135, -262), (142, -256), 
    (151, -255), (160, -255), (165, -259), (170, -263), (175, -270), 
    (177, -280), (180, -289), (181, -295), (180, -301), (179, -309), 
    (175, -315), (168, -320), (162, -323), (155, -323), (147, -323), 
    (139, -319), (135, -311), (130, -304), (129, -296), (128, -287), 
    (132, -287), (123, -287), (115, -285), (107, -284)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 0.82, 0.71), width=2)
points = [
    (153, -302), (154, -309), (155, -315), (158, -320), (159, -323)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)

# 头
points = [
    (-139, -80), (-147, -76), (-151, -73), (-156, -68), (-158, -64), 
    (-161, -59), (-164, -53), (-164, -49), (-165, -41), (-165, -36), 
    (-169, -30), (-172, -27), (-176, -17), (-177, -12), (-178, -5), 
    (-179, -1), (-179, 7), (-177, 16), (-175, 25), (-174, 29), 
    (-177, 34), (-179, 41), (-181, 47), (-182, 51), (-183, 59), 
    (-183, 64), (-181, 76), (-180, 81), (-178, 88), (-174, 94), 
    (-169, 100), (-167, 106), (-167, 114), (-167, 123), (-165, 131), 
    (-162, 139), (-161, 144), (-154, 151), (-150, 156), (-147, 160), 
    (-141, 164), (-135, 168), (-132, 170), (-130, 178), (-125, 189), 
    (-122, 195), (-117, 200), (-114, 203), (-103, 212), (-95, 215), 
    (-81, 218), (-72, 218), (-67, 223), (-63, 226), (-55, 234), 
    (-51, 238), (-43, 243), (-36, 246), (-27, 248), (-22, 254), 
    (-20, 261), (-15, 265), (-9, 267), (-1, 270), (5, 276), 
    (13, 283), (22, 284), (29, 288), (37, 288), (43, 287), 
    (50, 291), (50, 293), (59, 297), (67, 299), (76, 302), 
    (83, 306), (90, 312), (92, 318), (100, 314), (103, 311), 
    (109, 307), (115, 300), (120, 293), (123, 283), (123, 278), 
    (131, 274), (140, 271), (148, 264), (152, 256), (153, 247), 
    (152, 242), (160, 240), (165, 233), (171, 224), (182, 217), 
    (187, 213), (192, 207), (195, 202), (199, 195), (200, 189), 
    (207, 187), (216, 183), (224, 179), (264, 118), (271, 112), 
    (277, 103), (280, 93), (280, 85), (282, 77), (282, 68), 
    (279, 60), (279, 54), (279, 44), (284, 36), (286, 27), 
    (286, 13), (289, 3), (286, -8), (283, -15), (279, -23), 
    (275, -30), (269, -36), (271, -42), (272, -50), (269, -57), 
    (268, -65), (267, -69), (265, -75), (263, -80), (258, -85), 
    (254, -91), (249, -94), (242, -102), (235, -106), (223, -111)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (-27, 248), (-24, 242), (-21, 237), (-18, 232), (-13, 229), 
    (-9, 227), (-2, 224)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (1, 269), (6, 265), (11, 258), (16, 253), (24, 249), 
(30, 248), (37, 245), (47, 245)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (45, 286), (46, 280), (48, 275), (53, 271), (62, 267)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (123, 276), (121, 273), (115, 270), (112, 268), (105, 266), 
(98, 266)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (152, 244), (146, 239), (138, 235), (128, 236), (117, 236), 
(104, 237)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)
points = [
    (168, 223), (168, 217), (163, 210), (159, 205), (154, 203), 
(143, 202), (137, 201), (131, 201)
    ]
plotLine(points, pencolor=(0.67, 0.5, 0.22), width=2)

# 羊角(左)
points = [
    (194, 169), (201, 165), (209, 171), (216, 176), (224, 181), 
(231, 184), (244, 188), (258, 192), (271, 194), (281, 194), 
(288, 192), (296, 191), (301, 189), (306, 188), (311, 185), 
(316, 182), (318, 177), (319, 172), (317, 168), (313, 163), 
(311, 160), (310, 158), (306, 153), (300, 151), (293, 148), 
(291, 145), (285, 141), (281, 139), (278, 136), (275, 132), 
(272, 127), (268, 123), (265, 118), (257, 113), (251, 105), 
(254, 102), (257, 96), (254, 102), (251, 106), (245, 109), 
(238, 105), (237, 110), (236, 118), (233, 124), (229, 129), 
(225, 131), (219, 132), (216, 132), (215, 135), (215, 139), 
(216, 148), (215, 150), (214, 154), (209, 160), (205, 162)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(0.51, 0.37, 0.28), width=2)

# 羊角(右)
points = [
    (-119, 201), (-122, 205), (-124, 211), (-128, 216), (-135, 223), 
(-139, 232), (-140, 240), (-141, 249), (-137, 254), (-129, 256), 
(-119, 257), (-110, 256), (-98, 253), (-90, 250), (-81, 246), 
(-74, 242), (-66, 237), (-59, 230), (-62, 225), (-71, 217), 
(-82, 217), (-94, 215), (-104, 211), (-111, 204)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(0.51, 0.37, 0.28), width=2)

# 耳朵(左)
points = [
    (229, 68), (233, 70), (242, 70), (250, 69), (257, 65), 
(263, 58), (269, 51), (274, 44), (278, 38), (282, 31), 
(288, 25), (294, 20), (296, 19), (293, 16), (285, 12), 
(281, 9), (274, 7), (267, 6), (262, 6), (252, 6), 
(241, 9), (234, 12), (228, 18), (221, 29), (217, 40), 
(218, 52), (220, 60), (225, 64)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 0.82, 0.72), width=2)

# 耳朵(右)
points = [
    (-165, 112), (-174, 109), (-179, 106), (-187, 103), (-188, 101), 
(-185, 97), (-182, 91), (-178, 88), (-176, 90), (-174, 92), 
(-167, 99), (-165, 102), (-165, 108)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(1, 0.82, 0.72), width=2)

# 眼睛(右)
turtle.color((0.67, 0.5, 0.22), (1, 1, 1))
turtle.width(2)
turtle.up()
turtle.goto(-71, 8)
turtle.down()
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()

turtle.pencolor((0.1, 0.1, 0.1))
turtle.up()
turtle.goto(-71, 37)
turtle.down()
turtle.dot(25)

turtle.pencolor((1, 1, 1))
turtle.up()
turtle.goto((-74, 41))
turtle.down()
turtle.dot(9)

# 眼睛(左)
turtle.color((0.67, 0.5, 0.22), (1, 1, 1))
turtle.up()
turtle.goto(36, -12)
turtle.down()
turtle.begin_fill()
turtle.circle(30)
turtle.end_fill()

turtle.pencolor((0.1, 0.1, 0.1))
turtle.up()
turtle.goto(39, 18)
turtle.down()
turtle.dot(25)

turtle.pencolor((1, 1, 1))
turtle.up()
turtle.goto((34, 22))
turtle.down()
turtle.dot(9)

# 眉毛(左)
points = [
    (-82, 89), (-78, 92), (-74, 95), (-67, 97), (-60, 97), 
(-54, 95), (-48, 93), (-45, 91), (-41, 89), (-38, 86), 
(-35, 83)
    ]
plotLine(points, (0.67, 0.5, 0.22), width=2)

# 眉毛(右)
points = [
    (22, 74), (26, 77), (31, 80), (40, 82), (48, 82), 
(55, 80), (63, 78), (70, 74), (75, 70), (79, 65)
    ]
plotLine(points, (0.67, 0.5, 0.22), width=2)

# 鼻子
points = [
    (-39, 2), (-46, 0), (-52, -2), (-55, -6), (-56, -10), 
(-56, -14), (-55, -17), (-52, -20), (-48, -23), (-44, -25), 
(-40, -26), (-35, -27), (-30, -28), (-24, -27), (-21, -26), 
(-17, -23), (-15, -21), (-14, -17), (-14, -12), (-15, -9), 
(-20, -4), (-22, -3), (-26, -1), (-30, 0), (-33, 1)
    ]
plotPoly(points, True, pencolor=(0.2, 0.14, 0.11),
         fillcolor=(0.2, 0.14, 0.11), width=2)
turtle.pencolor((1, 1, 1))
turtle.up()
turtle.goto((-22, -12))
turtle.down()
turtle.dot(10)

# 嘴巴
points = [
    (-71, -44), (-69, -48), (-63, -53), (-64, -56), (-66, -66), 
(-65, -78), (-65, -87), (-62, -92), (-59, -99), (-56, -103), 
(-50, -106), (-47, -109), (-39, -111), (-29, -111), (-23, -110), 
(-18, -108), (-13, -105), (-10, -102), (-6, -97), (-1, -90), 
(2, -83), (5, -75), (6, -69), (6, -60), (11, -65), 
(7, -61), (5, -58), (3, -53), (5, -58), (2, -58), 
(-5, -60), (-9, -60), (-14, -62), (-20, -62), (-25, -60), 
(-34, -60), (-43, -59), (-52, -57), (-58, -55), (-62, -54), 
(-67, -49)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(0.89, 0.48, 0.43), width=2)
points = [
    (-62, -54), (-60, -55), (-57, -55), (-54, -57), (-49, -58), 
(-47, -60), (-50, -61), (-55, -63), (-59, -67), (-64, -72), 
(-65, -75), (-66, -69), (-65, -60)
    ]
plotPoly(points, True, pencolor=(0.67, 0.5, 0.22),
         fillcolor=(0.63, 0.24, 0.25), width=0)

# 隐藏海龟
turtle.hideturtle()

turtle.done()

运行效果

用代码画一只懒羊羊_第2张图片

备注

视频已发布到抖音和 b 站。抖音和b站名称:会代码的依古比古
抖音视频网址:用代码画一只懒羊羊
b站名称网址:用代码画一只懒羊羊
欢迎各位关注抖音和b站!!!可以及时获取最新消息!!!

你可能感兴趣的:(python)