用Python代码画一只汤姆

简介

用python代码画一只汤姆,仅使用turtle库。如下:
在这里插入图片描述
绘画过程可以在下列平台查看:

抖音:用代码画一只汤姆
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(452, 680, 100, 100)

# 绘制

points = [
    (8, 275), (6, 268), (4, 261), (1, 253), (1, 247), 
    (-7, 245), (-14, 243), (-22, 240), (-30, 236), (-34, 233), 
    (-39, 229), (-43, 223), (-46, 217), (-48, 212), (-50, 205), 
    (-51, 199), (-52, 195), (-64, 195), (-68, 194), (-73, 190), 
    (-74, 187), (-74, 177), (-72, 172), (-69, 167), (-68, 163), 
    (-65, 159), (-62, 155), (-58, 149), (-55, 144), (-53, 140), 
    (-50, 132), (-47, 129), (-37, 129), (-32, 125), (-27, 121), 
    (-26, 119), (-26, 115), (-30, 112), (-34, 108), (-37, 102), 
    (-40, 95), (-41, 81), (-44, 76), (-47, 70), (-49, 63), 
    (-52, 56), (-53, 48), (-54, 42), (-57, 35), (-58, 27), 
    (-59, 23), (-80, 24), (-88, 20), (-93, 14), (-96, 2), 
    (-98, -8), (-100, -25), (-101, -35), (-103, -41), (-104, -50), 
    (-103, -59), (-97, -64), (-91, -65), (-84, -72), (-72, -71), 
    (-64, -67), (-59, -66), (-59, -66), (-58, -76), (-57, -87), 
    (-79, -108), (-88, -122), (-91, -130), (-91, -130), (-90, -141), 
    (-90, -141), (-87, -147), (-82, -153), (-76, -159), (-93, -161), 
    (-103, -163), (-117, -158), (-124, -160), (-133, -166), (-144, -177), 
    (-143, -191), (-141, -197), (-136, -203), (-128, -204), (-123, -205), 
    (-123, -205), (-115, -209), (-112, -211), (-102, -212), (-95, -208), 
    (-73, -199), (-63, -196), (-38, -194), (-31, -196), (-22, -197), 
    (-15, -192), (-16, -185), (-17, -181), (-27, -170), (-39, -149), 
    (-37, -147), (-26, -144), (-4, -143), (37, -158), (19, -177), 
    (14, -179), (7, -188), (4, -193), (6, -200), (14, -204), 
    (36, -212), (67, -227), (75, -234), (78, -241), (84, -245), 
    (93, -248), (104, -246), (113, -242), (123, -237), (133, -229), 
    (135, -219), (134, -204), (127, -199), (113, -197), (109, -198), 
    (99, -192), (88, -189), (75, -185), (62, -185), (63, -182), 
    (89, -164), (90, -160), (90, -150), (86, -139), (72, -115), 
    (68, -107), (68, -98), (76, -91), (91, -62), (96, -54), 
    (104, -26), (112, -10), (120, 1), (129, 10), (140, 15), 
    (153, 20), (136, 23), (120, 22), (108, 17), (91, 4), 
    (80, -10), (71, -23), (67, -33), (55, -47), (45, -36), 
    (38, -19), (56, -18), (63, -15), (70, -9), (73, -3), 
    (71, 26), (64, 52), (57, 71), (49, 86), (39, 102), 
    (28, 116), (23, 119), (19, 120), (21, 125), (27, 130), 
    (42, 131), (51, 130), (45, 136), (41, 138), (48, 140), 
    (55, 137), (62, 132), (65, 130), (66, 139), (64, 150), 
    (59, 169), (65, 169), (74, 175), (83, 184), (90, 194), 
    (97, 209), (102, 217), (109, 225), (115, 229), (110, 233), 
    (102, 236), (86, 240), (63, 240), (46, 235), (39, 241), 
    (35, 248), (32, 254), (31, 248), (32, 243), (29, 249), 
    (25, 258), (21, 263), (16, 268), (13, 271), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.33, 0.39, 0.45), width=2)

