当前位置: 首页 > 后端技术 > Python

turtle库

时间:2023-03-26 19:47:18 Python

turtle库是Python中非常流行的绘图函数库。它主要是根据坐标轴绘制图像。画笔是一只小乌龟。通过控制乌龟在坐标平面上的运动,可以绘制出各种图像。installturtlepipinstallturtlecanvasturtle中的canvas是绘画的区域,我们可以设置它的大小和初始位置。常用画布方法:screensize()方法语法:turtle.screensize(canvwidth=None,canvheight=None,bg=None)canvwidth--widecanvheight--highbg--backgroundcolor例子:importturtle,timeturtle.screensize(800,600,"green")turtle.screensize()#返回默认大小(400,300)time.sleep(3)setup()方法语法:turtle.setup(width=0.5,height=0.75,startx=None,starty=None)width,height--当输入的宽高为整数时,表示像素。为小数时,表示计算机屏幕的比例startx,starty——该坐标表示矩形窗口左上角的位置。如果为空,则窗口位于屏幕中央。例子:importturtle,timeturtle.screensize(800,600,"green")turtle.screensize()#返回默认大小(400,300)time.sleep(3)brush画笔有颜色和线宽等属性:turtle.pensize():设置画笔的宽度;turtle.pencolor():不传入参数,返回当前画笔颜色。传入参数设置画笔颜色,可以是“red”、“blue”或RGB三元组等字符串。turtle.speed():设置画笔的移动速度,画笔绘制的速度范围为整数[0,10],数字越大越快。绘图命令操作海龟绘图的命令有很多种,这些命令可以分为三种类型:画笔移动命令、画笔控制命令和全局控制命令。笔刷运动命令命令说明turtle.forward(distance)movedistancepixellengthtothecurrentbrushdirectionturtle.backward(distance)movedistancepixellengthtotheanti-directionturtle.right(degree)moveclockwisedegree°turtle.left(degree)逆时针移动度数°turtle.pendown()移动时绘制图形,默认也是绘制turtle.goto(x,y)移动画笔到坐标为x,y的位置绘制图形,提笔,并用它来画另一个地方。绘制时用turtle.speed(speed)画笔绘制的速度范围[0,10]整数。在画笔控制命令的左边(右边)画一个圆命令说明turtle.pensize(width)绘制图形时的宽度turtle.pencolor()画笔颜色turtle.fillcolor(colorstring)绘制图形turtle时的填充颜色.color(color1,color2)同时设置pencolor=color1,fillcolor=color2turtle.filling()返回当前是否处于填充状态turtle.begin_fill()准备开始填充图形turtle.end_fill()填充完成turtle.hideturtle()隐藏箭头显示;turtle.showturtle()和hideturtle()函数对应全局控制命令命令说明turtle.clear()清除海龟窗口,但是海龟的位置和状态不会改变turtle.reset()清除窗口并重置海龟状态到初始状态turtle.undo()撤销它海龟动作turtle.isvisible()返回当前海龟是否可见stamp()复制当前图形turtle.write(s[,font=("font-name",font_size,"font_type")])写入文字,s为文字内容,font为字体参数,分别为字体名称、字号、字型;字体是可选的,字体参数也是可选的。练习画一个五角星importturtleimporttimeturtle。setup(1500,1400,0,0)turtle.pensize(5)turtle.pencolor("pink")turtle.fillcolor("red")turtle.begin_fill()for_inrange(5):turtle.forward(400)turtle.right(144)turtle.end_fill()time.sleep(5)之前看到一个例子,可以画出美丽的樱花树。如果你有兴趣,你可以看看。链接:https://blog.csdn.net/z564359805/article/details/85861481#commentBox具体代码如下:#!/usr/bin/envpython#coding=utf-8#画樱花importturtleimportrandomfromturtleimport*fromtimeimportsleep#drawcherryblossomtorso(60,t)deftree(branchLen,t):sleep(0.0005)如果branchLen>3:如果8<=branchLen<=12:如果random.randint(0,2)==0:t.color('snow')#whiteelse:t.color('lightcoral')#浅珊瑚色t.pensize(branchLen/3)elifbranchLen<8:ifrandom.randint(0,1)==0:t.color('snow')else:t.color('lightcoral')#浅珊瑚t.pensize(branchLen/2)else:t.color('sienna')#ocher(zhě)颜色t.pensize(branchLen/10)#6t.forward(branchLen)a=1.5*random.random()t.right(20*a)b=1.5*random.random()tree(branchLen-10*b,t)t.left(40*a)tree(branchLen-10*b,t)t.right(20*a)t.up()t.backward(branchLen)t.down()#droppedPetaldef花瓣(m,t):对于范围(m)中的i: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)defmain():#绘图区域t=turtle.Turtle()#canvassizew=turtle.Screen()t.hideturtle()#hidebrushgetscreen().tracer(5,0)w.screensize(bg='wheat')#小麦wheatt.left(90)t.up()t.backward(150)t.down()t.color('sienna')#画樱花树干树(60,t)#飘落的花瓣petal(200,t)w.exitonclick()main()参考:https://www.9xkd.com/