pythonturtle库手册

编程入门 行业动态 更新时间:2024-10-09 02:27:20

pythonturtle库<a href=https://www.elefans.com/category/jswz/34/1769120.html style=手册"/>

pythonturtle库手册

1.介绍

Turtle graphics是向孩子们介绍编程的一种流行方式。它是Wally Feurzig和Seymour Papert于1966年开发的原始Logo编程语言的一部分。

想象一下robotic turtle从x-y平面的(0,0)开始。 在导入乌龟后,给它命令turtle.forward(15),它在它面对的方向上移动(在屏幕上)15个像素,在它移动时画一条线。 给它命令turtle.right(25),它顺时针旋转25度。

通过将这些和类似命令组合在一起,可以轻松绘制复杂的形状和图片。

该turtle模块是从Python标准分发版到Python 2.5版的同名模块的扩展重新实现。

它试图保持旧乌龟模块的优点,并且(几乎)100%兼容它。这首先意味着学习程序员可以交互式地使用所有命令,类和方法-n。

turtle模块以面向对象和面向过程的方式提供turtle图形基元。 由于它使用Tkinter作为底层图形,因此需要安装Tk支持的Python版本。

面向对象的接口基本上使用两个+两个类:

TurtleScreen类将绘图窗口定义为绘图turtle的操场。 它的构造函数需要一个Tkinter.Canvas或一个ScrolledCanvas作为参数。 当应用程序使用turtle时应该使用它。

函数Screen()返回一个TurtleScreen子类的单例对象。 当turtle被用作独立的图形工具时,应该使用这个函数。 作为一个单例对象,从它的类继承是不可能的。

TurtleScreen / Screen的所有方法也作为函数存在,即作为面向过程的接口的一部分。

RawTurtle(别名:)RawPen定义了绘制的Turtle对象TurtleScreen。它的构造函数需要Canvas,ScrolledCanvas或TurtleScreen作为参数,所以RawTurtle对象知道在哪里绘制。

派生自RawTurtle的是子类Turtle(别名:),该子类使用自动创建Pen的“ Screen实例” (如果尚未存在)。

RawTurtle / Turtle的所有方法也作为函数存在,即面向过程的接口的一部分。

过程接口提供了从类Screen和方法派生的函数Turtle。它们与相应的方法具有相同的名称。每当从Screen方法派生的函数被调用时,屏幕对象就会自动创建。无论何时调用Turtle方法派生的任何函数,都会自动创建一个(未命名的)turtle对象。

要使用多个turtle,必须使用面向对象的界面。

注意

在以下文档中给出了函数的参数列表。当然,方法有另外的第一个参数self,在这里省略。

2.概述了可用的turtle和屏幕方法

2.1 turtle方法

Turtle motionMove and drawTell Turtle's stateSetting and measurementPen control绘图状态颜色控制填充更多绘图控制驼背状态可见性外观使用事件特殊的turtle法

2.2 TurtleScreen / Screen的方法

窗口控制动画控制使用屏幕事件设置和特殊方法特定于屏幕的方法

3. RawTurtle / Turtle的方法和相应的功能

本节中的大部分示例都涉及一个叫做Turtle的实例。

3.1 turtle议案

turtle.forward(distance)turtle.fd(distance)

参数:距离 - 一个数字(整数或浮点数)

turtle.back(distance)turtle.bk(distance)turtle.backward(distance)

参数:距离 - 一个数字

turtle.right(angle)turtle.rt(angle)

参数:角度 - 一个数字(整数或浮点数)

turtle.left(angle)turtle.lt(angle)

参数:角度 - 一个数字(整数或浮点数)

turtle.goto(x, y=None)turtle.setpos(x, y=None)turtle.setposition(x, y=None)

参数:x - 一个数字或一对数字y的数字 - 一个数字或无

x - 一个数字或一对数字

y - 一个数字或None

