python之海龟绘图

海龟绘图(turtle)是一个Python内置的绘图库,也被称为“Turtle Graphics”或简称“Turtles”。它采用了一种有趣的绘图方式,模拟一只小海龟在屏幕上爬行,而小海龟爬行的路径就形成了绘制的图形。这种绘图方式最初源自20世纪60年代的Logo编程语言,后来一些Python程序员构建了turtle库,使得其他程序员可以通过简单的import turtle命令,在Python中使用海龟作图。

今天就来简单举几个例子!

一,小猪佩奇

from turtle import *


def nose(x, y):  # 鼻子
    penup()  # 提起笔
    goto(x, y)  # 定位
    pendown()  # 落笔,开始画
    setheading(-30)  # 将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南)
    begin_fill()  # 准备开始填充图形
    a = 0.4
    for i in range(120):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            left(3)  # 向左转3度
            forward(a)  # 向前走a的步长
        else:
            a = a - 0.08
            left(3)
            forward(a)
    end_fill()  # 填充完成

    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    pencolor(255, 155, 192)  # 画笔颜色
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)  # 返回或设置pencolor和fillcolor
    end_fill()

    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255, 155, 192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160, 82, 45)
    end_fill()


def head(x, y):  # 头
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300, -30)
    circle(100, -60)
    circle(80, -100)
    circle(150, -20)
    circle(60, -95)
    setheading(161)
    circle(-300, 15)
    penup()
    goto(-100, 100)
    pendown()
    setheading(-30)
    a = 0.4
    for i in range(60):
        if 0 <= i < 30 or 60 <= i < 90:
            a = a + 0.08
            lt(3)  # 向左转3度
            fd(a)  # 向前走a的步长
        else:
            a = a - 0.08
            lt(3)
            fd(a)
    end_fill()


def cheek(x, y):  # 腮
    color((255, 155, 192))
    penup()
    goto(x, y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()


def mouth(x, y):  # 嘴
    color(239, 69, 19)
    penup()
    goto(x, y)
    pendown()
    setheading(-80)
    circle(30, 40)
    circle(40, 80)


def setting():  # 参数设置
    pensize(4)
    hideturtle()  # 使乌龟无形(隐藏)
    colormode(255)  # 将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内
    color((255, 155, 192), "pink")
    setup(840, 500)
    speed(100)


def ears(x, y):
    color((255, 155, 192), "pink")
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50, 50)
    circle(-10, 120)
    circle(-50, 56)
    end_fill()


def eyes(x, y):
    color((255, 155, 192), "pink")
    fillcolor('white')
    penup()
    goto(x, y)
    pendown()
    begin_fill()
    setheading(0)
    circle(20)
    end_fill()
    color((255, 155, 192))
    fillcolor('white')
    penup()
    goto(x, y)
    begin_fill()
    forward(80)
    pendown()
    setheading(100)
    circle(20)
    end_fill()
    color('black')
    penup()
    goto(x-7, y+13)
    pendown()
    begin_fill()
    setheading(0)
    circle(5)
    end_fill()
    penup()
    goto(x, y)
    begin_fill()
    forward(60)
    pendown()
    setheading(100)
    circle(5)
    end_fill()


def body(x, y):
    width(5)
    color('firebrick')
    list = ['orangered', 'firebrick']

    up()
    goto(x, y)
    down()
    setheading(-105)
    begin_fill()
    fillcolor(list[0])
    circle(250, 30)
    setheading(0)
    forward(138)
    setheading(75)
    circle(250, 30)
    end_fill()
    setheading(-45)
    forward(70)
    begin_fill()
    fillcolor(list[1])
    circle(5)
    end_fill()
    up()
    goto(x, y)
    down()
    setheading(-145)
    forward(70)
    begin_fill()
    circle(5)
    end_fill()
    up()
    goto(x+20, y-250/2-4)
    down()
    setheading(270)
    forward(50)
    left(90)
    width(10)
    color('black')
    forward(13)
    width(5)
    color('firebrick')
    up()
    goto(x + 120, y - 250 / 2-4)
    down()
    setheading(270)
    forward(50)
    left(90)
    width(10)
    color('black')
    forward(13)


def main():
    body(-25, -15)
    setting()  # 画布、画笔设置
    nose(-100, 100)  # 鼻子
    head(-69, 167)  # 头
    ears(0, 160)  # 耳朵
    eyes(-15, 100)  # 眼睛
    cheek(80, 10)  # 腮
    mouth(-20, 30)  # 嘴
    done()




if __name__ == '__main__':
    main()

运行结果:

二,哆啦A梦

import turtle as t

t2=t.Turtle()

def funa(x,y):
    t.pu()
    t.goto(x,y)
    t.dot(10,'red')
    t2.clear()
    t2.write('{},{}'.format(x,y),align='center',font=('黑体',20,'bold'))

def huxian(x,y,r,a):
    t.pu()
    t.goto(x,y)
    t.pd()
    t.circle(r,a)

def line2(x1,y1,x2,y2,c='black',size=1):
    t.color(c)
    t.pensize(size)
    t.pu()
    t.goto(x1,y1)
    t.pd()
    t.goto(x2,y2)


def sanjiao(x,y,m,c,fc='black'):
    t.color(fc,c)
    t.pu()
    t.goto(x,y)
    t.pd()
    t.seth(90)
    t.begin_fill()
    t.circle(m,180)
    t.end_fill()
        

