python海龟画图 turtle的简单使用 海龟画图四个例子(附源码)

liftword1周前 (04-09)技术文章8

python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics),出现在1966年的Logo计算机语言。海龟绘图(turtle库)是python的内部模块,使用前导入即可 import turtle

海龟有3个关键属性:方向、位置和画笔(笔的属性有色彩、宽度和开/关状态)

一、画布(canvas)

画布就是turtle为我们展开用于绘图区域, 我们可以设置它的大小和初始位置
设置画布的大小:

turtle.screensize(canvwidth=None, canvheight=None, bg=None)

canvwidth:画布的宽(单位像素,默认值400)

canvheight:画布的高(单位像素,默认值300)

bg:背景颜色

使用:

t.screensize(800,600,'blue')


turtle.setup(width=0.5, height=0.75, startx=None, starty=None)

width, height: 输入宽和高为整数时, 表示像素; 为小数时, 表示占据电脑屏幕的比例

startx, starty: 这一坐标表示 矩形窗口左上角顶点的位置, 如果为空,则窗口位于屏幕中心

使用:

turtle.setup(width=0.9, height=0.9)
turtle.setup(0.9,0.9)		#和上面代码效果相同
turtle.setup(width=800, height=800, startx=100, starty=100)

二、画笔

2.1画笔的状态


在画布上,默认有一个坐标原点为画布中心的坐标轴, 坐标原点上有一只面朝x轴正方向小乌龟.

这里我们描述小乌龟时使用了两个词语:坐标原点(位置),面朝x轴正方向(方向), turtle绘图中, 就是使用位置方向描述小乌龟(画笔)的状态

2.2 画笔的属性


画笔(画笔的属性,颜色、画线的宽度)

turtle.pensize():设置画笔的宽度;

turtle.pencolor(); 没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色,可以是字符串如"green", “red”,也可以是RGB 3元组

turtle.speed(speed): 设置画笔移动速度,画笔绘制的速度范围[0,10]整数, 数字越大越快


2.3 绘图命令


操纵海龟绘图有着许多的命令,这些命令可以划分为3种:一种为运动命令,一种为画笔控制命令,还有一种是全局控制命令

1、画笔运动的命令:

2、画笔控制命令

3、全局控制命令

4、其他命令

三、circle函数

以给定半径画圆

turtle.circle(radius, extent=None, steps=None)

radius(半径); 半径为正(负),表示圆心在画笔的左边(右边)画圆
extent(弧度) (optional);
steps (optional) (做半径为radius的圆的内切正多边形,多边形边数为steps)

举例:

turtle.circle(50) # 整圆;
turtle.circle(50,steps=3) # 三角形;
turtle.circle(120, 180) # 半圆

四、绘图举例

奥运五环

import turtle as t
#直接到达坐标x,y的位置
def go(x,y) :
    t.penup()
    t.goto(x,y)
    t.pendown()
#设置画布大小、画笔大小、画笔粗细
def pen() :
    t.screensize(0.99, 0.99)
    t.pensize(10)
    t.speed(10)
def main() :
    pen()
    pencolor = ['blue','black','red','yellow','green']			#列表存储画笔颜色
    x = -450
    y = 0
    for i in range(5) :
        if i == 3 :
            x = -225
            y = -150
        if i < 3 :
            go(x + i * 450,y)
            t.pencolor(pencolor[i])
            t.circle(200)
        else :
            go(x + ( i - 3 ) * 450, y)
            t.pencolor(pencolor[i])
            t.circle(200)
if __name__ ==  '__main__':
    main()
t.done()

黑白皮卡丘

# coding:utf-8
import turtle as t

def infoPrt():
    print('coordinate: ' + str(t.pos()))
    print('angle: ' + str(t.heading()))

t.pensize(3)
t.hideturtle()
t.colormode(255)
t.color("black")
t.setup(700, 650)
t.speed(10)
t.st()
#t.dot()
t.pu()
#t.goto(-150,100)
t.goto(-210,86)
t.pd()
infoPrt()

# 头
print('头')
t.seth(85)
t.circle(-100,50)
#t.seth(78)
#t.circle(-100,25)

infoPrt()

t.seth(25)
t.circle(-170,50)
infoPrt()

# 右耳
print('右耳')
t.seth(40)
#t.circle(-250,52)
t.circle(-250,30)
infoPrt()


