Python中使用小海龟画图

 一、画笔

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

1.画笔自身属性

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

2.画笔运动属性

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

3.画笔的控制命令

命令说明
turtle.pensize(width) 绘制图形时的宽度
turtle.pencolor() 画笔颜色
turtle.fillcolor(colorstring) 绘制图形的填充颜色
turtle.color(color1, 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.pensize(10)
t.color("black","white")
t.speed(10)
t.circle(R,360) # 画圆圈
t.penup()
t.left(90)
t.forward(R)
t.pensize(1)
t.pendown()
t.forward(R)
t.pensize(1)
t.fillcolor("black")
# 左侧三角形
t.begin_fill()
t.left(170)
t.forward(r)
t.right(180)
t.forward(r)
t.right(170)
t.forward(R)
t.right(60)
t.forward(R)
t.right(170)
t.forward(r)
t.end_fill()
# 下面三角形
t.right(180)
t.forward(r)
t.begin_fill()
t.left(160)
t.forward(r)
t.right(180)
t.forward(r)
t.right(170)
t.forward(R)
t.right(60)
t.forward(R)
t.right(170)
t.forward(r)
t.end_fill()
# 右边三角形
t.right(180)
t.forward(r)
t.begin_fill()
t.left(160)
t.forward(r)
t.right(180)
t.forward(r)
t.right(170)
t.forward(R)
t.right(60)
t.forward(R)
t.right(170)
t.forward(r)
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.speed(10)
t.pensize(10)
t.circle(R,360)
t.left(90)
t.penup()
t.pensize(1)
t.forward(300)
t.pendown()

t.begin_fill()
t.left(170)
t.forward(r)
t.right(40)
t.forward(r)
t.left(160)
t.forward(r)
t.right(40)
t.forward(r)
t.left(160)
t.forward(r)
t.right(40)
t.forward(r)
t.end_fill()

t.hideturtle()
t.mainloop()
3.0版本改进版

2. 画方格

# 画方格
import turtle as t
t.pensize(1)
for i in range(1,300,2):
            t.left(90)
            t.fd(i*2)

3.来盘蚊香

import turtle as t
import time

R = 150

t.speed(100)
t.pensize(3)
t.color("blue")

for i in range(50):
            t.circle(R-i*3, 180)

4.Tencent LOGO

       

     公企鹅效果图 

 

       母企鹅效果图

 附企鹅代码:

import turtle
import time

def head():
    
    #配置画笔属性
    turtle.fillcolor("black")
    turtle.color("black")
    turtle.colormode(255)
    turtle.speed(1)

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


def lefteye():

    #配置画笔属性
    turtle.Turtle().screen.delay(0)
    turtle.fillcolor("white")
    turtle.speed(1)
    turtle.colormode(255)

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

    #画椭圆
    a=1.0
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt(3) #向左转3度
            turtle.fd(a) #向前走a的步长
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()
    
    #绘制左眼眼影
    turtle.begin_fill()
    turtle.fillcolor("pink")
    turtle.pu()
    turtle.goto(-378,255)
    turtle.pendown()
    turtle.color("pink")
    turtle.seth(15)
    turtle.circle(-100,35)
    turtle.pu()
    turtle.goto(-350,275)
    turtle.seth(180)
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.goto(-350,275)
    turtle.seth(0)
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()

    #绘制睫毛
    turtle.pu()
    turtle.goto(-350,275)
    turtle.seth(180)
    turtle.pd()
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pensize(3)
    turtle.color(242,156,177)
    turtle.circle(-20,190)
    turtle.pensize(1)

    #绘制母企鹅的左眼珠
    turtle.color("black")
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pu()
    turtle.goto(-330,245)
    turtle.seth(180)
    turtle.pd()
    turtle.begin_fill() 
    a=0.01
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.05
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.05
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()
    

def righteye():
    
    #配置画笔属性
    turtle.Turtle().screen.delay(0)
    turtle.fillcolor("white")
    turtle.speed(1)
    turtle.colormode(255)

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

    a=1.0
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()
    
    #绘制右眼眼影
    turtle.begin_fill()
    turtle.fillcolor("pink")
    turtle.pu()
    turtle.goto(-278,255)
    turtle.pendown()
    turtle.color("pink")
    turtle.seth(15)
    turtle.circle(-100,35)
    turtle.pu()
    turtle.goto(-250,275)
    turtle.seth(180)
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.goto(-250,275)
    turtle.seth(0)
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt(3) 
            turtle.fd(a) 
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()

    #绘制睫毛
    turtle.pu()
    turtle.goto(-250,275)
    turtle.seth(0)
    turtle.pd()
    a=1
    for i in range(20):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            turtle.rt(3) #向左转3度
            turtle.fd(a) #向前走a的步长
        else:
            a=a-0.08
            turtle.lt(3)
            turtle.fd(a)
    turtle.pensize(3)
    turtle.color(242,156,177)
    turtle.circle(20,190)
    turtle.pensize(1)

    #绘制母企鹅的右眼珠
    turtle.color("black")
    turtle.fillcolor("black")
    turtle.begin_fill()
    turtle.pu()
    turtle.goto(-270,245)
    turtle.seth(180)
    turtle.pd()
    turtle.begin_fill() 
    a=0.01
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.05
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.05
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()
    

def mouth():

    #配置画笔属性
    turtle.fillcolor((250,175,8)) 
    turtle.color((250,175,8))
    turtle.speed(1)
    turtle.colormode(255)

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


def bowknot():

    #配置画笔属性
    turtle.speed(1)
    turtle.fillcolor("pink")
    turtle.pu()
    turtle.color("pink")
    turtle.goto(-250,360)
    turtle.pd()
    turtle.seth(225)

    #绘制蝴蝶结的上半部分
    turtle.begin_fill()
    turtle.fillcolor("pink")
    turtle.pu()
    turtle.goto(-250,360)
    turtle.pd()
    turtle.circle(-70,100)
    turtle.seth(50)
    turtle.circle(-80,53)
    turtle.pu()
    turtle.goto(-250,360)
    turtle.seth(50)
    turtle.pd()
    turtle.circle(70,100)
    turtle.seth(225)
    turtle.circle(80,53)
    turtle.end_fill()

    #绘制蝴蝶结下半部分
    turtle.pu()
    turtle.goto(-195,335)
    turtle.pd()
    turtle.begin_fill()
    turtle.fillcolor("pink")
    turtle.seth(45)
    turtle.circle(-70,100)
    turtle.seth(230)
    turtle.circle(-80,62.5)
    turtle.pu()
    turtle.goto(-195,335)
    turtle.seth(225)
    turtle.pd()
    turtle.circle(70,100)
    turtle.seth(50)
    turtle.circle(80,58)
    turtle.end_fill()
    
    #蝴蝶结的中间部分
    turtle.begin_fill()
    turtle.fillcolor("pink")
    turtle.pu()
    turtle.color("pink")
    turtle.goto(-255,370)
    turtle.pd()
    turtle.seth(225)
    turtle.circle(40,360)
    turtle.end_fill()
    
def scarf():

    #配置画笔属性
    turtle.fillcolor("pink") 
    turtle.color("pink")
    turtle.speed(1)
    turtle.colormode(255)

    #绘制围巾
    turtle.pu()
    turtle.goto(-489,82)
    turtle.seth(-115)
    turtle.pd()
    turtle.begin_fill() 
    turtle.fd(75)
    turtle.seth(-30)
    turtle.circle(450,10)
    turtle.seth(-90)
    turtle.fd(100)
    turtle.seth(-35)
    turtle.circle(50,70)
    turtle.seth(90)
    turtle.fd(95)
    turtle.seth(-12)
    turtle.circle(450,40)
    turtle.seth(110)
    turtle.fd(70)
    turtle.seth(213)
    turtle.circle(-345,64)
    turtle.pu()
    turtle.end_fill() 


def body():
    
    #配置画笔属性
    turtle.fillcolor("black") 
    turtle.color("black") 
    turtle.speed(1)
    turtle.colormode(255)

    #绘制母企鹅的身体
    turtle.pu()
    turtle.goto(-489,82)
    turtle.seth(-115)
    turtle.pd()
    turtle.begin_fill() 
    turtle.fd(175)
    turtle.circle(120,40)
    turtle.seth(30)
    turtle.circle(200,40)
    turtle.seth(-90)
    turtle.circle(174,180)
    turtle.seth(-70)
    turtle.circle(200,40)
    turtle.seth(70)
    turtle.circle(120,43)
    turtle.fd(175)
    turtle.seth(213)
    turtle.circle(-345,66)
    turtle.pu()
    turtle.end_fill()


def feet():

    #配置画笔属性
    turtle.fillcolor((250,175,8)) 
    turtle.color((250,175,8)) 
    turtle.speed(1)
    turtle.colormode(255)

    #绘制母企鹅的左脚
    turtle.pu()
    turtle.goto(-300,-228)
    turtle.seth(180)
    turtle.pd()
    turtle.begin_fill() 
    turtle.fd(200)
    turtle.circle(-10,120)
    turtle.circle(-120,40)
    turtle.pu()
    turtle.end_fill()

    #绘制母企鹅的右脚
    turtle.pu()
    turtle.goto(-300,-228)
    turtle.seth(0)
    turtle.pd()
    turtle.begin_fill() 
    turtle.fd(200)
    turtle.circle(10,120)
    turtle.circle(120,50)
    turtle.pu()
    turtle.end_fill()


def belly():

    #配置画笔属性
    turtle.fillcolor("white") 
    turtle.color("white") 
    turtle.speed(1)
    turtle.colormode(255)

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

    #画椭圆
    a=4.5
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.1
            turtle.lt(3) 
            turtle.fd(a) 
        else:
            a=a-0.1
            turtle.lt(3)
            turtle.fd(a)
    turtle.pu()
    turtle.end_fill()
    turtle.pu()
    turtle.end_fill()    

turtle.Turtle().screen.delay(0)

head();
bowknot();
lefteye();
righteye();
mouth();
feet();
body();
belly();
scarf();
turtle.hideturtle()
turtle.mainloop()
企鹅代码

小企鹅生成动画:

三、参考博文

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

posted @ 2019-05-29 17:28  小白的个人总结  阅读(18352)  评论(3编辑  收藏  举报