Python源码15:使用海龟画图turtle画小黄人

turtle模块是一个Python的标准库之一,它提供了一个基于Turtle graphics的绘图库。Turtle graphics是一种流行的绘图方式,它通过控制一个小海龟在屏幕上移动来绘制图形。

turtle模块可以让您轻松地创建和控制海龟图形,从而帮助您学习Python编程和图形编程的基本概念。您可以使用turtle模块绘制各种形状、线条和图案,还可以通过添加颜色和其他效果来增强绘图的视觉效果。

#我的Python教程
#微信公众号:wdPython

turtle模块还提供了一些简单的函数和命令,例如前进、后退、转向、设置画笔颜色和粗细等等。这些函数和命令使得您可以轻松地控制海龟的移动和绘图行为。此外,turtle模块还提供了一些高级功能,例如创建自己的绘图函数、保存和加载绘图文件等等。

总之,turtle模块是一个非常适合初学者使用的Python库,它可以帮助您学习Python编程和图形编程的基本概念,并为您提供了一个轻松愉快的绘图环境。


import turtle as t

class DrawYellowpeople(object):

    #画布初始化
    def __init__(self):
        wn = t.Screen()
        t.colormode(255)
        t.hideturtle()
        t.speed(15)#控制画笔速度

    #定位
    def Dw(self,x,y):
        t.penup()
        t.goto(x,y)
        t.pendown()

    #画身体
    def mkbody(self):
        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()

    #画眼睛框
    def mkclass(self):
        t.pensize(12)

        t.penup()
        t.goto(-100, 200)
        t.pendown()

        t.right(100)
        t.circle(500, 23)
        t.pensize(3)

    #画眼睛
    def mkeye(self,x,y,z):
        t.seth(x)
        t.color("black", "white")
        t.begin_fill()
        t.circle(30)
        t.end_fill()
        t.penup()
        t.goto(y, 200)
        t.pendown()
        t.color("black", "black")
        t.begin_fill()
        t.circle(15)
        t.end_fill()
        #点光
        t.penup()
        t.goto(z, 205)
        t.color("black", "white")
        t.begin_fill()
        t.circle(5)
        t.end_fill()

    #画嘴巴
    def mkmouth(self):

        t.seth(270)
        t.color("black", "white")
        t.begin_fill()
        t.circle(20, 180)
        t.left(90)
        t.forward(40)
        t.end_fill()

    #画裤子
    def mktrouse(self):
        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()


    #裤腰
    def mktrouseby(self,x,y,z,e):

        t.color("black", "blue")
        t.begin_fill()
        t.seth(x)
        t.forward(15)
        t.left(y)
        t.forward(60)
        t.seth(270)
        t.forward(15)
        t.left(z)
        t.forward(50)
        t.end_fill()
        t.left(180)
        t.goto(e, 30)
        t.dot()

    #脚
    def mkfoot(self,x,y,z,w,l):


        t.seth(270)
        t.color("black", "black")
        t.begin_fill()
        t.forward(30)
        t.left(x)
        t.forward(40)
        t.seth(20)
        t.circle(10, y)
        t.circle(400, w)
        t.seth(90)
        t.forward(l)
        t.goto(z, -100)
        t.end_fill()



    #画手
    def mkhands(self,x,y,z):

        t.seth(x)
        t.color("black", "yellow")
        t.begin_fill()
        t.forward(40)
        t.left(y)
        t.forward(z)
        t.seth(90)
        t.forward(50)
        t.end_fill()

    #画心图案
    def mkheart(self):
        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()

    #画口袋
    def mkbag(self):
        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)

    #画头发
    def mkhair(self,x):

        t.penup()
        t.goto(2, 300)
        t.pendown()
        t.begin_fill()
        t.seth(x)
        t.circle(100, 40)
        t.end_fill()


if __name__ == '__main__':

    E=DrawYellowpeople()
    E.mkbody()#身体
    E.mkclass()#眼睛框
    E.Dw(0,200)
    E.mkeye(270,15,35)#右眼
    E.Dw(0,200)
    E.mkeye(90,-15,-35)#左眼
    E.Dw(-20,100)
    E.mkmouth()
    E.Dw(-100,0)
    E.mktrouse()#裤子
    E.Dw(-70,20)
    E.mktrouseby(45,90,40,-70)#左裤带
    E.Dw(70,20)
    E.mktrouseby(135,-90,-40,70)#右裤带
    E.Dw(4,-100)
    E.Dw(4,-100)
    E.mkfoot(90,180,4,2,20)#右脚
    E.Dw(-4,-100)
    E.mkfoot(-90,-225,-4,-3,21)#左脚
    E.Dw(-100,50)
    E.mkhands(225,90,35)#右手
    E.Dw(100, 50)
    E.mkhands(315, -90, 36)#左手
    E.Dw(0,-100)
    t.forward(30)
    E.Dw(0,-20)
    E.mkheart()#心图案
    E.mkbag()#口袋
    E.mkhair(135)#左头发
    E.mkhair(45)#右头发
    t.done()

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/gxz888/article/details/134828384