# 右耳尖
t.begin_fill()
# 左
t.circle(-250,22)
#t.fillcolor("pink")
# 右
t.seth(227)
t.circle(-270, 15)

prePos = t.pos()
infoPrt()
# 下
t.seth(105)
t.circle(100, 32)
t.end_fill()

t.pu()
t.setpos(prePos)
t.pd()
t.seth(212)
t.circle(-270, 28)

prePos = t.pos()
t.pu()
t.goto(t.xcor()+5,t.ycor()-2)
t.pd()

# 躯干
print('躯干')
t.seth(280)
t.circle(500, 30)
infoPrt()

# 臀部
print('臀部')
t.seth(120)
#t.circle(150, -55)
t.circle(150, -11)
p_tail=t.pos()
t.circle(150, -44)
p_butt=t.pos()
infoPrt()

# 尾巴
t.pu()
t.setpos(p_tail)
t.pd()

t.begin_fill()
t.seth(50)
t.fd(25)
t.seth(-50)
t.fd(30)
p_tail1=t.pos
t.seth(-140)
t.fd(36)
t.end_fill()
t.seth(39)
# 右尾和h1
t.fd(72)
# 右尾和v1
t.seth(125)
t.fd(48)
# 右尾和h2
t.seth(40)
t.fd(53)
# 右尾和v2
t.seth(88)
t.fd(45)
# 右尾和h3
t.seth(35)
t.fd(105)
# 右尾和v3
t.seth(105)
t.circle(850, 8)
#t.fd(105)
t.seth(215)
#t.fd(125)
t.circle(850, 11)
t.seth(280)
t.fd(110)
t.seth(220)
t.fd(50)
t.seth(309)
t.fd(56)

# 底盘
print('底盘')
t.pu()
t.setpos(p_butt)
t.pd()
t.seth(20)
t.circle(120, -45)
infoPrt()

t.seth(330)
t.circle(-150, -30)
infoPrt()

prePos = t.pos()
t.pu()
t.goto(t.xcor()+20,t.ycor())
t.pd()

t.seth(230)
t.circle(-70, 120)
p_bot=t.pos()

# 两脚-right
t.pu()
t.setpos(p_butt)
t.setpos(t.xcor()+5,t.ycor()+5)
t.pd()
t.seth(-86)
t.fd(30)
t.seth(-93)
t.fd(33)
t.seth(-225)
t.circle(-150, 22)
# 两脚-left
t.pu()
t.setpos(p_bot)
t.setpos(t.xcor()+85,t.ycor()-43)
t.pd()
t.seth(-105)
t.fd(50)
t.seth(-225)
t.circle(-150, 22)

# 左躯干
print('躯干')
t.pu()
t.setpos(p_bot)
t.pd()
t.seth(90)
t.circle(450, 13)
p_lfhd = t.pos()
t.circle(450, 5)
t.pu()
t.circle(450, 5)
t.pd()
t.circle(450, 6)
infoPrt()

# 左脸
print('左脸')
t.seth(330)
t.circle(50, -90)
infoPrt()

# 左酒窝
t.seth(30)
t.circle(-15, 120)
t.seth(-70)
t.circle(-30, 90)

# 左手
t.pu()
t.setpos(p_lfhd)
t.pd()
t.seth(160)
t.circle(150, 30)
infoPrt()

t.seth(180)
t.circle(-30, 150)
t.fd(67)

t.pu()
t.setpos(t.xcor()-40,t.ycor()-60)
t.pd()
t.seth(200)
t.circle(-5, 180)

# 右手
t.pu()
t.setpos(p_lfhd)
t.setpos(t.xcor()+180,t.ycor()+5)
t.pd()
t.seth(200)
t.circle(-50, 100)
t.pu()
t.circle(-50, 15)
t.pd()
t.circle(-50, 65)
t.pu()
t.setpos(t.xcor()+10,t.ycor()-45)
t.pd()
#t.seth(270)
#t.circle(-30, -180)
t.seth(80)
t.fd(10)
t.seth(165)
t.circle(10, 60)
t.seth(90)
t.fd(5)
t.seth(165)
t.circle(10, 60)
t.seth(95)
t.fd(5)
t.seth(185)
t.circle(10, 60)
t.seth(105)
t.fd(10)
t.seth(230)
t.fd(20)
t.seth(145)
t.fd(10)
t.seth(285)
t.fd(20)

# 右酒窝
t.pu()
t.setpos(t.xcor()-40,t.ycor()+110)
t.pd()
t.circle(27, 360)

