Python 用turtle实现用正方形画圆的例子
最近发现一个很有意思的画图的python库,叫做turtle,这里先说下用turtle这个库来实现用正方形画圆的思路。 每次都用乌龟(turtle) 来画出一个正方形,然后通过旋转3°后,继续画一样的正方形,在通过120次循环后就实现了完整的圆,这里当然也可以用其他的角度和次数,只要能完成360度就可以了。 先看完成的图形和代码。 代码如下: import turtle window = turtle.Screen() #设置好画图的基本参数 window.bgcolor(“blue”) wugui= turtle.Turtle() wugui.shape(“turtle”) wugui.color(“red”) wugui.speed(5) for i in range(120): #这里设定正方形的个数 wugui.forward(100) wuguiright(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(90) wugui.forward(100) wugui.right(93)#这里决定每次旋转角度,也就决定了需要画正方形的次数。 window.exitonclick() 代码应该很简单易懂,就不再说了。turtle真的是非常强大的一个绘图工具,可以绘制各种各样有趣的图形,详情请看 turtle官方文档,这里说点基本的参数与用法吧。主要包括两部分,乌龟与画布。 乌龟方法 乌龟运动 乌龟移动与绘画 forward() | fd() 向前移动指定的距离。参数:(integer or float))一个数字 backward() | bk() | back() 向后移动指定的距离。参数:(integer or float))一个数字 right() | rt() left() | lt() 向右 旋转指定的角度。参数:(integer or float))一个数字 goto() | setpos() | setposition() 去到位置(x,y)。参数:(x,y=None))一个数字 setx() 设置X位置。参数:(integer or float)一个数字 sety() 设置Y位置。参数:(integer or float)一个数字 setheading() | seth() 方向设置为to_angle.就是东西南北方向,上北下南左西右东 home() 移动到原点 – 坐标(0,0):并将其标题设置为其起始方向 circle() 绘制一个给定半径的圆。参数:(radius,extent,steps)(一个数字__半径,如果值为正则逆时针,负数为顺时针__,一个数字,执行的步数) dot() 用颜色画出一个直径大小的圆点。参数:(size,color)(一个大于1的整数_可None,颜色值) stamp() 将当前位置上的形状复制到画布上,返回stamp_id.可通过下方的clearstamp删除 clearstamp() 删除stamp()返回来的值,参数:(stamp_id)stamp函数返回值 clearstamps() 删除所有的stamp,默认无参数,删除所有 undo() 撤销上一步动作 speed() 乌龟爬行速度,我们这设置的是5,不设置为最快,直接生成 乌龟当前状态 position() | pos() 当前位置 设置与测量 degrees() 设置整个圆的角度,最好不要动。参数:(integer or float)一个整数 画笔控制 绘画状态 pendown() | pd() | down() 将笔落下放在图上,移动的时候将会绘图 pen() 使用键值对设置笔的属性 “shown”: True/False 显示 “pendown”: True/False 笔落下 “pencolor”: color-string or color-tuple 笔的颜色 “fillcolor”: color-string or color-tuple 填充颜色 “pensize”: positive number 笔大小(正整数) “speed”: number in range 0..10 绘画速度(范围0-10) “resizemode”: “auto” or “user” or “noresize” 大小调整模式 “stretchfactor”: (positive number,positive number) 拉伸参数 “outline”: positive number 外部 “tilt”: number 倾斜 isdown() 如果笔停止返回True,反之返回False 颜色控制 color() 颜色,直接使用返回当前笔颜色与填充颜色 填充 filling() 返回填充状态, 更多绘画控制 reset() 重置所有参数 arg – object to be written to the TurtleScreen 写到TurtleScreen的参数 move – True/False 移动 align – one of the strings “left”,“center” or right” 对齐参数3选1(left,right,center) font – a triple (fontname,fontsize,fonttype) 字体 乌龟状态 可视性 showturtle() | st() 显示乌龟的形状 外表 shape() 设置乌龟的图形形状,可选( “arrow”,“turtle”,“circle”,“square”,“triangle”,“classic”) resizemode() 大小调整模式 “auto”: adapts the appearance of the turtle corresponding to the value of pensize. 由画笔大小决定(自动) “user”: adapts the appearance of the turtle according to the values of stretchfactor and outlinewidth (outline),由拉伸参数决定 “noresize”: no adaption of the turtle's appearance takes place. 不调整 shapesize() | turtlesize() 返回笔的属性。 监听动作 onclick() 鼠标点击事件 fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas函数需要有两个参数 num – number of the mouse-button,defaults to 1 (left mouse button) 单击次数,默认1 add – True or False – if True,a new binding will be added,otherwise it will replace a former binding 添加新的绑定函数,否则替代之前函数 例子:def turn(x,y): 。。。 left(180) onclick(turn) onrelease() 鼠标释放事件,同上 ondrag() 鼠标移动事件,同上 乌龟一些特殊方法 begin_poly() 开始记录多边形的顶点,当前点为起始点 画布的方法 窗口控制 bgcolor() 设置或返回当前画布的背景颜色 screensize() 画布大小 canvwidth – positive integer,new width of canvas in pixels 宽度 canvheight – positive integer,new height of canvas in pixels 高度 bg – colorstring or color-tuple,new background color 颜色 setworldcoordinates() 全局坐标 llx – a number,x-coordinate of lower left corner of canvas 左下X坐标 lly – a number,y-coordinate of lower left corner of canvas 左下X坐标 urx – a number,x-coordinate of upper right corner of canvas 右下X坐标 ury – a number,y-coordinate of upper right corner of canvas 右下X坐标 动画控制 delay() 动画延迟(毫秒)参数:(integer )一个数字 n – nonnegative integer n个动作执行一次 delay – nonnegative integer 延迟,毫秒 update() 更新画布,当tracer关闭时使用 画布监听 listen() 开启监听,将鼠标定位到画布 fun – a function with no arguments or None 动作函数 onclick() | onscreenclick() 鼠标点击事件 fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas 函数需要两个参数 fun – a function with no arguments 无需函数 设置与特殊方法 mode() 绘图模式,3选1 “standard”,“logo” or “world” 1.直接调用图片。screen.register_shape(“turtle.gif”) 2.调用形状,制定点位置。 screen.register_shape("triangle",((5,-3),(0,5),(-5,-3))) 3,调用形状,名字随便取 turtles() 返回乌龟list数组 输入方法 textinput() 文字输入 title – string 输入名字 title – string 输入名字 屏幕特有方法 bye() 关闭turtle窗口 width – if an integer,a size in pixels,if a float,a fraction of the screen; default is 50% of screen 宽 度 以上这篇Python 用turtle实现用正方形画圆的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |