当前位置: 移动技术网 > IT编程>脚本编程>Python > 开个小灶——turtle 海龟图形

开个小灶——turtle 海龟图形

2019年02月27日  | 移动技术网IT编程  | 我要评论

山东南山职业技术学院,汤婆子是什么,jlu

turtle 海龟图形

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

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()

 

 

 

 

 

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.tracer(false)    turtle.tracer(true)  直接将绘制结果显示,略去中间绘制过程。

 

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网