# 嘴
t.pu()
t.setpos(t.xcor()-30,t.ycor()+28)
t.pd()
t.seth(280)
t.circle(-130, 30)
t.seth(270)
t.circle(-6, 160)
t.seth(130)
t.circle(-130, 30)
t.pu()
t.setpos(t.xcor()-5,t.ycor()+5)
t.pd()
t.seth(160)
t.circle(-20, -70)
t.seth(160)
t.circle(-30, -60)
t.pu()
t.setpos(t.xcor(),t.ycor()-28)
t.pd()
t.seth(200)
t.circle(50, 58)

# 左眼
t.pu()
t.setpos(t.xcor()-40,t.ycor()+90)
t.pd()
t.circle(5)
t.pu()
t.setpos(t.xcor()+5,t.ycor()+10)
t.pd()
t.begin_fill()
t.seth(190)
t.circle(15, 130)
t.seth(310)
t.circle(10, 15)
t.seth(0)
t.circle(17, 133)
t.seth(90)
t.circle(10, 15)
t.end_fill()
t.pu()
t.setpos(t.xcor()+2,t.ycor()-15)
t.pd()
t.color("white")
t.begin_fill()
t.circle(5)
t.end_fill()

# 右眼
t.pu()
t.setpos(t.xcor()+85,t.ycor()+15)
t.pd()
t.color("black")
t.circle(5)
t.pu()
t.setpos(t.xcor()+5,t.ycor()+10)
t.pd()
t.begin_fill()
t.seth(190)
t.circle(20, 130)
t.seth(310)
t.circle(10, 15)
t.seth(0)
t.circle(22, 133)
t.seth(90)
t.circle(13, 15)
t.end_fill()
t.pu()
t.setpos(t.xcor()-7,t.ycor()-15)
t.pd()
t.color("white")
t.begin_fill()
t.circle(7)
t.end_fill()

# 左耳
t.color("black")
t.pu()
t.goto(-210,86)
t.setpos(t.xcor()+15,t.ycor()+38)
t.pd()
t.seth(90)
t.circle(-250,30)

t.begin_fill()
# 左
t.circle(-250,18)

# 右
t.seth(270)
t.circle(-270, 12)

prePos = t.pos()
# 下
t.seth(180)
t.circle(100, 30)
t.end_fill()

t.pu()
t.setpos(prePos)
t.pd()
t.seth(270)
t.circle(-270, 18)


t.done()

史迪仔

