Python中使用小海龟画图

 一、画笔

  在画布上,默认画布中心为坐标原点,且隐藏着一个直角坐标系,小海龟的初始正方向规定为X轴的正方向。使用小海龟绘图时,确定方向与距离进行绘制图像。

1.画笔自身属性

命令 说明
turtle.pensize) 设置画笔的宽度
turtle.pencolor) 设置画笔的颜色,可以使用“black”“red”或者RGB3元组
turtle.speed) 设置画笔的移动速度,参数的范围在[0,10]之间的整数,数字越大,速度越快

2.画笔运动属性

命令 说明
turtle.forwarddistance) 向当前画笔方向移动distance像素长
turtle.backwarddistance) 向当前画笔相反方向移动distance像素长度
turtle.rightdegree) 顺时针移动degree°
turtle.leftdegree) 逆时针移动degree°
turtle.pendown) 移动时绘制图形,缺省时也为绘制
turtle.gotox,y) 将画笔移动到坐标为x,y的位置
turtle.penup) 移动时不绘制图形,提起笔,用于另起一个地方绘制时用
turtle.speedspeed) 画笔绘制的速度范围[0,10]整数
turtle.circle) 画圆,半径为正负),表示圆心在画笔的左边右边)画圆

3.画笔的控制命令

命令 说明
turtle.pensizewidth) 绘制图形时的宽度
turtle.pencolor) 画笔颜色
turtle.fillcolorcolorstring) 绘制图形的填充颜色
turtle.colorcolor1, color2) 同时设置pencolor=color1, fillcolor=color2
turtle.filling) 返回当前是否在填充状态
turtle.begin_fill) 准备开始填充图形
turtle.end_fill) 填充完成;
turtle.hideturtle) 隐藏箭头显示;
turtle.showturtle) 与hideturtle)函数对应

4.其他操作:

turtle.hideturtle)   # 隐藏小海龟画笔
turtle.mainloop)     # 保留最后图像

二、图像展示

1.Mercedes LOGO

       

    奔驰标志 1.0

        

    奔驰标志 2.0

 

                  奔驰标志 3.0

附 3.0版本代码如下:

import turtle as t
import time
R = 150
r = R - 10

t.pensize10)
t.color"black","white")
t.speed10)
t.circleR,360) # 画圆圈
t.penup)
t.left90)
t.forwardR)
t.pensize1)
t.pendown)
t.forwardR)
t.pensize1)
t.fillcolor"black")
# 左侧三角形
t.begin_fill)
t.left170)
t.forwardr)
t.right180)
t.forwardr)
t.right170)
t.forwardR)
t.right60)
t.forwardR)
t.right170)
t.forwardr)
t.end_fill)
# 下面三角形
t.right180)
t.forwardr)
t.begin_fill)
t.left160)
t.forwardr)
t.right180)
t.forwardr)
t.right170)
t.forwardR)
t.right60)
t.forwardR)
t.right170)
t.forwardr)
t.end_fill)
# 右边三角形
t.right180)
t.forwardr)
t.begin_fill)
t.left160)
t.forwardr)
t.right180)
t.forwardr)
t.right170)
t.forwardR)
t.right60)
t.forwardR)
t.right170)
t.forwardr)
t.end_fill)

t.hideturtle) # 隐藏小海龟画笔
t.mainloop) # 保留图像

奔驰标志3.0代码

3.0代码改进版(感谢@Hui_101 提供)

import turtle as t
R = 150
r = R - 10
t.color'black','red')
t.speed10)
t.pensize10)
t.circleR,360)
t.left90)
t.penup)
t.pensize1)
t.forward300)
t.pendown)

t.begin_fill)
t.left170)
t.forwardr)
t.right40)
t.forwardr)
t.left160)
t.forwardr)
t.right40)
t.forwardr)
t.left160)
t.forwardr)
t.right40)
t.forwardr)
t.end_fill)

t.hideturtle)
t.mainloop)

3.0版本改进版

2. 画方格

# 画方格
import turtle as t
t.pensize1)
for i in range1,300,2):
            t.left90)
            t.fdi*2)

3.来盘蚊香

import turtle as t
import time

R = 150

t.speed100)
t.pensize3)
t.color"blue")

for i in range50):
            t.circleR-i*3, 180)

4.Tencent LOGO

       

     公企鹅效果图 

 

       母企鹅效果图

 附企鹅代码:

import turtle
import time