points = [
    (-67, 181), (-67, 181), (-67, 176), (-67, 176), (-68, 167), 
    (-68, 167), (-63, 156), (-63, 156), (-54, 144), (-54, 144), 
    (-52, 134), (-46, 129), (-37, 130), (-34, 128), (-27, 127), 
    (-20, 129), (-11, 134), (-3, 140), (3, 147), (8, 154), 
    (-4, 155), (-11, 157), (-16, 160), (-20, 165), (-22, 171), 
    (-20, 177), (-16, 183), (-9, 187), (-5, 199), (-1, 208), 
    (-1, 220), (-7, 225), (-13, 223), (-17, 219), (-19, 221), 
    (-21, 222), (-23, 228), (-27, 228), (-30, 225), (-35, 223), 
    (-35, 228), (-38, 228), (-41, 225), (-45, 221), (-48, 214), 
    (-50, 207), (-52, 196), (-51, 192), (-51, 186), (-58, 186), 
    (-64, 184), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.85, 0.8, 0.82), width=2)

turtle.up()
turtle.goto((-46, 140))
turtle.down()
turtle.pencolor((0.32, 0.24, 0.26))
turtle.dot(9)

points = [
    (-51, 167), (-47, 170), (-44, 174), (-39, 176), (-42, 180), 
    (-46, 182), (-49, 182), (-54, 181), (-57, 179), (-58, 176), 
    (-56, 171), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.08, 0.09, 0.09), width=2)

turtle.up()
turtle.goto((-51, 178))
turtle.down()
turtle.pencolor((1, 1, 1))
turtle.dot(5)

points = [
    (-21, 214), (-17, 220), (-11, 223), (-7, 224), (-3, 222), 
    (-1, 218), (-2, 208), (-4, 200), (-6, 194), (-8, 188), 
    (-11, 185), (-16, 182), (-19, 179), (-22, 175), (-25, 177), 
    (-29, 179), (-32, 180), (-31, 184), (-30, 190), (-29, 198), 
    (-27, 204), (-23, 210), (-20, 216), (-15, 221), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.93, 0.84, 0.3), width=2)

# 右眼
points = [
    (-40, 226), (-44, 220), (-47, 215), (-49, 210), (-51, 203), 
    (-52, 195), (-51, 189), (-51, 186), (-45, 185), (-43, 191), 
    (-41, 196), (-40, 199), (-38, 207), (-36, 213), (-35, 219), 
    (-35, 226), (-38, 228), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.91, 0.86, 0.29), width=2)

points = [
    (-49, 210), (-46, 211), (-43, 211), (-43, 208), (-43, 205), 
    (-44, 201), (-45, 198), (-46, 195), (-48, 193), (-51, 194), 
    (-51, 197), (-51, 200), (-50, 204), (-49, 207), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.05, 0.09, 0.06), width=2)

# 左眼
points = [
    (-24, 207), (-20, 207), (-18, 205), (-17, 198), (-19, 193), 
    (-20, 188), (-22, 185), (-23, 183), (-26, 183), (-29, 186), 
    (-29, 191), (-29, 198), (-27, 202), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.09, 0.09, 0.05), width=2)

# 双眼之间
points = [
    (-34, 223), (-30, 223), (-28, 227), (-24, 228), (-22, 227), 
    (-22, 224), (-22, 222), (-19, 222), (-16, 221), (-19, 218), 
    (-22, 213), (-24, 210), (-26, 205), (-28, 202), (-29, 199), 
    (-30, 194), (-31, 190), (-31, 185), (-33, 182), (-36, 185), 
    (-41, 185), (-44, 184), (-43, 190), (-42, 194), (-39, 200), 
    (-38, 205), (-36, 212), (-35, 218), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.63, 0.62, 0.58), width=2)



# 胡子
points = [
    (-62, 180), (-73, 187), (-81, 190), (-91, 193), (-102, 195), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-63, 176), (-78, 175), (-86, 174), (-94, 173), (-100, 172), 
    (-105, 171), (-110, 170), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-30, 173), (-17, 177), (-4, 179), (6, 181), (20, 180), 
    (31, 181), (38, 180), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-30, 165), (-15, 165), (-4, 161), (7, 158), (16, 155), 
    (26, 150), (34, 146), (42, 143), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

# 眉毛
points = [
    (-33, 234), (-30, 237), (-27, 238), (-26, 237), (-25, 236), 
    (-24, 235), (-26, 235), (-28, 235), (-30, 235), (-31, 235), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.25, 0.25, 0.27), width=2)

points = [
    (-11, 231), (-9, 234), (-5, 236), (1, 236), (4, 235), 
    (7, 233), (9, 231), (10, 229), (11, 225), (11, 223), 
    (11, 219), (10, 216), (8, 220), (7, 223), (5, 227), 
    (4, 229), (2, 231), (-2, 231), (-6, 231), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.1, 0.13, 0.14), width=2)