from turtle import *
setup(650,650)
penup()
pensize(5)
speed(10)
pencolor("#065693")
seth(180)
fd(140)
seth(-90)
fd(50)
pendown()      #起点
fillcolor("#0079C6")
begin_fill()
seth(170)
circle(-40,100)
seth(180)
fd(50)
seth(180)
circle(-10,46)
seth(130)
circle(-300,40)#耳朵外廓大圆
circle(-100,45)
right(10)
circle(-50,30)
right(10)
circle(-30,30)
left(1)
fd(2)
right(1)
fd(3)
right(4)
fd(3)
right(3)
fd(5)
right(4)
fd(6)
right(4)
fd(10)
right(4)
fd(10)
right(3)
fd(15)
right(2)
fd(20)
right(2)
fd(20)
right(4)
fd(20)
right(3)
fd(30)
right(1)
fd(40)
right(1)
fd(60)
seth(-115)
fd(5)    #脸左侧开始逆时针
circle(200,30)
end_fill()
begin_fill()
left(8)
fd(20)
left(10)
fd(20)
left(14)
circle(100,30)
left(10)
circle(150,20)
right(2)
fd(55)
left(5)
fd(40)
left(3)
fd(25)
right(3)
circle(150,20)
left(7)
circle(100,30)
fd(5)#右侧耳朵下部开始
left(3)
circle(80,30)
right(3)
circle(80,30)
right(9)
circle(100,30)
left(2)
circle(200,20)
left(3)
fd(20)
seth(108)#小毛尖儿1
circle(30,5)
right(3)
circle(200,3)
left(7)
circle(20,5)
circle(15,10)
left(5)
circle(15,20)
left(11)
circle(15,20)
left(10)
circle(15,20)
left(9)
circle(13,15)
left(10)
circle(13,15)
left(8)
circle(20,15)
seth(135)  #小毛尖2
fd(20)
circle(8,168)
right(180)  #小毛尖3
circle(7,170)
seth(-176) #顶部结尾
fd(3)
circle(100,10)
right(5)
circle(70,15)
fd(3)
right(5)
circle(100,10)
right(4)
circle(80,10)
left(5)
circle(100,5)
right(6)
circle(100,5)
left(1)
circle(50,10)
right(10)
fd(9)
seth(-115)
fd(5)
circle(200,30)
end_fill() #脸廓结束
penup()    #眼睛左开始
seth(0)
fd(15)
seth(-60)
pendown()
fillcolor("#69C4EF")
begin_fill()
circle(50,20)
left(4)
circle(55,20)
right(4)
circle(50,20)
right(4)
circle(50,20)
left(13)
circle(50,20)
left(15)
circle(50,30)
right(10)
circle(80,20)
right(10)
circle(80,20)
right(7)
circle(80,20)
left(10)
circle(80,15)
left(17)
circle(80,15)
left(30)
circle(80,15)
left(10)
circle(80,15)
circle(80,15)
right(8)
circle(80,15)
right(7)
circle(80,15)
right(7)
circle(80,6)
end_fill()
penup()   #左眼内部(黑)开始
seth(0)
fd(34)
seth(90)
fd(10)
seth(-60)
pendown()
fillcolor("black")
begin_fill()
pencolor("black")
pensize(1)
circle(80,7)
left(20)
circle(80,9)
left(25)
circle(80,9)
left(30)
circle(80,9)
left(35)
circle(80,9)
left(10)
circle(80,3)
right(15)
fd(13)
left(15)
circle(80,10)
left(20)
circle(80,15)
left(25)
circle(80,10)
left(30)
circle(80,9)
left(35)
circle(80,15)
left(10)
circle(80,9)
circle(80,15)
end_fill() #左眼内部(黑)结束
penup()  #左眼内部(白)开始
seth(90)
fd(47)
seth(0)
fd(12)
pendown()
fillcolor("white")
begin_fill()
circle(-10,360)
end_fill() #左眼整体结束
penup()    #右眼开始
seth(0)
fd(237)
seth(-90)
pensize(5)
pencolor("#065693")
fillcolor("#69C4EF")
begin_fill()
pendown()
right(10)
circle(80,11)
right(30)
circle(80,11)
right(35)
circle(80,15)
right(35)
circle(80,12)
right(20)
circle(80,9)
right(37)
circle(80,9)
right(40)
circle(80,9)
right(38)
circle(80,9)
right(15)
circle(80,9)
fd(7)
right(11)
circle(80,9)
right(11)
circle(80,9)
right(12)
circle(80,9)
right(14)
circle(80,9)
right(16)
circle(80,5)
right(16)
circle(80,5)
right(18)
circle(80,5)
right(23)
circle(80,5)
right(25)
circle(80,5)
right(28)
circle(80,5)
right(5)
circle(80,5)
right(12)
circle(80,5)
right(15)
circle(80,5)
right(17)
circle(80,5)
right(15)
circle(80,5)
right(13)
circle(80,5)
right(13)
circle(80,9)
right(11)
circle(80,9)
right(11)
circle(80,5)
right(10)
circle(80,5)
right(10)
circle(80,9)
end_fill() #右眼外框结束
penup()    #右眼内部开始
seth(180)
fd(70)
seth(87)
pensize(1)
pencolor("black")
fillcolor("black")
begin_fill()
pendown()
circle(80,8)
right(15)
circle(80,7)
right(18)
circle(80,5)
right(23)
circle(80,5)
right(23)
circle(80,5)
right(23)
circle(80,5)
right(28)
circle(80,5)
right(35)
circle(80,5)
right(35)
circle(80,6)
right(37)
circle(80,6)
fd(5)
left(5)
circle(80,5)
right(3)
fd(5)
right(10)
circle(80,5)
right(15)
circle(80,5)
right(18)
circle(80,5)
right(25)
circle(80,5)
right(37)
circle(80,5)
right(38)
circle(80,7)
right(42)
circle(80,9)
right(38)
circle(80,9)
right(40)
fd(5)
end_fill()
penup() #右眼内部(白)开始
seth(0)
fd(22)
seth(90)
fd(10)
pendown()
pensize(1)
pencolor("white")
fillcolor("white")
begin_fill()
circle(10,360)
end_fill()#右眼内部(白)结束
penup()   #鼻子外开始
seth(180)
fd(167)
seth(-90)
fd(60)
pencolor("#07548C")
seth(0)
pendown()
fillcolor("#07548C")
begin_fill()
left(83)
circle(-80,30)
right(15)
circle(-80,30)
fd(5)
left(2)
circle(-80,15)
circle(-80,10)
circle(-80,20)
left(2)
circle(-80,9)
right(20)
circle(-80,20)
right(25)
circle(-80,20)
right(10)
circle(-80,15)
right(8)
circle(-80,12)
seth(-175)
fd(9)
left(2)
fd(6)
left(2)
fd(8)
right(3)
circle(-80,10)
right(3)
circle(-80,12)
circle(-80,10)
right(3)
circle(-80,10)
right(7)
circle(-80,10)
right(6)
circle(-80,8)
right(6)
circle(-80,8)
right(7)
circle(-80,7)
end_fill()#鼻子外结束
penup()#鼻子内开始
seth(8)
fd(20)
seth(-90)
fd(45)
pensize(1)
pencolor("#0A3873")
pendown()
fillcolor("#0A3873")
begin_fill()
seth(-30)
fd(20)
seth(110)
fd(20)
left(70)
circle(10,100)
end_fill()
penup()
seth(3)
fd(87)
seth(-90)
fd(5)
seth(47)
begin_fill()
pendown()
fd(20)
seth(227)
fd(20)
right(150)
fd(20)
right(70)
circle(-10,100)
end_fill()#鼻子结束
penup()#右耳朵开始
seth(0)
fd(95)
seth(90)
fd(45)
pendown()
fillcolor("#0079C6")
begin_fill()
pensize(5)
pencolor("#065693")
seth(20)
circle(40,95)
right(100)
fd(50)
circle(10,46)
seth(45)
circle(300,40)
circle(100,45)
left(10)
circle(50,30)
left(10)
circle(30,30)
right(1)
fd(2)
left(1)
fd(3)
left(4)
fd(3)
left(3)
fd(5)
left(4)
fd(6)
left(4)
fd(10)
left(4)
fd(10)
left(3)
fd(15)
left(2)
fd(20)
left(2)
fd(20)
left(4)
fd(20)
left(3)
fd(30)
left(1)
fd(40)
left(1)
fd(60)
left(3)
fd(51)
left(70)
pensize(1)
fd(8)
right(3)
fd(8)
right(3)
fd(8)
right(3)
fd(5)
right(3)
fd(5)
right(2)
fd(5)
right(2)
fd(5)
right(3)
fd(9)
right(3)
fd(10)
right(3)
fd(9)
right(5)
fd(9)
right(6)
fd(6)
right(6)
fd(6)
right(7)
fd(6)
right(7)
fd(6)
end_fill()#右耳朵外廓完成
penup()#右耳内廓开始
seth(0)
fd(6)
seth(90)
fd(20)
seth(45)
pendown()
pensize(1)
pencolor("#F7CEDC")
fillcolor("#F7CEDC")
begin_fill()
circle(40,75)
right(106)
fd(53)
circle(10,40)
seth(47)
circle(310,40)
left(10)
circle(80,45)
left(25)
circle(40,30)
left(23)
circle(30,20)
seth(-145)
right(1)
fd(2)
left(1)
fd(3)
left(4)
fd(3)
left(3)
fd(5)
left(4)
fd(6)
left(4)
fd(10)
left(4)
fd(10)
left(3)
fd(15)
left(2)
fd(20)
left(2)
fd(20)
left(3)
fd(20)
left(2)
fd(30)
left(2)
fd(30)
left(2)
fd(40)
left(3)
fd(30)
left(1)
fd(7)
left(2)
fd(7)
left(12)
fd(2)
left(8)
fd(20)
left(10)
fd(10)
left(15)
fd(10)
right(2)
fd(20)
right(10)
fd(15)
right(8)
fd(10)
end_fill()#右耳内廓结束
penup()
seth(180)
fd(327)
seth(-90)
fd(34)
pendown()
begin_fill()
seth(140)
circle(-40,75)
left(113)
fd(53)
circle(-7,40)
seth(130)
circle(-310,40)
right(17)
circle(-80,45)
right(20)
circle(-40,30)
right(23)
circle(30,20)
seth(-47)
left(3)
fd(2)
right(2)
fd(3)
right(2)
fd(3)
right(3)
fd(5)
right(4)
fd(6)
right(4)
fd(10)
right(4)
fd(10)
right(3)
fd(15)
right(1)
fd(20)
right(1)
fd(20)
right(1)
fd(30)
right(1)
fd(40)
right(1)
fd(40)
right(11)
fd(2)
right(15)
left(20)
right(10)
fd(20)
right(15)
fd(10)
left(5)
fd(20)
right(5)
fd(20)
left(10)
fd(20)
left(3)
fd(20)
end_fill()#脸部结束
penup()#身体开始
seth(0)
fd(70)
seth(-90)
fd(80)
pensize(5)
pencolor("#065693")
fillcolor("#0079C6")
begin_fill()
seth(-112)
pendown()
circle(220,22)
right(86)
circle(70,40)
right(90)
fd(8)
right(33)
circle(10,160)
right(9)
fd(8)
right(50)
fd(9)
right(28)
fd(6)
circle(8,160)
left(5)
fd(6)
right(85)
fd(9)
right(28)
fd(6)
circle(6,110)
fd(4)
right(23)
fd(5)
left(2)
circle(80,10)
left(2)
circle(80,5)
left(4)
circle(80,10)
left(7)
circle(80,10)
left(7)
circle(80,10)
right(2)
fd(5)
right(1)
circle(80,10)
left(4)
circle(80,10)
right(10)
circle(-80,10)
right(8)
circle(-80,10)
right(11)
circle(-80,10)
right(90)
fd(5)
right(5)
circle(10,180)
fd(2)
right(130)
fd(5)
left(5)
circle(10,130)
fd(5)
right(80)
fd(5)
circle(10,180)
seth(0)
fd(65)
right(180)
circle(10,190)
fd(5)
right(90)
fd(5)
circle(10,150)
left(10)
fd(5)
right(90)
fd(2)
circle(10,180)
right(20)
fd(3)
right(125)
circle(-80,10)
right(11)
circle(-80,10)
right(8)
circle(-80,10)
right(10)
circle(80,10)
left(4)
circle(80,10)
right(1)
fd(5)
right(2)
circle(80,10)
left(7)
circle(80,10)
left(7)
circle(80,10)
left(4)
circle(80,5)
left(2)
circle(80,10)
left(2)
fd(5)
right(23)
fd(4)
circle(6,110)
fd(6)
right(28)
fd(9)
right(85)
fd(6)
left(5)
circle(8,160)
fd(6)
right(28)
fd(9)
right(50)
fd(8)
right(9)
circle(10,160)
right(33)
fd(8)
right(90)
circle(70,40)
right(92)
circle(210,22)
fd(16)#身体外廓结束
left(90)
circle(-80,5)
right(1)
circle(-80,5)
right(1)
circle(80,5)
right(1)
circle(80,5)
right(2)
fd(5)
right(1)
fd(5)
right(1)
fd(5)
right(3)
fd(5)
right(2)
fd(5)
right(3)
fd(7)
right(2)
fd(7)
right(2)
fd(7)
left(2)
fd(5)
left(2)
fd(7)
right(3)
fd(7)
left(2)
fd(7)
right(2)
fd(7)
left(2)
fd(7)
right(2)
fd(7)
left(2)
fd(7)
right(2)
fd(7)
left(2)
fd(7)
right(2)
fd(7)
left(2)
fd(7)
right(5)
fd(7)
left(2)
fd(7)
right(7)
fd(7)
left(2)
fd(7)
right(8)
fd(6)
end_fill()#身体外廓结束
penup()#身体内部开始
seth(0)
fd(48)
right(65)
pendown()
fillcolor("#69C4EF")
begin_fill()
circle(-300,24)
circle(10,90)
seth(0)
fd(50)
circle(10,90)
left(9)
circle(-300,26)
fd(3)
seth(-167)
fd(5)
right(1)
fd(7)
left(1)
fd(7)
right(2)
fd(7)
left(1)
fd(7)
right(3)
fd(7)
left(2)
fd(7)
right(3)
fd(7)
right(3)
fd(7)
left(2)
fd(7)
right(4)
fd(7)
left(2)
fd(7)
right(3)
fd(7)
left(2)
fd(7)
right(2)
fd(7)
left(3)
fd(7)
left(3)
fd(5)
end_fill()#身体外廓中间结束
penup()
seth(-90)
fd(154)
seth(0)
fd(4)
pensize(1)
fillcolor("#065693")
pencolor("#065693")
pendown()
begin_fill()
seth(-155)
fd(8)
right(120)
fd(8)
right(90)
circle(-10,50)
end_fill()
penup()
seth(0)
fd(16)
seth(-120)
pendown()
fd(8)
begin_fill()
right(113)
fd(8)
right(90)
circle(-10,57)
end_fill()
penup()
seth(0)
fd(10)
seth(90)
fd(10)
pendown()
begin_fill()
seth(-45)
fd(8)
right(120)
fd(8)
right(90)
circle(-10,57)
end_fill()
penup()
seth(0)
fd(72)
seth(-90)
fd(2)
seth(-115)
pendown()#
begin_fill()
fd(8)
left(90)
circle(10,57)
end_fill()
penup()
seth(0)
fd(4)
seth(-90)
fd(6)
seth(-70)
pendown()
begin_fill()
fd(8)
left(90)
circle(10,57)
end_fill()
penup()
seth(0)
fd(4)
seth(90)
fd(5)
seth(0)
pendown()
begin_fill()
fd(8)
left(90)
circle(10,57)
end_fill()#手结束
penup()#左脚掌开始
seth(180)
fd(237)
seth(-90)
fd(10)
seth(180)
pendown()
fillcolor("#065693")
begin_fill()
circle(-17,360)
end_fill()#左脚掌结束
seth(180)#左脚丫开始
penup()
fd(20)
seth(90)
fd(7)
seth(180)
right(15)
pendown()
begin_fill()
fd(9)
right(120)
fd(9)
right(120)
fd(9)
end_fill()
penup()
seth(90)
fd(20)
seth(180)
right(53)
pendown()
begin_fill()
fd(9)
right(120)
fd(9)
right(120)
fd(9)
end_fill()
penup()
seth(0)
fd(10)
seth(90)
fd(10)
right(20)
pendown()
begin_fill()
fd(9)
right(120)
fd(9)
right(120)
fd(9)
end_fill()#左脚丫结束
penup()#右脚掌开始
seth(0)
fd(345)
seth(-90)
fd(12)
seth(0)
pendown()
begin_fill()
circle(17,360)
end_fill()#右脚掌结束
penup()#右脚丫开始
fd(23)
seth(90)
fd(4)
seth(0)
left(90)
pendown()
begin_fill()
fd(9)
right(120)
fd(9)
right(120)
fd(9)
end_fill()
penup()
seth(90)
fd(25)
seth(0)
left(127)
pendown()
begin_fill()
fd(9)
right(120)
fd(9)
right(120)
fd(9)
end_fill()
penup()
seth(180)
fd(27)
seth(90)
fd(12)
seth(0)
pendown()
begin_fill()
left(4)
fd(9)
left(120)
fd(9)
left(120)
fd(9)
end_fill()
penup()#右脚丫结束
fd(200)
done()

