python怎么实现turtle海龟绘图(python,turtle,开发技术)

时间:2024-05-09 10:00:43 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

一、安装

  • 写出许多有趣的可视化东西

  • 也可以画出很多奇妙的图案

pipinstallturtule

二、画布

  • 画布就是turtle为我们展开用于绘图区域

  • 我们可以设置它的大小和初始位置

importturtle#返回默认大小(400,300)turtle.screensize()#设置画布方法一,设置宽、高、背景色turtle.screensize(800,600,"green")#设置画布方法二,宽高为小数时候为占据电脑屏幕比例,宽高为整数时候为像素turtle.setup(width=0.6,height=0.6)#startx,starty表示矩形窗口左上角顶点的位置,如果为空,则窗口位于屏幕中心turtle.setup(width=800,height=800,startx=100,starty=100)

三、画笔

  • 可以设置画笔的属性,颜色、画线的宽度等

importturtle#设置画笔的宽度turtle.pensize()#没有参数传入,返回当前画笔颜色,传入参数设置画笔颜色turtle.pencolor()#设置画笔移动速度,画笔绘制的速度范围[0,10]整数,数字越大越快turtle.speed(speed)

四、绘图命令

importturtle#向当前画笔方向移动distance像素长turtle.forward(distance)#向当前画笔相反方向移动distance像素长度turtle.backward(distance)#顺时针移动degree°turtle.right(degree)#逆时针移动degree°turtle.left(degree)#移动时绘制图形,缺省时也为绘制turtle.pendown()#将画笔移动到坐标为x,y的位置turtle.goto(x,y)#移动时不绘制图形,提起笔,用于另起一个地方绘制时用turtle.penup()#画笔绘制的速度范围[0,10]整数turtle.speed(speed)#画圆,半径为正(负),表示圆心在画笔的左边(右边)画圆turtle.circle()

五、画笔控制命令

importturtle#绘制图形时的宽度turtle.pensize(width)#画笔颜色turtle.pencolor()#绘制图形的填充颜色turtle.fillcolor(colorstring)#同时设置pencolor=color1,fillcolor=color2turtle.color(color1,color2)#返回当前是否在填充状态turtle.filling()#准备开始填充图形turtle.begin_fill()#填充完成turtle.end_fill()#隐藏箭头显示turtle.hideturtle()#与hideturtle()函数对应turtle.showturtle()

六、全局控制命令

importturtle#清空turtle窗口,但是turtle的位置和状态不会改变turtle.clear()#清空窗口,重置turtle状态为起始状态turtle.reset()#撤销上一个turtle动作turtle.undo()#返回当前turtle是否可见turtle.isvisible()#复制当前图形stamp()#写文本,s为文本内容,font是字体的参数,里面分别为字体名称,大小和类型turtle.write(s[,font=("font-name",font_size,"font_type")])

七、绘制方形螺旋

fromturtleimport*foriinrange(500):forward(i)left(91)

python怎么实现turtle海龟绘图

八、绘制彩色螺旋

fromturtleimport*colors=['red','purple','blue','green','yellow','orange']forxinrange(360):pencolor(colors[x%6])width(x/100+1)forward(x)left(59)

python怎么实现turtle海龟绘图

九、绘制太阳花

importturtleastimporttimet.color("red","yellow")t.speed(10)t.begin_fill()for_inrange(50):t.forward(200)t.left(170)end_fill()time.sleep(1)

python怎么实现turtle海龟绘图

十、绘制小蟒蛇

importturtledefdrawSnake(rad,angle,len,neckrad):for_inrange(len):turtle.circle(rad,angle)turtle.circle(-rad,angle)turtle.circle(rad,angle/2)turtle.forward(rad/2)#直线前进turtle.circle(neckrad,180)turtle.forward(rad/4)if__name__=="__main__":turtle.setup(1500,1400,0,0)turtle.pensize(30)#画笔尺寸turtle.pencolor("green")turtle.seth(-40)#前进的方向drawSnake(70,80,2,15)

python怎么实现turtle海龟绘图

十一、绘制五角星

importturtleimporttimeturtle.pensize(5)turtle.pencolor("yellow")turtle.fillcolor("red")turtle.begin_fill()for_inrange(5):turtle.forward(200)turtle.right(144)turtle.end_fill()time.sleep(2)turtle.penup()turtle.goto(-150,-120)turtle.color("violet")turtle.write("Done",font=('Arial',40,'normal'))time.sleep(1)