def head):
    
    #配置画笔属性
    turtle.fillcolor"black")
    turtle.color"black")
    turtle.colormode255)
    turtle.speed1)

    #绘制母企鹅头部
    turtle.penup)
    turtle.goto-300,350)
    turtle.seth0)
    turtle.pendown)
    turtle.begin_fill)
    turtle.circle-200,-110)   
    turtle.penup)
    turtle.goto-300,350)
    turtle.pendown)
    turtle.seth0)
    turtle.circle-200,110) 
    turtle.seth213)
    turtle.circle-345,66)
    turtle.end_fill)


def lefteye):

    #配置画笔属性
    turtle.Turtle).screen.delay0)
    turtle.fillcolor"white")
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的左眼
    turtle.pu)
    turtle.goto-350,275)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill)

    #画椭圆
    a=1.0
    for i in range120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt3) #向左转3度
            turtle.fda) #向前走a的步长
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)
    
    #绘制左眼眼影
    turtle.begin_fill)
    turtle.fillcolor"pink")
    turtle.pu)
    turtle.goto-378,255)
    turtle.pendown)
    turtle.color"pink")
    turtle.seth15)
    turtle.circle-100,35)
    turtle.pu)
    turtle.goto-350,275)
    turtle.seth180)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.goto-350,275)
    turtle.seth0)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)

    #绘制睫毛
    turtle.pu)
    turtle.goto-350,275)
    turtle.seth180)
    turtle.pd)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pensize3)
    turtle.color242,156,177)
    turtle.circle-20,190)
    turtle.pensize1)

    #绘制母企鹅的左眼珠
    turtle.color"black")
    turtle.fillcolor"black")
    turtle.begin_fill)
    turtle.pu)
    turtle.goto-330,245)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill) 
    a=0.01
    for i in range120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.05
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.05
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)
    

def righteye):
    
    #配置画笔属性
    turtle.Turtle).screen.delay0)
    turtle.fillcolor"white")
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的右眼
    turtle.pu)
    turtle.goto-250,275)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill)

    a=1.0
    for i in range120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)
    
    #绘制右眼眼影
    turtle.begin_fill)
    turtle.fillcolor"pink")
    turtle.pu)
    turtle.goto-278,255)
    turtle.pendown)
    turtle.color"pink")
    turtle.seth15)
    turtle.circle-100,35)
    turtle.pu)
    turtle.goto-250,275)
    turtle.seth180)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.goto-250,275)
    turtle.seth0)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt3) 
            turtle.fda) 
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)

    #绘制睫毛
    turtle.pu)
    turtle.goto-250,275)
    turtle.seth0)
    turtle.pd)
    a=1
    for i in range20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt3) #向左转3度
            turtle.fda) #向前走a的步长
        else:
            a=a-0.08
            turtle.lt3)
            turtle.fda)
    turtle.pensize3)
    turtle.color242,156,177)
    turtle.circle20,190)
    turtle.pensize1)

    #绘制母企鹅的右眼珠
    turtle.color"black")
    turtle.fillcolor"black")
    turtle.begin_fill)
    turtle.pu)
    turtle.goto-270,245)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill) 
    a=0.01
    for i in range120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.05
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.05
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)
    

def mouth):

    #配置画笔属性
    turtle.fillcolor250,175,8)) 
    turtle.color250,175,8))
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的嘴巴
    turtle.pu)
    turtle.goto-410,120)
    turtle.seth30)
    turtle.pd)
    turtle.begin_fill) 
    turtle.circle-225,60)
    turtle.circle-8,150)
    turtle.seth210)
    turtle.circle-225,60)
    turtle.seth-210)
    turtle.circle-8,150)
    turtle.pu)
    turtle.end_fill)