穿雨靴的鸭子

#穿雨鞋的小鸭
from turtle import *
 
#扁嘴
pensize(2)
 
pu()
goto(-100,100)#上嘴最高顶点
seth(-50)
pd()
color('#6C3100','#FADD77')
begin_fill()
fd(16)
vertex_right = pos()#嘴最右顶点
rt(50)
fd(12)
vertex_down = pos()#下嘴最低顶点
rt(80)
fd(30)
circle(-3,200)
vertex_left = pos()#嘴最左顶点
goto(-100,100)
end_fill()
goto(vertex_left)#回到最左顶点
circle(-3,-200)#扁嘴
goto(vertex_right)
    
#身体
#头颈背尾曲线
color('#B6A88E')
pu()
goto(-100,100)
pd()
 
seth(80)
circle(-36,160)
fd(25)
circle(115,20)
circle(60,55)
circle(-200,20)
circle(110,20)
color('#7D6A4C')
circle(40,40)
color('#B6A88E')
seth(-100)
circle(-180,30)
circle(-20,50)
 
#右鸭腿
circle(20,70)
color('#736856')
circle(-12,120)
leg_pos1 = pos()#定位左腿位置
fd(25)
 
#前胸肚曲线
pu()
goto(vertex_down)
pd()
seth(-10)
color('#B9AD9D')
circle(-40,50)
circle(-80,48)
color('#736856')
circle(250,5)
circle(50,75)
color('#B9AD9D')
circle(220,28)
 