If _y_ is `None`, _x_ must be a pair of coordinates or a [`Vec2D`](about:blank#turtle.Vec2D) (e.g. as returned by [`pos()`](about:blank#turtle.pos)).

将turtle移到绝对位置。如果笔落下,画线。不要改变turtle的方向。

tp = turtle.pos() >>> tp (0.00,0.00) >>> turtle.setpos(60,30) >>> turtle.pos() (60.00,30.00) >>> turtle.setpos((20,80)) >>> turtle.pos() (20.00,80.00) >>> turtle.setpos(tp) >>> turtle.pos() (0.00,0.00)

turtle.setx(x)

参数:x - 一个数字(整数或浮点数)

turtle.sety(y)

参数:y - 一个数字(整数或浮点数)

turtle.setheading(to_angle)turtle.seth(to_angle)

参数:to_angle - 数字(整数或浮点数)

turtle.home()

将turtle移动到原点 - 坐标(0,0) - 并将其标题设置为其起始方向(取决于模式,请参阅参考资料mode())。

>>> turtle.heading()

90.0

>>> turtle.position()

(0.00,-10.00)

>>> turtle.home()

>>> turtle.position()

(0.00,0.00)

>>> turtle.heading()

0.0

turtle.circle(radius, extent=None, steps=None)

参数:半径 - 数字范围 - 数字(或无)步骤 - 整数(或无)

半径 - 一个数字

程度 - 一个数字(或None)

步骤 - 整数(或None)

Draw a circle with given _radius_. The center is _radius_ units left of the turtle; _extent_ – an angle – determines which part of the circle is drawn. If _extent_ is not given, draw the entire circle. If _extent_ is not a full circle, one endpoint of the arc is the current pen position. Draw the arc in counterclockwise direction if _radius_ is positive, otherwise in clockwise direction. Finally the direction of the turtle is changed by the amount of _extent_.

由于圆是用刻有正多边形的近似值,所以步数决定了要使用的步数。如果没有给出,它会自动计算。可用于绘制正多边形。

turtle.home() >>> turtle.position() (0.00,0.00) >>> turtle.heading() 0.0 >>> turtle.circle(50) >>> turtle.position() (-0.00,0.00) >>> turtle.heading() 0.0 >>> turtle.circle(120, 180) # draw a semicircle >>> turtle.position() (0.00,240.00) >>> turtle.heading() 180.0

turtle.dot(size=None, *color)

参数:size - 一个整数> = 1(如果给定)color - 一个颜色字符串或一个数字颜色元组

大小 - 一个整数> = 1(如果给出)

颜色 - 颜色字符串或数字颜色元组

Draw a circular dot with diameter _size_, using _color_. If _size_ is not given, the maximum of pensize+4 and 2\*pensize is used.

turtle.home() >>> turtle.dot() >>> turtle.fd(50); turtle.dot(20, "blue"); turtle.fd(50) >>> turtle.position() (100.00,-0.00) >>> turtle.heading() 0.0

turtle.stamp()

在当前turtle位置上将turtle形状的副本印到画布上。返回该邮票的stamp_id,可以通过调用将其删除clearstamp(stamp_id)。

>>> turtle.color("blue")

>>> turtle.stamp()

11

>>> turtle.fd(50)

turtle.clearstamp(stampid)

参数:stampid - 一个整数,必须是以前的stamp()调用的返回值

turtle.clearstamps(n=None)

参数:n - 整数(或无)

turtle.undo()

撤消(重复)最后的turtle动作。可用撤销操作的数量由不可扩展器的大小决定。

>>> for i in range(4):

... turtle.fd(50); turtle.lt(80)

...

>>> for i in range(8):

... turtle.undo()

turtle.speed(speed=None)

参数:速度 - 范围为0..10的整数或速度串(见下文)

“fastest”: 0

“fast”: 10

“normal”: 6

“slow”: 3

“slowest”: 1

从1到10的速度强制执行越来越快的画线和turtle转动的动画。

注意:速度 = 0意味着没有动画发生。向前/向后使turtle跳跃,同样向左/向右使turtle立即转动。

turtle.speed() 3 >>> turtle.speed('normal') >>> turtle.speed() 6 >>> turtle.speed(9) >>> turtle.speed() 9

3.2。告诉turtle的状态

turtle.position()turtle.pos()

返回turtle的当前位置(x,y)(作为Vec2D矢量)。

>>> turtle.pos()

(440.00,-0.00)

turtle.towards(x, y=None)

参数:x - 一个数字或一对数字或一个乌龟实例的向量y - 一个数字,如果x是一个数字,否则无

x - 一个数字或一对数字或一个turtle实例的向量

y - 如果x是数字,则为数字,否则None

Return the angle between the line from turtle position to position specified by (x,y), the vector or the other turtle. This depends on the turtle’s start orientation which depends on the mode - “standard”/”world” or “logo”).

turtle.goto(10, 10) >>> turtle.towards(0,0) 225.0

turtle.xcor()

返回turtle的x坐标。

>>> turtle.home()

>>> turtle.left(50)

>>> turtle.forward(100)

>>> turtle.pos()

(64.28,76.60)

>>> print turtle.xcor()

64.2787609687

turtle.ycor()

返回turtle的y坐标。

>>> turtle.home()

>>> turtle.left(60)

>>> turtle.forward(100)

>>> print turtle.pos()

(50.00,86.60)

>>> print turtle.ycor()

86.6025403784

turtle.heading()

更多推荐

pythonturtle库手册

本文发布于:2024-02-13 22:16:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760881.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手册   pythonturtle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!