turtle 海龟图形

turtle数据库是python语言中最流行的绘制函数图形的数据库,绘制笔头像个小海龟,因此一般称为 海龟图形。海龟数据库的导入   import turtle

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

1 画布大小设置

turtle.screensize(canvwidth, canvheight, 'bg背景颜色')

turtle.setup(width,height)   width and height 为整表示像素,为小数表示占据屏幕比例

2 画笔

2.1画笔属性

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

turtle.pencolor() 设置画笔颜色

turtle.speed() 画笔的速度  [0,10]  逐渐增大

2.2 画笔的移动命令

import turtle as tl

tl.fd()  向前移动距离

tl.bd()  向后移动距离

tl.right()  顺时针旋转角度

tl.left()    逆时针旋转角度

tl.goto(x,y)  将画笔移动至(x,y) 处

tl.penup()  提起画笔   tl.pendown() 放下画笔   两者一般配套使用

tl.circle(半径,角度)  绘制圆弧,其中半径为正值,表示逆时针画

tl.dot(半径,‘颜色’)  指定一个点的大小和颜色

2.3 画笔的控制命令

tl.fillcolor(‘颜色’)  绘制图形的填充颜色

tl.color('pencolor','fillcolor') 同时设置两种颜色

tl.filling()   返回当前是否处于填充状态

tl.begin_fill()  开始填充

tl.end_fill() 停止填充

tl.hideturtle() and tl.showturtle()  隐藏和显示海龟箭头

2.4 全局控制命令

tl.clear()   清空turtle窗口

tl.reset() 重新设置turtle窗口

tl.undo() 撤销

tl.isvisible()  turtle图像可见

tl.write('名称’,font=('字体',‘大小’,‘类型’))

tl.mainloop()  tl.done()   循环

tl.delay( 数字)   绘制延迟毫秒数

3 实例

3.1五角星

import turtle as tl
tl.pensize(10)
tl.color('red','yellow')

tl.begin_fill()
for i in range(5):
    tl.fd(200)
    tl.left(144)
    tl.fd(200)
    tl.right(72)
tl.end_fill()

tl.penup()
tl.goto(-155,-255)
tl.color("violet")
tl.hideturtle()
tl.write("pentagram",font=('newtimes','35','normal'))

tl.done()

 

 

 

 

 

开个小灶——turtle 海龟图形 Python 第1张

3.2  螺旋线

import turtle as tl
import time
tl.pensize(2)
tl.bgcolor('black')
colors =[ 'yellow','red','green','purple',]
tl.tracer(False)
for i in range(400):
    tl.fd(i*2)
    tl.color(colors[i% 4])
    tl.left(91)
tl.tracer(True)

time.sleep(5)

开个小灶——turtle 海龟图形 Python 第2张

turtle.tracer(False)    turtle.tracer(True)  直接将绘制结果显示,略去中间绘制过程。

 

 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