#左鸭腿
pu()
seth(175)
fd(40)
pd()
seth(-120)
fd(8)
circle(-10,120)
leg_pos2 = pos()#定位右腿位置
fd(15)
 
#眼睛
color('black')
#左眼
pu()
goto(vertex_down - (1,-29))
pd()
dot(4,'black')#相比circle(),不需要再额外填充颜色
#右眼
pu()
goto(vertex_down + (23,20))
pd()
dot(4,'black')
 
 
#翅膀
color('#BCB2A6')
pu()
goto(vertex_down - (-75,130))
seth(130)
pd()
circle(-25,130)
circle(-100,30)
fd(85)
point = pos()
rt(137)
fd(52)
circle(-100,58)
 
pu()
goto(point)
lt(30)
pd()
fd(60)
 
pu()
goto(point)
pd()
lt(10)
fd(70)
 
 
#腿部
#左腿
def leg(pos0):#鸭腿绘制函数
    pensize(8)
    color('#ECC578')
    pu()
    goto(pos0)
    seth(0)
    fd(7)
    seth(-90)
    fd(8.5)
    pd()
    fd(20)#腿长
 
leg(leg_pos1)
leg(leg_pos2)
 
#小红靴——函数
def boot(pos0):
    pensize(2)
    color('#B4070B','#FBA06B')
    pu()
    goto(pos0)#靴子右上顶点
    pd()
    begin_fill()
    seth(140)
    circle(25,80)
    seth(-80)
    fd(35)#fd(30)左侧线条
 
    circle(-2,60)#靴低
    fd(20)
    circle(4,180)    
    seth(5)
    fd(30)
    circle(2,60)
    
    goto(pos0)#右侧线条
    end_fill()
    