def bowknot):

    #配置画笔属性
    turtle.speed1)
    turtle.fillcolor"pink")
    turtle.pu)
    turtle.color"pink")
    turtle.goto-250,360)
    turtle.pd)
    turtle.seth225)

    #绘制蝴蝶结的上半部分
    turtle.begin_fill)
    turtle.fillcolor"pink")
    turtle.pu)
    turtle.goto-250,360)
    turtle.pd)
    turtle.circle-70,100)
    turtle.seth50)
    turtle.circle-80,53)
    turtle.pu)
    turtle.goto-250,360)
    turtle.seth50)
    turtle.pd)
    turtle.circle70,100)
    turtle.seth225)
    turtle.circle80,53)
    turtle.end_fill)

    #绘制蝴蝶结下半部分
    turtle.pu)
    turtle.goto-195,335)
    turtle.pd)
    turtle.begin_fill)
    turtle.fillcolor"pink")
    turtle.seth45)
    turtle.circle-70,100)
    turtle.seth230)
    turtle.circle-80,62.5)
    turtle.pu)
    turtle.goto-195,335)
    turtle.seth225)
    turtle.pd)
    turtle.circle70,100)
    turtle.seth50)
    turtle.circle80,58)
    turtle.end_fill)
    
    #蝴蝶结的中间部分
    turtle.begin_fill)
    turtle.fillcolor"pink")
    turtle.pu)
    turtle.color"pink")
    turtle.goto-255,370)
    turtle.pd)
    turtle.seth225)
    turtle.circle40,360)
    turtle.end_fill)
    
def scarf):

    #配置画笔属性
    turtle.fillcolor"pink") 
    turtle.color"pink")
    turtle.speed1)
    turtle.colormode255)

    #绘制围巾
    turtle.pu)
    turtle.goto-489,82)
    turtle.seth-115)
    turtle.pd)
    turtle.begin_fill) 
    turtle.fd75)
    turtle.seth-30)
    turtle.circle450,10)
    turtle.seth-90)
    turtle.fd100)
    turtle.seth-35)
    turtle.circle50,70)
    turtle.seth90)
    turtle.fd95)
    turtle.seth-12)
    turtle.circle450,40)
    turtle.seth110)
    turtle.fd70)
    turtle.seth213)
    turtle.circle-345,64)
    turtle.pu)
    turtle.end_fill) 


def body):
    
    #配置画笔属性
    turtle.fillcolor"black") 
    turtle.color"black") 
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的身体
    turtle.pu)
    turtle.goto-489,82)
    turtle.seth-115)
    turtle.pd)
    turtle.begin_fill) 
    turtle.fd175)
    turtle.circle120,40)
    turtle.seth30)
    turtle.circle200,40)
    turtle.seth-90)
    turtle.circle174,180)
    turtle.seth-70)
    turtle.circle200,40)
    turtle.seth70)
    turtle.circle120,43)
    turtle.fd175)
    turtle.seth213)
    turtle.circle-345,66)
    turtle.pu)
    turtle.end_fill)


def feet):

    #配置画笔属性
    turtle.fillcolor250,175,8)) 
    turtle.color250,175,8)) 
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的左脚
    turtle.pu)
    turtle.goto-300,-228)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill) 
    turtle.fd200)
    turtle.circle-10,120)
    turtle.circle-120,40)
    turtle.pu)
    turtle.end_fill)

    #绘制母企鹅的右脚
    turtle.pu)
    turtle.goto-300,-228)
    turtle.seth0)
    turtle.pd)
    turtle.begin_fill) 
    turtle.fd200)
    turtle.circle10,120)
    turtle.circle120,50)
    turtle.pu)
    turtle.end_fill)


def belly):

    #配置画笔属性
    turtle.fillcolor"white") 
    turtle.color"white") 
    turtle.speed1)
    turtle.colormode255)

    #绘制母企鹅的白色肚皮
    turtle.pu)
    turtle.goto-305,28)
    turtle.seth180)
    turtle.pd)
    turtle.begin_fill) 

    #画椭圆
    a=4.5
    for i in range120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.1
            turtle.lt3) 
            turtle.fda) 
        else:
            a=a-0.1
            turtle.lt3)
            turtle.fda)
    turtle.pu)
    turtle.end_fill)
    turtle.pu)
    turtle.end_fill)    

turtle.Turtle).screen.delay0)

head);
bowknot);
lefteye);
righteye);
mouth);
feet);
body);
belly);
scarf);
turtle.hideturtle)
turtle.mainloop)

企鹅代码

小企鹅生成动画:

三、参考博文

本文中部分基本操作参照 博文 10分钟轻松学会 Python turtle 绘图

本人计算机小白一枚,对编程有浓厚兴趣,在此贴出自己的计算机学习历程,还有很多不足,望多多指教!
读书后发现好多的内容与具体专业有偏差,没来得及完成,虽然“有时间我就会做…”是人生最大的谎言,但有时间我会继续搞定未完成的内容,有始有终,兴趣使然!

Published by

风君子

独自遨游何稽首 揭天掀地慰生平