def tuoyuan(x,y,st,s,c,b,ps=1,ang=0):
    t.pensize(ps)
    t.pu()
    t.goto(x,y)
    t.pd()
    t.seth(ang)

    a=st
    t.color(c,b)
    t.begin_fill()
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a+=0.01*s
            t.lt(3)
            t.fd(a)
        else:
            a-=0.01*s
            t.lt(3)
            t.fd(a)
    t.end_fill()

def head():
    tuoyuan(0,-44,7.0,0.0,'black','#00A0DE')
    tuoyuan(0,-49,6.0,0.03,'black','white')


def eyes():
    tuoyuan(-25,133,1.0,4.0,'black','white')
    tuoyuan(32,133,1.0,4.0,'black','white')
    tuoyuan(-15,161,0.0,2,'black','black',1)          
    tuoyuan(23,161,0.0,2,'black','black',1)
    tuoyuan(-15,161,0.0,1,'black','white',1)    
    tuoyuan(23,161,0.0,1,'black','white',1)    

def huzi():
    tuoyuan(-77,118,0.7,0,'#FFC5C8','#FFC5C8')
    tuoyuan(75,113,0.7,0,'#FFC5C8','#FFC5C8')    
    line2(-53,118,-136,149)
    line2(-50,95,-146,95)
    line2(-51,78,-139,52)
    line2(54,118,135,145)
    line2(55,95,150,95)
    line2(52,79,141,53)  


def nose():
    tuoyuan(4,100,1,0.1,'black','#DB3A49')
    #line2(4,101,4,43)
    t.color('black')
    t.pu()
    t.goto(4,101)
    t.seth(-80)
    t.pd()
    t.circle(-300,12)

    tuoyuan(15,117,0.0,1.0,'white','white',1)  

def setting():
    t.delay(0)  
    t.ht()
    #t.bgpic('./DLAM.gif')
    t.pensize(2)
    t.color('blue')
    t.bgcolor('white')

    t2.ht()
    t2.pu()
    t2.goto(0,-280)


def mouth():
    t.pu()
    t.goto(-73,59)
    t.seth(-24)
    t.pd()
    t.color('black','#E92A14')
    t.begin_fill()
    t.circle(200,45)
    t.seth(-98)   
    t.circle(-77,165)
    t.end_fill()

    t.pu()
    t.goto(-32,2)
    t.seth(24)
    t.pd()
    t.color('black','#E74810')
    t.begin_fill()
    t.circle(-150,35)
    t.seth(-130)   
    t.circle(-70,90)
    t.end_fill()


def xianglian():
    t.pu()
    t.goto(-66,-30)
    t.pd()
    t.color('black','#F53338')
    t.begin_fill()
    t.seth(-15)
    t.circle(260,26)
    t.seth(-80)
    t.fd(10)
    t.seth(-167)
    t.circle(-240,30)  
    t.seth(80) 
    t.fd(12)
    t.end_fill()

def lingdang():
    tuoyuan(-10,-80,1.0,0.1,'black','#FFD957',1)
    line2(-30,-60,10,-60,c='#E89F10',size=3)          
    line2(-10,-70,-10,-80,c='black') 

    t.pu()
    t.goto(-10,-70)
    t.pd()
    t.color('black')
    t.begin_fill()
    t.seth(0)
    t.circle(2.5)
    t.end_fill()  

    t.pu()
    t.goto(-3,-50)  
    t.pd()
    t.pensize(3)
    t.color('white')
    t.seth(-50)
    t.circle(-100,2)


def dupi():
    t.pu()
    t.goto(-50,-40)
    t.pd()
    t.color('black','white')
    t.begin_fill()
    t.seth(-140)
    t.circle(60,280)
    t.end_fill()
    
    t.pu()
    t.goto(-40,-90)
    t.pd()
    t.color('black','white')
    t.begin_fill()
    t.seth(0)
    t.fd(60)
    t.seth(-90)
    t.circle(-30,180)
    t.end_fill()  

def body():
    t.color('black','#00A0DE')
    t.pu()
    t.goto(-73,-40)
    t.pd()

    t.begin_fill()
    t.seth(-140)
    t.circle(360,15)
    t.seth(-40)  
    t.fd(40) 

    t.pu()
    t.goto(-89,-84)
    t.pd()
    t.seth(-90)
    t.circle(360,15)

    t.pu()
    t.goto(53,-176)
    t.pd()

    t.seth(80)
    t.circle(360,15)


    t.pu()
    t.goto(76,-134)
    t.pd()
    t.seth(40)  
    t.fd(40) 
    t.seth(120)
    t.circle(330,15)
    t.end_fill()


    sanjiao(2,-178,10,'white')

def feet():
    t.pu()
    t.goto(-113,-135)
    t.pd()
    t.color('black','white')
    t.begin_fill()
    t.seth(0)
    t.circle(23)
    t.end_fill()    

    t.pu()
    t.goto(85,-135)
    t.pd()
    t.color('black','white')
    t.begin_fill()
    t.seth(0)
    t.circle(23)
    t.end_fill()

    
    tuoyuan(-13,-192,1.0,5.0,'black','white',ang=90)
    tuoyuan(63,-192,1.0,5.0,'black','white',ang=90)

def cat_name():
    t.pu()
    t.goto(80,-50)
    t.color('#0180DD')
    t.write('ドラえもん',font=('宋体',24,'normal'))   
    t.goto(120,-22)
    t.color('#dddddd')
    t.write('Doraemon',font=('宋体',20,'italic'))


setting()

head()

eyes()
nose()
mouth()
huzi()
body()
dupi()
xianglian()
feet()
lingdang()

cat_name()

t.done()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值