boot(leg_pos1-(-20,30))
boot(leg_pos2-(-20,30)) 
    
 
#小雨滴
color('#77DDFF','#D8E8E5')
fd_ls = [200,140,250,240,230,220,180,250]
lt_ls = [30,60,60,100,125,170,200,330]
for i in range(8):
    pu()
    home()    
    lt(lt_ls[i])
    fd(fd_ls[i])
 
    pd()
    seth(-78)
    fd(15)
    begin_fill()
    circle(-3,200)
    end_fill()
    fd(15)
 
#文字
pu()
goto(vertex_left)
seth(180)
fd(150)
seth(-90)
fd(300)
color('black')
write('code by totoup',font=("Arial",15,"normal"))
    
hideturtle()
done()

为解决初学者学习上的困难,专门建立的Python学习圈,从零基础开始到Python各领域的项目实战教程、开发工具与电子书籍。与你分享企业当下对于python人才需求及学好python的高效技巧,不停更新最新教程! 需要的可以私聊我哦!

相关文章

编程与数学:在Python里怎么用turtle库函数填色?

这里只给出一个示例,一个最简单的示例。看懂这个示例,你就能在自己的代码里需要填色的地方填色。首先,与前面发的Python绘画程序一样,先要装入turtle库。然后在代码中,下面需要填色时,先写一个填色...

