海龟绘图体系
窗体:
turtle.setup(宽,高,start_x,start_y) # 开启绘图窗口,后两个值不设默认在中心,如果设为0的话则在左上角
turtle.screensize(宽,高,bg=None) # 里面的画布,bg为背景颜色
#两条命令都不设置的话,执行后面的代码时会出现一个默认大小的窗体
绘图:
- #绝对坐标(窗体中心为0,0点)
turtle.goto(x,y) #直接到达某一个点,需要两个坐标,不改变海龟的朝向
turtle.seth(angle) #angle为绝对角度
- #相对坐标
turtle.fd(z) # forward,以海龟自身位置为坐标原点,以朝向为正,前进或后退,z负=后退,但海龟朝向还是为正方向
turtle.bk(z) # back,向后走,与前一个相反,z正=后退,z负=前进,朝向不变
turtle.left(A) #左转A度
turtle.right(A) #右转A度
turtle.circle(r,angle) #画圆弧,r为半径,左为圆心,angle为角度,不给angle的话,画360度
其他命令:
- 颜色
turtle.pencolor('red')#画笔颜色,字符串 ‘yello,red....’ OR:r,g,b
turtle.bgcolor('red')#背景颜色
turtle.begin_fill()
turtle.fill_color('red') #封闭区间,填充颜色
turtle.end_fill()
turtle.colormode() #1.0 RGB小数模式,255 RGB整数模式
- 画笔
turtle.pendown()=turtle.pd() #画笔落
turtle.penup()=turtle.pu() #画笔起
turtle.pensize(3) #画笔宽
turtle.speed(22)#绘画速度
turtle.hideturtle()
turtle.showturtle() #隐藏或显示画笔形状
turtle.write( s , font=(a,b,c) ) #写字,a为字体名称,b为大小,c为类型 例:'Arial',30,'normal'
turtle.done()# 完成绘制后,窗口不关闭
例子:
#风车
import turtle as t
t.pensize(5)
t.pencolor('cyan')
t.seth(90)
t.speed(10)
for i in range(4):
t.fd(150)
t.right(90)
t.circle(-150,45)
t.right(90)
t.fd(150)
t.left(3*45)
t.hideturtle()
t.done()