python中的画图神器——turtle模块使用

Turtle库是Python语言中一个简单流行的绘图函数库,使用非常方便,直接导入import trutle即可

 

turtle库的基础命令介绍

1、画布

画布cancas是绘图区域,可以设置它的大小和初始位置

turtle.screensize(1000,600,'red')    大小的设置
turtle.setup(width=0.5,height=0.75)  初始位置

2、画笔

(1)画笔运动的命令
turtle.forward(a)   向当前画笔方向移动a像素长度
turtle.backward(a)  向当前画笔相反方向移动a像素长度
turtle.right(a)     顺时针移动
aturtle.left(a)     逆时针移动
aturtle.pendown()   移动时绘制图形
turtle.goto(x,y)    将画笔移动到坐标为x,y的位置
turtle.penup()      移动时不绘制图形,提起笔
turtle.speed(a)     画笔绘制的速度范围
turtle.circle()     画图,半径为正,表示圆心在画笔的左边画圈
(2)画笔控制命令
turtle.pensize(width)   绘制图形的宽度
turtle.pencolor()       画笔的颜色
turtle.fillcolor(a)     绘制图形的填充颜色
turtle.color(a1,a2)     同时设置pencolor=a1,fillcolor=a2
turtle.filling()        返回当前是否在填充状态
turtle.begin_fill()     准备开始填充图形
turtle.end_fill()       填充完成
turtle.hideturtle()     隐藏箭头显示
turtle.showturtle()     显示箭头
(3)全局控制命令
turtle.clear()   清空turtle窗口,但是turtle的位置和状态不会改变
turtle.reset()   清空窗口,重置turtle状态为起始位置
turtle.undo()    撤销上一个turtle动作

turtle绘制图形举例

1、樱花树1的绘制

import turtle as T
import random
import time


# 绘制樱花
def Tree(branch,t):
    time.sleep(0.01)
    if branch>3:
        if 8<=branch<=12:
            if random.randint(0,2)==0:
                # 上色
                t.color('snow')
            else:
                # 上色
                t.color('lightcoral')
            t.pensize(branch/3)
        elif branch<8:
            if random.randint(0,1)==0:
                t.color('snow')
            else:
                t.color('lightcoral')
            t.pensize(branch/2)
        else:
            t.color('sienna')
            t.pensize(branch/2)
        t.forward(branch)
        a=1.5*random.random()
        t.right(20*a)
        b=1.5*random.random()
        Tree(branch-10*b,t)
        t.left(40*a)
        Tree(branch-10*b,t)
        t.right(20*a)
        t.up()
        t.backward(branch)
        t.down()


# 绘制花瓣
def Petal(m,t):
    for i in range(m):
        a=200-400*random.random()
        b=10-20*random.random()
        t.up()
        t.forward(b)
        t.left(90)
        t.forward(a)
        t.down()
        t.color('lightcoral')
        t.circle(1)
        t.up()
        t.backward(a)
        t.right(90)
        t.backward(b)


t=T.Turtle()
w=T.Screen()
t.hideturtle()  # 隐藏turtle
t.getscreen().tracer(5,0)
w.screensize(bg='wheat')
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')

Tree(60,t)
Petal(200,t)
w.exitonclick()
 

2、樱花树2的绘制

from turtle import *
from random import *
from math import *

def tree(n, l):
    pd() # 下笔
    # 阴影效果
    t = cos(radians(heading() + 45)) / 8 + 0.25
    pencolor(t, t, t)
    pensize(n / 3)
    forward(l) # 画树枝
    if n > 0:
        b = random() * 15 + 10 # 右分支偏转角度
        c = random() * 15 + 10 # 左分支偏转角度
        d = l * (random() * 0.25 + 0.7) # 下一个分支的长度
        # 右转一定角度,画右分支
        right(b)
        tree(n - 1, d)
        # 左转一定角度,画左分支
        left(b + c)
        tree(n - 1, d)

        # 转回来
        right(c)
    else:
        # 画叶子
        right(90)
        n = cos(radians(heading() - 45)) / 4 + 0.5
        pencolor(n, n*0.8, n*0.8)
        circle(3)
        left(90)

        # 添加0.3倍的飘落叶子
        if(random() > 0.7):
            pu()
            # 飘落
            t = heading()
            an = -40 + random()*40
            setheading(an)
            dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
            forward(dis)
            setheading(t)


            # 画叶子
            pd()
            right(90)
            n = cos(radians(heading() - 45)) / 4 + 0.5
            pencolor(n*0.5+0.5, 0.4+n*0.4, 0.4+n*0.4)
            circle(2)
            left(90)
            pu()

            #返回
            t = heading()
            setheading(an)
            backward(dis)
            setheading(t)

    pu()
    backward(l)# 退回