使用Python寻找图像最常见的颜色

如果我们知道图像或对象最常见的是哪种颜色,那么可以解决图像处理中的几个用例,例如在农业领域,我们可能需要确定水果的成熟度。我们可以简单地检查一下水果的颜色是否在预定的范围内,看看它是成熟的,腐烂的,还...

Python其实很简单 第二十六章 用Python也能绘图

在Python中绘图时,需要导入图形模块turtle。“turtle”的意思是“海龟”,所以这个模块又被称为海龟图形模块。有一个比较出名的集成编程环境叫海龟编辑器,以积木的形式表示命令,编程过程由“写...

我们怎么样使用Python实现一个简单画图软件界面

精选python重要知识设计实践,奉献给一直努力的你,流量不够,感谢头条君,看到是一种缘分,仅给能看到的朋友、同学们默默献上。我想搞一个画图软件,就要一个简单的画布,如我们生活中,要有纸和笔,或者树枝...

OpenCV-Python绘制多边形和选择随机颜色

绘制多边形在使用 opencv 绘制几何图像时,除了大多数情况下绘制的简单形状,有时也需要绘制复杂的多边形形状。下面就来一起尝试如何绘制多边形形状。首先需要了解几个 opencv 库函数。cv.pol...

Colorama:为Python命令行增添一抹色彩

在编程的世界里,命令行界面(CLI)是开发者与计算机交流的重要方式之一。然而,传统的黑白命令行界面往往显得单调乏味,缺乏视觉吸引力。今天,我们将介绍一个能让Python命令行程序“活”起来的神奇模块—...