python怎么实现turtle海龟绘图

十二、绘制小猪佩奇

fromturtleimport*#绘制鼻子defnose(x,y):pu()goto(x,y)pd()seth(-30)begin_fill()a=0.4foriinrange(120):if0<=i<30or60<=i<90:a=a+0.08lt(3)#向左转3度fd(a)#向前走a的步长else:a=a-0.08lt(3)fd(a)end_fill()pu()seth(90)fd(25)seth(0)fd(10)pd()pencolor(255,155,192)seth(10)begin_fill()circle(5)color(160,82,45)end_fill()pu()seth(0)fd(20)pd()pencolor(255,155,192)seth(10)begin_fill()circle(5)color(160,82,45)end_fill()#绘制头部defhead(x,y):color((255,155,192),"pink")pu()goto(x,y)seth(0)pd()begin_fill()seth(180)circle(300,-30)circle(100,-60)circle(80,-100)circle(150,-20)circle(60,-95)seth(161)circle(-300,15)pu()goto(-100,100)pd()seth(-30)a=0.4foriinrange(60):if0<=i<30or60<=i<90:a=a+0.08lt(3)#向左转3度fd(a)#向前走a的步长else:a=a-0.08lt(3)fd(a)end_fill()#绘制耳朵defears(x,y):color((255,155,192),"pink")pu()goto(x,y)pd()begin_fill()seth(100)circle(-50,50)circle(-10,120)circle(-50,54)end_fill()pu()seth(90)fd(-12)seth(0)fd(30)pd()begin_fill()seth(100)circle(-50,50)circle(-10,120)circle(-50,56)end_fill()#绘制眼睛defeyes(x,y):color((255,155,192),"white")pu()seth(90)fd(-20)seth(0)fd(-95)pd()begin_fill()circle(15)end_fill()color("black")pu()seth(90)fd(12)seth(0)fd(-3)pd()begin_fill()circle(3)end_fill()color((255,155,192),"white")pu()seth(90)fd(-25)seth(0)fd(40)pd()begin_fill()circle(15)end_fill()color("black")pu()seth(90)fd(12)seth(0)fd(-3)pd()begin_fill()circle(3)end_fill()#绘制腮defcheek(x,y):color((255,155,192))pu()goto(x,y)pd()seth(0)begin_fill()circle(30)end_fill()#绘制嘴巴defmouth(x,y):color(239,69,19)pu()goto(x,y)pd()seth(-80)circle(30,40)circle(40,80)#绘制身体defbody(x,y):color("red",(255,99,71))pu()goto(x,y)pd()begin_fill()seth(-130)circle(100,10)circle(300,30)seth(0)fd(230)seth(90)circle(300,30)circle(100,3)color((255,155,192),(255,100,100))seth(-135)circle(-80,63)circle(-150,24)end_fill()#绘制手defhands(x,y):color((255,155,192))pu()goto(x,y)pd()seth(-160)circle(300,15)pu()seth(90)fd(15)seth(0)fd(0)pd()seth(-10)circle(-20,90)pu()seth(90)fd(30)seth(0)fd(237)pd()seth(-20)circle(-300,15)pu()seth(90)fd(20)seth(0)fd(0)pd()seth(-170)circle(20,90)#绘制脚deffoot(x,y):pensize(10)color((240,128,128))pu()goto(x,y)pd()seth(-90)fd(40)seth(-180)color("black")pensize(15)fd(20)pensize(10)color((240,128,128))pu()seth(90)fd(40)seth(0)fd(90)pd()seth(-90)fd(40)seth(-180)color("black")pensize(15)fd(20)#绘制尾巴deftail(x,y):pensize(4)color((255,155,192))pu()goto(x,y)pd()seth(0)circle(70,20)circle(10,330)circle(70,30)#参数设置defsetting():pensize(4)hideturtle()colormode(255)color((255,155,192),"pink")setup(840,500)speed(10)if__name__=="__main__":setting()#画布、画笔设置nose(-100,100)#鼻子head(-69,167)#头ears(0,160)#耳朵eyes(0,140)#眼睛cheek(80,10)#腮mouth(-20,30)#嘴body(-32,-8)#身体hands(-56,-45)#手foot(2,-177)#脚tail(148,-155)#尾巴done()#结束
 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:python怎么实现turtle海龟绘图的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python的blinker信号库怎么创建下一篇:

12 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18