bgcolor(0.5, 0.5, 0.5) # 背景色
ht() # 隐藏turtle
speed(0) # 速度,1-10渐进,0最快
tracer(0, 0)
pu() # 抬笔
backward(100)
left(90) # 左转90度
pu() # 抬笔
backward(300) # 后退300
tree(12, 100) # 递归7层
done()

3、樱花树3的绘制

from turtle import *
from random import *
from math import *

def tree(n,l):
    pd()
    t=cos(radians(heading()+45))/8+0.25
    pencolor(t,t,t)
    pensize(n/4)
    forward(l)
    if n>0:
        b=random()*15+10
        c=random()*15+10
        d=l*(random()*0.35+0.6)
        right(b)
        tree(n-1,d)
        left(b+c)
        tree(n-1,d)
        right(c)
    else:
        right(90)
        n=cos(radians(heading()-45))/4+0.5
        pencolor(n,n,n)
        circle(2)
        left(90)
    pu()
    backward(l)
bgcolor(0.5,0.5,0.5)
ht()
speed(0)
tracer(0,0)
left(90)
pu()
backward(300)
tree(13,100)
done()

4、螺旋圈的绘制

import turtle
import time
turtle.pensize(2)
turtle.bgcolor("black")
colors = ["red","yellow",'purple','blue']
turtle.tracer(False)
for x in range (400):
    turtle.forward(2*x)
    turtle.color(colors[x % 4])
    turtle.left(91)
turtle.tracer(True)
turtle.done()

5、玫瑰花的绘制

from turtle import *
import time

setup(800,600,0,0)
speed(0)
penup()
seth(90)
fd(340)
seth(0)
pendown()

speed(5)
begin_fill()
fillcolor('red')
circle(50,30)

for i in range(10):
    fd(1)
    left(10)

circle(40,40)

for i in range(6):
    fd(1)
    left(3)

circle(80,40)

for i in range(20):
    fd(0.5)
    left(5)

circle(80,45)

for i in range(10):
    fd(2)
    left(1)

circle(80,25)

for i in range(20):
    fd(1)
    left(4)

circle(50,50)

time.sleep(0.1)

circle(120,55)

speed(0)

seth(-90)
fd(70)

right(150)
fd(20)

left(140)
circle(140,90)

left(30)
circle(160,100)

left(130)
fd(25)

penup()
right(150)
circle(40,80)
pendown()

left(115)
fd(60)

penup()
left(180)
fd(60)
pendown()

end_fill()

right(120)
circle(-50,50)
circle(-20,90)

speed(1)
fd(75)

speed(0)
circle(90,110)

penup()
left(162)
fd(185)
left(170)
pendown()
circle(200,10)
circle(100,40)
circle(-52,115)
left(20)
circle(100,20)
circle(300,20)
speed(1)
fd(250)

penup()
speed(0)
left(180)
fd(250)
circle(-300,7)
right(80)
circle(200,5)
pendown()

left(60)
begin_fill()
fillcolor('green')
circle(-80,100)
right(90)
fd(10)
left(20)
circle(-63,127)
end_fill()

penup()
left(50)
fd(20)
left(180)

pendown()
circle(200,25)

penup()
right(150)

fd(180)

right(40)
pendown()
begin_fill()
fillcolor('green')
circle(-100,80)
right(150)
fd(10)
left(60)
circle(-80,98)
end_fill()

penup()
left(60)
fd(13)
left(180)

pendown()
speed(1)
circle(-200,23)



exitonclick()

 

欢迎喜欢数据分析和python工具的小伙伴关注我的微信公众号——DT学说。

 

  • 3
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值