小黄人

文章目录

  • 系列目录
  • 写在前面
  • 完整代码
  • 代码分析
  • 1. 初始化部分
  • 2. 身体部分
  • 3. 右眼睛部分
  • 4. 左眼睛部分
  • 5. 嘴巴部分
  • 6. 裤子部分
  • 7. 脚部部分
  • 8. 手部部分
  • 9. 头顶装饰部分
  • 10. 代码总结
  • 写在后面
  • 系列目录

    序号 直达链接
    爱心系列
    1 Python制作一个无法拒绝的表白界面
    2 Python满屏飘字表白代码
    3 Python无限弹窗满屏表白代码
    4 Python李峋同款可写字版跳动的爱心
    5 Python流星雨代码
    6 Python漂浮爱心代码
    7 Python爱心光波代码
    8 Python普通的玫瑰花代码
    9 Python炫酷的玫瑰花代码
    10 Python多彩的玫瑰花代码
    节日系列
    1 Python动漫风烟花秀代码
    2 Python新年烟花秀代码
    3 Python圣诞礼物代码
    4 Python画圣诞树代码
    5 Python可爱版圣诞树丨绿色
    6 Python可爱版圣诞树丨粉色
    7 Python大雪纷飞代码
    8 Python生日蛋糕代码
    9 Python五彩气球代码
    10 Python国庆祝福代码
    11 Python万圣礼物代码
    12 Python愚人节礼物代码
    13 Python浪漫星空代码
    14 Python樱花树代码
    动漫系列
    1 Python名侦探柯南
    2 Python喜羊羊
    3 Python懒羊羊
    4 Python沸羊羊
    5 Python小灰灰
    6 Python小香香
    7 Python灰太狼
    8 Python海绵宝宝
    9 Python哆啦A梦
    10 Python凯蒂猫
    11 Python猫和老鼠
    12 Python草莓熊
    13 Python迷你皮卡丘
    14 Python高级皮卡丘
    15 Python豪华皮卡丘
    16 Python史迪仔
    17 Python小熊猫
    18 Python蜘蛛侠
    19 Python可爱版蜡笔小新
    20 Python萌萌的蜡笔小新
    21 Python罗小黑
    22 Python猪猪侠
    炫酷系列
    1   Python张万森下雪了
    2 Python一闪一闪亮晶晶
    3 Python黑客帝国代码雨
    4 Python七彩花朵
    5 Python模拟3D星空
    6 Python金榜题名
    7 Python满天星

    写在前面

    Python绘制小黄人的完整代码。

    完整代码

    import turtle
    turtle.setup(1.0, 1.0)
    turtle.title('小黄人')
    t = turtle.Turtle()
    t.screen.delay(0)
    turtle.colormode(255)
    t.hideturtle()
    t.speed(10)
    t.penup()
    t.pensize(4)
    t.goto(100, 0)
    t.pendown()
    t.left(90)
    t.color((0, 0, 0), (255, 255, 0))
    # 身体绘制上色
    t.begin_fill()
    t.forward(200)
    t.circle(100, 180)
    t.forward(200)
    t.circle(100, 180)
    t.end_fill()
    # 右眼睛绘制上色
    t.pensize(12)
    t.penup()
    t.goto(-100, 200)
    t.pendown()
    t.right(100)
    t.circle(500, 23)
    
    t.pensize(3)
    t.penup()
    t.goto(0, 200)
    t.pendown()
    t.seth(270)
    t.color("black", "white")
    t.begin_fill()
    t.circle(30)
    t.end_fill()
    
    t.penup()
    t.goto(15, 200)
    t.pendown()
    t.color("black", "black")
    t.begin_fill()
    t.circle(15)
    t.end_fill()
    
    t.penup()
    t.goto(35, 205)
    t.color("black", "white")
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    # 左眼睛绘制上色
    t.pensize(3)
    t.penup()
    t.goto(0, 200)
    t.pendown()
    t.seth(90)
    t.color("black", "white")
    t.begin_fill()
    t.circle(30)
    t.end_fill()
    
    t.penup()
    t.goto(-15, 200)
    t.pendown()
    t.color("black", "black")
    t.begin_fill()
    t.circle(15)
    t.end_fill()
    
    t.penup()
    t.goto(-35, 205)
    t.color("black", "white")
    t.begin_fill()
    t.circle(5)
    t.end_fill()
    
    # 嘴绘制上色
    t.penup()
    t.goto(-20, 100)
    t.pendown()
    t.seth(270)
    t.color("black", "white")
    t.begin_fill()
    t.circle(20, 180)
    t.left(90)
    t.forward(40)
    t.end_fill()
    
    # 裤子绘制上色
    t.penup()
    t.goto(-100, 0)
    t.pendown()
    t.seth(0)
    t.color("black", "blue")
    t.begin_fill()
    t.forward(20)
    t.left(90)
    t.forward(40)
    t.right(90)
    t.forward(160)
    t.right(90)
    t.forward(40)
    t.left(90)
    t.forward(20)
    t.seth(270)
    t.penup()
    t.goto(-100, 0)
    t.circle(100, 180)
    t.end_fill()
    
    # 左裤子腰带
    t.penup()
    t.goto(-70, 20)
    t.pendown()
    t.color("black", "blue")
    t.begin_fill()
    t.seth(45)
    t.forward(15)
    t.left(90)
    t.forward(60)
    t.seth(270)
    t.forward(15)
    t.left(40)
    t.forward(50)
    t.end_fill()
    t.left(180)
    t.goto(-70, 30)
    t.dot()
    
    # 右裤腰带
    t.penup()
    t.goto(70, 20)
    t.pendown()
    t.color("black", "blue")
    t.begin_fill()
    t.seth(135)
    t.forward(15)
    t.right(90)
    t.forward(60)
    t.seth(270)
    t.forward(15)
    t.right(40)
    t.forward(50)
    t.end_fill()
    
    t.left(180)
    t.goto(70, 30)
    
    t.dot()
    
    # 脚
    t.penup()
    t.goto(4, -100)
    t.pendown()
    t.seth(270)
    t.color("black", "black")
    t.begin_fill()
    t.forward(30)
    t.left(90)
    t.forward(40)
    t.seth(20)
    t.circle(10, 180)
    t.circle(400, 2)
    t.seth(90)
    t.forward(20)
    t.goto(4, -100)
    t.end_fill()
    
    t.penup()
    t.goto(-4, -100)
    t.pendown()
    t.seth(270)
    t.color("black", "black")
    t.begin_fill()
    t.forward(30)
    t.right(90)
    t.forward(40)
    t.seth(20)
    t.circle(10, -225)
    t.circle(400, -3)
    t.seth(90)
    t.forward(21)
    t.goto(-4, -100)
    t.end_fill()
    
    # 左手
    t.penup()
    t.goto(-100, 50)
    t.pendown()
    t.seth(225)
    t.color("black", "yellow")
    t.begin_fill()
    t.forward(40)
    t.left(90)
    t.forward(35)
    t.seth(90)
    t.forward(50)
    t.end_fill()
    # 右手
    t.penup()
    t.goto(100, 50)
    t.pendown()
    t.seth(315)
    t.color("black", "yellow")
    t.begin_fill()
    t.forward(40)
    t.right(90)
    t.forward(36)
    t.seth(90)
    t.forward(50)
    t.end_fill()
    
    #
    t.penup()
    t.goto(0, -100)
    t.pendown()
    t.forward(30)
    
    #
    t.penup()
    t.goto(0, -20)
    t.pendown()
    t.color("yellow")
    t.begin_fill()
    t.seth(45)
    t.forward(20)
    t.circle(10, 180)
    t.right(90)
    t.circle(10, 180)
    t.forward(20)
    t.end_fill()
    
    #
    t.penup()
    t.color("black")
    t.goto(-100, -20)
    t.pendown()
    t.circle(30, 90)
    
    t.penup()
    t.goto(100, -20)
    t.pendown()
    t.circle(30, -90)
    # 头顶
    t.penup()
    t.goto(2, 300)
    t.pendown()
    t.begin_fill()
    t.seth(135)
    t.circle(100, 40)
    t.end_fill()
    
    t.penup()
    t.goto(2, 300)
    t.pendown()
    t.begin_fill()
    t.seth(45)
    t.circle(100, 40)
    t.end_fill()
    turtle.done()
    

    代码分析

    这段代码使用了Python中的turtle模块绘制了一个可爱的“小黄人”形象,包括其身体、眼睛、嘴巴、裤子、手脚以及头顶装饰等部分。以下是详细的代码分析:


    1. 初始化部分

    import turtle
    turtle.setup(1.0, 1.0)
    turtle.title('小黄人')
    t = turtle.Turtle()
    t.screen.delay(0)
    turtle.colormode(255)
    t.hideturtle()
    t.speed(10)
    
  • 导入turtle模块,用于绘图。
  • 设置画布大小为全屏setup(1.0, 1.0)
  • 设置窗口标题为“小黄人”。
  • 创建画笔t,并设置绘图的颜色模式为RGB 255模式。
  • 隐藏画笔箭头hideturtle()以避免影响视觉效果,并将绘图速度设置为最快模式。

  • 2. 身体部分

    t.penup()
    t.pensize(4)
    t.goto(100, 0)
    t.pendown()
    t.left(90)
    t.color((0, 0, 0), (255, 255, 0))
    t.begin_fill()
    t.forward(200)
    t.circle(100, 180)
    t.forward(200)
    t.circle(100, 180)
    t.end_fill()
    
  • 设置画笔粗细为4,调整位置到(100, 0)并开始绘制。
  • 使用黄色填充(255, 255, 0),通过两段直线和两个半圆形成“小黄人”的椭圆形身体。

  • 3. 右眼睛部分

    t.pensize(12)
    t.penup()
    t.goto(-100, 200)
    t.pendown()
    t.right(100)
    t.circle(500, 23)
    ...
    
  • 画眼镜框,设置线条粗细为12,利用圆弧circle绘制黑色镜框。
  • 右眼由三层组成:外层白色圆(眼球)、中间黑色圆(瞳孔)和内层小白点(高光),分别使用填充绘制,表现眼睛的立体感。

  • 4. 左眼睛部分

    t.penup()
    t.goto(0, 200)
    t.pendown()
    t.seth(90)
    t.color("black", "white")
    ...
    
  • 左眼的绘制与右眼类似,位置对称,绘制步骤完全相同,包括镜框、眼球、瞳孔和高光。

  • 5. 嘴巴部分

    t.penup()
    t.goto(-20, 100)
    t.pendown()
    t.seth(270)
    t.color("black", "white")
    t.begin_fill()
    t.circle(20, 180)
    t.left(90)
    t.forward(40)
    t.end_fill()
    
  • 使用半圆circle绘制弯曲的嘴巴,并填充白色,增加“小黄人”微笑的效果。

  • 6. 裤子部分

    t.penup()
    t.goto(-100, 0)
    t.pendown()
    t.seth(0)
    t.color("black", "blue")
    t.begin_fill()
    ...
    
  • 裤子主要由矩形、曲线和腰带组成。
  • 利用多段线和填充蓝色绘制裤子,同时添加左右腰带,通过计算斜角和长度表现出裤子的层次感。

  • 7. 脚部部分

    t.penup()
    t.goto(4, -100)
    t.pendown()
    t.seth(270)
    t.color("black", "black")
    t.begin_fill()
    ...
    
  • 左右脚分别绘制,形状为矩形加上圆弧的组合。
  • 使用黑色填充,模仿鞋子的形状,位置与身体对称。

  • 8. 手部部分

    t.penup()
    t.goto(-100, 50)
    t.pendown()
    t.seth(225)
    t.color("black", "yellow")
    t.begin_fill()
    t.forward(40)
    t.left(90)
    ...
    
  • 左右手臂的绘制类似,分别从身体两侧延伸。
  • 使用黄色填充,线条粗细和角度设置得当,体现“小黄人”的简洁风格。

  • 9. 头顶装饰部分

    t.penup()
    t.goto(2, 300)
    t.pendown()
    t.begin_fill()
    t.seth(135)
    t.circle(100, 40)
    t.end_fill()
    ...
    
  • 在头顶添加两组对称的装饰弧线,利用填充使其更为突出,增加角色的趣味性。

  • 10. 代码总结

    这段代码巧妙地利用Turtle库的图形绘制功能,通过简单的几何图形组合、颜色填充和对称设计,构建了“小黄人”的可爱形象。代码结构清晰,采用模块化的方式分部分绘制身体、眼睛、嘴巴、裤子等部位,既体现了代码的逻辑性,又便于后续修改或扩展。

    适合初学者理解Turtle绘图的基本概念,包括坐标系操作、颜色填充、线条粗细、角度控制等内容。

    写在后面

    我是一只有趣的兔子,感谢你的喜欢。

    作者:Want595

    物联沃分享整理
    物联沃-IOTWORD物联网 » Python小黄人

    发表回复