您好,欢迎来到意榕旅游网。
搜索
您的当前位置:首页Python用turtle实现用正方形画圆的例子

Python用turtle实现用正方形画圆的例子

来源:意榕旅游网
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() 当前位置

towards() 返回与指定点之间的⾓度 参数:(X,Y)⼀个位置xcor() 返回乌龟X坐标ycor() 返回乌龟Y坐标

heading() 返回当前乌龟的⽅向值

distance() 返回乌龟与坐标点之间的距离。参数:(X,Y)⼀个位置设置与测量

degrees() 设置整个圆的⾓度,最好不要动。参数:(integer or float)⼀个整数radians() 将⾓度测量单位设置为弧度。360度就是2π画笔控制绘画状态

pendown() | pd() | down() 将笔落下放在图上,移动的时候将会绘图penup() | pu() | up() 将笔提起来,移动的时候将不会绘图

pensize() | width() 设置线条的粗细。参数:(width)⼀个正数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() 颜⾊,直接使⽤返回当前笔颜⾊与填充颜⾊pencolor() 设置笔的颜⾊fillcolor() 设置笔的填充颜⾊

填充

filling() 返回填充状态,begin_fill() 在填充之前使⽤end_fill() 结束填充更多绘画控制

reset() 重置所有参数

clear() 删除绘画,与reset不同之处仅仅是删除图形,参数保留write() 写⽂字

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() 显⽰乌龟的形状hideturtle() | ht() 隐藏乌龟的形状

isvisible() 是否可见,返回True or False外表

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() 返回笔的属性。shearfactor() 设置或者返回当前剪切因⼦

settiltangle() 与tilt() ⼀样,只是可以为空,则返回当前旋转⾓度tiltangle() 弃⽤

tilt() 设置当前乌龟⾓度,不调整乌龟前进⽅向(仅仅改变乌龟样⼦)shapetransform() 设置或返回乌龟的形状的当前转换矩阵get_shapepoly() 返回当前形状的坐标监听动作

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() 开始记录多边形的顶点,当前点为起始点end_poly() 结束记录多边形的顶点,当前点为起始点get_poly() 返回最后记录的多边形clone() 复制⼀个⼀模⼀样的乌龟

getturtle() | getpen() 获取trutle对象本⾝getscreen() 获取画布对象

setundobuffer() 设置或禁⽤中断器

undobufferentries() 返回undobuffer中的条⽬数

画布的⽅法窗⼝控制

bgcolor() 设置或返回当前画布的背景颜⾊bgpic() 设置或返回当前画布的背景图⽚名称clear() | clearscreen() 清除图形reset() | resetscreen() 重置画布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 )⼀个数字tracer() 开启动画,设置延迟

n – nonnegative integer n个动作执⾏⼀次delay – nonnegative integer 延迟,毫秒

update() 更新画布,当tracer关闭时使⽤画布监听

listen() 开启监听,将⿏标定位到画布

onkey() | onkeyrelease() 键盘弹起(需要位于焦点上,使⽤上⾯listen后)fun – a function with no arguments or None 动作函数

key – a string: key (e.g. “a”) or key-symbol (e.g. “space”) 按键onkeypress() 键盘按下事件,同上onclick() | onscreenclick() ⿏标点击事件

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) 点击次数

add – True or False – if True, a new binding will be added, otherwise it will replace a former binding 是否是添加,还是替换ontimer() 计时器

fun – a function with no arguments ⽆需函数t – a number >= 0 事件间隔

mainloop() | done() 开始事件循环,必须是乌龟绘画中的最后⼀个函数设置与特殊⽅法

mode() 绘图模式,3选1 “standard”, “logo” or “world”colormode() 颜⾊模式, 1.0 或者 255

getcanvas() 返回当前TurtleScreen.的Canvasgetshapes() 返回当前可⽤形状

register_shape() | addshape() 3种调⽤⽅式。1.直接调⽤图⽚。screen.register_shape(“turtle.gif”)2.调⽤形状,制定点位置。

screen.register_shape(\"triangle\

3,调⽤形状,名字随便取

turtles() 返回乌龟list数组

window_height() 返回窗⼝⾼度window_width() 返回窗⼝宽度输⼊⽅法

textinput() ⽂字输⼊title – string 输⼊名字

prompt – string 输⼊的⽂本numinput() 数字输⼊

title – string 输⼊名字prompt – string 输⼊⽂本

default – number (optional) 默认minval – number (optional) 最⼩maxval – number (optional) 最⼤屏幕特有⽅法

bye() 关闭turtle窗⼝

exitonclick() ⿏标点击关闭窗⼝setup() 设置主窗⼝参数

width – if an integer, a size in pixels, if a float, a fraction of the screen; default is 50% of screen 宽 度height – if an integer, the height in pixels, if a float, a fraction of the screen; default is 75% of screen ⾼度

startx – if positive, starting position in pixels from the left edge of the screen, if negative from the right edge, if None, centerwindow horizontally 左边开始位置

startx – if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge, if None, centerwindow vertically 右边开始位置title() 设置绘画窗⼝标题

以上这篇Python ⽤turtle实现⽤正⽅形画圆的例⼦就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- yrrf.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务