points = [
    (9, 245), (-1, 251), (-9, 254), (-16, 255), (-26, 256), 
    (-16, 256), (-7, 255), (0, 253), (5, 251), (17, 247), 
    (13, 252), (7, 258), (2, 262), (-5, 265), (-16, 269), 
    (-7, 267), (0, 264), (5, 263), (11, 261), (17, 257), 
    (22, 254), (27, 250), (29, 246), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (114, 229), (108, 233), (99, 234), (89, 235), (79, 234), 
    (71, 232), (62, 228), (56, 223), (51, 217), (49, 212), 
    (50, 204), (53, 199), (60, 197), (65, 199), (69, 202), 
    (69, 199), (65, 197), (68, 193), (65, 192), (62, 193), 
    (59, 192), (55, 190), (56, 186), (56, 182), (56, 177), 
    (58, 173), (61, 171), (65, 170), (71, 173), (77, 178), 
    (84, 186), (88, 193), (92, 203), (97, 211), (102, 216), 
    (107, 224), (110, 227), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.72, 0.45, 0.54), width=2)

points = [
    (-31, 66), (-31, 74), (-36, 79), (-42, 79), (-45, 72), 
    (-48, 65), (-50, 57), (-52, 51), (-56, 42), (-58, 30), 
    (-60, 18), (-61, 6), (-62, -9), (-62, -17), (-62, -28), 
    (-62, -39), (-61, -58), (-60, -71), (-57, -87), (-51, -104), 
    (-45, -117), (-38, -125), (-28, -133), (-12, -137), (3, -134), 
    (17, -131), (26, -124), (32, -101), (22, -76), (14, -57), 
    (5, -42), (0, -27), (-3, -8), (-3, 12), (-2, 27), 
    (-1, 36), (1, 49), (-2, 64), (-15, 76), (-20, 76), 
    (-26, 73), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.62, 0.59, 0.62), width=2)

points = [
    (-68, 24), (-78, 25), (-85, 21), (-89, 18), (-93, 13), 
    (-95, 6), (-96, 0), (-98, -13), (-101, -31), (-101, -36), 
    (-103, -38), (-103, -46), (-103, -52), (-102, -59), (-98, -64), 
    (-92, -65), (-85, -71), (-79, -72), (-71, -72), (-66, -68), 
    (-60, -66), (-61, -52), (-62, -39), (-62, -29), (-63, -18), 
    (-63, -8), (-61, 5), (-60, 15), (-59, 24), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.85, 0.8, 0.82), width=2)

points = [
    (-12, 33), (-21, 33), (-32, 34), (-39, 29), (-44, 23), 
    (-47, 13), (-49, 5), (-50, -5), (-50, -14), (-50, -23), 
    (-52, -27), (-55, -36), (-58, -45), (-57, -55), (-49, -62), 
    (-40, -62), (-35, -68), (-28, -71), (-21, -69), (-16, -66), 
    (-15, -63), (-5, -63), (1, -61), (4, -58), (7, -52), 
    (6, -38), (2, -31), (-7, -29), (-6, -20), (-6, -12), 
    (-5, -4), (-6, 7), (-8, 11), (-4, 16), (-8, 20), 
    (-12, 22), (-9, 26), (-10, 31), (-12, 32), (-18, 33), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.85, 0.8, 0.82), width=2)

points = [
    (-66, 24), (-59, 24), (-60, 20), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.55, 0.55, 0.56), width=2)

points = [
    (-66, 13), (-63, 16), (-61, 16), (-61, 10), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.4, 0.39, 0.4), width=2)

points = [
    (-11, 33), (-8, 27), (-13, 24), (-5, 19), (-4, 13), 
    (-8, 12), (-5, 3), (-2, -4), (0, 2), (1, 10), 
    (1, 18), (3, 27), 
    ]
plotPoly(points, True, pencolor=(0.34, 0.38, 0.45),
         fillcolor=(0.34, 0.38, 0.45), width=2)

points = [
    (45, 30), (35, 32), (26, 32), (16, 32), (6, 32), 
    (-11, 33), (-9, 30), (-9, 26), (-13, 24), (-9, 21), 
    (-5, 19), (-3, 16), (-5, 14), (-9, 12), (-6, 8), 
    (-5, 2), (-2, -5), (3, -7), (10, -10), (17, -12), 
    (26, -15), (32, -17), (39, -18), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (14, 54), (17, 46), (20, 40), (22, 36), (23, 32), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-98, -41), (-100, -36), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-93, -52), (-92, -58), (-91, -64), (-89, -66), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-77, -51), (-76, -57), (-73, -62), (-68, -65), (-65, -67), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-45, -47), (-44, -53), (-43, -58), (-41, -63), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-28, -48), (-25, -55), (-20, -59), (-15, -62), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-143, -179), (-140, -174), (-136, -169), (-130, -165), (-126, -162), 
    (-121, -159), (-113, -159), (-106, -160), (-103, -164), (-98, -163), 
    (-93, -162), (-85, -161), (-75, -161), (-71, -161), (-68, -165), 
    (-64, -169), (-59, -172), (-55, -177), (-50, -183), (-50, -179), 
    (-49, -177), (-44, -179), (-45, -175), (-46, -172), (-47, -168), 
    (-44, -167), (-40, -168), (-33, -173), (-35, -168), (-34, -166), 
    (-31, -166), (-28, -169), (-25, -172), (-23, -175), (-19, -180), 
    (-15, -185), (-15, -191), (-20, -196), (-23, -197), (-29, -197), 
    (-32, -196), (-35, -194), (-40, -194), (-59, -194), (-64, -195), 
    (-71, -198), (-77, -199), (-84, -202), (-90, -204), (-95, -208), 
    (-103, -210), (-112, -210), (-118, -208), (-123, -205), (-126, -204), 
    (-135, -203), (-140, -198), (-142, -193), (-143, -186), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.85, 0.8, 0.82), width=2)

points = [
    (20, -177), (25, -176), (29, -176), (25, -182), (31, -178), 
    (38, -178), (40, -180), (37, -184), (34, -187), (32, -189), 
    (39, -187), (41, -189), (39, -192), (38, -196), (44, -194), 
    (48, -191), (53, -190), (59, -187), (70, -186), (81, -186), 
    (90, -189), (98, -192), (105, -194), (108, -197), (118, -195), 
    (125, -198), (129, -200), (133, -204), (135, -209), (135, -217), 
    (133, -227), (130, -231), (125, -235), (120, -238), (115, -240), 
    (108, -245), (101, -247), (96, -247), (86, -247), (80, -243), 
    (79, -241), (73, -234), (70, -230), (63, -227), (56, -222), 
    (43, -217), (28, -211), (18, -207), (10, -203), (5, -196), 
    (7, -188), (12, -183), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.84, 0.8, 0.82), width=2)



points = [
    (-135, -167), (-130, -166), (-125, -166), (-117, -166), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (-124, -204), (-127, -199), (-128, -194), (-126, -189), (-124, -184), 
    (-118, -181), (-110, -178), (-98, -178), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (101, -216), (108, -218), (111, -221), (114, -225), (116, -229), 
    (115, -234), (113, -238), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (124, -206), (130, -209), (133, -214), (134, -220), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (80, -10), (83, -13), (87, -13), (91, -9), (94, -7), 
    (92, -12), (91, -17), (91, -20), (93, -23), (96, -21), 
    (99, -17), (100, -20), (100, -23), (100, -25), (103, -23), 
    (106, -21), (106, -23), (108, -19), (110, -14), (114, -8), 
    (117, -3), (122, 3), (130, 10), (137, 15), (142, 17), 
    (148, 18), (154, 19), (152, 22), (145, 22), (136, 23), 
    (127, 23), (122, 22), (116, 19), (108, 16), (103, 13), 
    (97, 8), (92, 4), (87, 0), (83, -5), 
    ]
plotPoly(points, True, pencolor=(0.2, 0.2, 0.2),
         fillcolor=(0.85, 0.8, 0.82), width=2)

points = [
    (-24, 115), (-24, 114), (-24, 111), (-24, 109), (-25, 106), 
    (-25, 104), (-26, 101), (-26, 99), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

points = [
    (17, 121), (13, 120), (10, 118), (6, 115), (3, 111), 
    (2, 108), (1, 104), (1, 99), 
    ]
plotLine(points, pencolor=(0.2, 0.2, 0.2), width=2)

turtle.up()
turtle.goto((-47, 206))
turtle.down()
turtle.pencolor((1, 1, 1))
turtle.dot(5)

turtle.up()
turtle.goto((-24, 201))
turtle.down()
turtle.pencolor((1, 1, 1))
turtle.dot(5)


# 隐藏海龟
turtle.hideturtle()
turtle.done()

运行效果

在这里插入图片描述

备注

视频已发布到抖音和 b 站。抖音和b站名称:会代码的依古比古

抖音视频网址:用代码画一只汤姆

b站视频网址:用代码画一只汤姆_哔哩哔哩_bilibili

欢迎各位关注抖音和b站!!!可以及时获取最新消息!!!

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值