天天看点

用Python的Tultle模块创建一个五角星

 方案所需准备

一个可执行Python的解释器

Ttultle简介来源

乌龟图形是一个不错的方式来为孩子们介绍编程。它是Wally Feurzig和Seymour Papert在1966年开发的原始Logo编程语言的一部分。

想象一只在x-y平面上,从(0,0)开始的海龟机器人。在import turtle之后,输入命令turtle.forward(15),然后它就在屏幕上动起来了!当它移动时会沿着他面向的方向画出一条15像素长的线。输入命令turtle.right(25),然后它就会原地顺时针转25度。

Turtle star(星)

海龟可以重复简单动作来绘制复杂的图形。

fromturtleimport*

color('red', 'yellow')

begin_fill()

whileTrue:

    forward(200)

    left(170)

    ifabs(pos()) <1:

        break

end_fill()

done()

通过将这些类似的命令组合在一起,可以很容易地绘制复杂的图形。

turtle模块是Python 2.5标准版以来同名模块的扩展版本。

-------------------------------------------------------------------------------------------

turtle模块常用命令

Turtle的运动

移动和绘制

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.setx" target="_blank"><code>setx() 设定x坐标</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.home" target="_blank"><code>home()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.circle" target="_blank"><code>circle()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.dot" target="_blank"><code>dot()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.stamp" target="_blank"><code>stamp()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.clearstamp" target="_blank"><code>clearstamp()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.clearstamps" target="_blank"><code>clearstamps()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.undo" target="_blank"><code>undo()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.speed" target="_blank"><code>speed()</code></a>

告诉乌龟的状态

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.towards" target="_blank"><code>towards()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.xcor" target="_blank"><code>xcor()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.ycor" target="_blank"><code>ycor()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.heading" target="_blank"><code>heading()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.distance" target="_blank"><code>distance()</code></a>

设置和测量

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.degrees" target="_blank"><code>degrees()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.radians" target="_blank"><code>radians()</code></a>

笔控制

绘图状态

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.pen" target="_blank"><code>pen()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.isdown" target="_blank"><code>isdown()</code></a>

颜色控制

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.color" target="_blank"><code>color()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.pencolor" target="_blank"><code>pencolor()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.fillcolor" target="_blank"><code>fillcolor()</code></a>

填充

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.filling" target="_blank"><code>filling()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.begin_fill" target="_blank"><code>begin_fill()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.end_fill" target="_blank"><code>end_fill()</code></a>

更多绘图控制

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.reset" target="_blank"><code>reset()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.clear" target="_blank"><code>clear()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.write" target="_blank"><code>write()</code></a>

乌龟状态

能见度

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.isvisible" target="_blank"><code>isvisible()</code></a>

出现

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.shape" target="_blank"><code>shape()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.resizemode" target="_blank"><code>resizemode()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.shearfactor" target="_blank"><code>shearfactor()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.settiltangle" target="_blank"><code>settiltangle()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.tiltangle" target="_blank"><code>tiltangle()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.tilt" target="_blank"><code>tilt()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.shapetransform" target="_blank"><code>shapetransform()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.get_shapepoly" target="_blank"><code>get_shapepoly()</code></a>

使用事件

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.%20title=" target="_blank"><code>onclick()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.onrelease" target="_blank"><code>onrelease()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.ondrag" target="_blank"><code>ondrag()</code></a>

特殊龟方法

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.begin_poly" target="_blank"><code>begin_poly()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.end_poly" target="_blank"><code>end_poly()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.get_poly" target="_blank"><code>get_poly()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.clone" target="_blank"><code>clone()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.getscreen" target="_blank"><code>getscreen()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.setundobuffer" target="_blank"><code>setundobuffer()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.undobufferentries" target="_blank"><code>undobufferentries()</code></a>

窗口控制

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.bgcolor" target="_blank"><code>bgcolor()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.bgpic" target="_blank"><code>bgpic()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.screensize" target="_blank"><code>screensize()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.setworldcoordinates" target="_blank"><code>setworldcoordinates()</code></a>

动画控制

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.delay" target="_blank"><code>delay()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.tracer" target="_blank"><code>tracer()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.update" target="_blank"><code>update()</code></a>

使用屏幕事件

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.listen" target="_blank"><code>listen()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.onkeypress" target="_blank"><code>onkeypress()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.ontimer" target="_blank"><code>ontimer()</code></a>

设置和特殊方法

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.mode" target="_blank"><code>mode()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.colormode" target="_blank"><code>colormode()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.getcanvas" target="_blank"><code>getcanvas()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.getshapes" target="_blank"><code>getshapes()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.turtles" target="_blank"><code>turtles()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.window_height" target="_blank"><code>window_height()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.window_width" target="_blank"><code>window_width()</code></a>

输入法

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.textinput" target="_blank"><code>textinput()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.numinput" target="_blank"><code>numinput()</code></a>

筛选特异性方法

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.bye" target="_blank"><code>bye()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.exit%20title=" target="_blank"><code>exitonclick()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.setup" target="_blank"><code>setup()</code></a>

<a href="http://python.usyiyi.cn/documents/python_352/library/turtle.html#turtle.title" target="_blank"><code>title()</code></a>

--------------------------------------------------------------------------------------

运行第一段命令

这里代表的是 引用 海龟 画图库

库 命令 向前 200距离

<a href="https://s3.51cto.com/oss/201711/03/7558dc21dfc19fbf5dabb80db2e3f562.png" target="_blank"></a>

这样第一个命令就成功运行了。画笔向前200距离

<a href="https://s3.51cto.com/oss/201711/03/1caa8cec96430ad71d6c2703b90ee30a.png-wh_500x0-wm_3-wmp_4-s_192527688.png" target="_blank"></a>

2.   第二个命令

<a href="https://s3.51cto.com/oss/201711/03/d279e6e646d367743214c9ad8e07091b.png-wh_500x0-wm_3-wmp_4-s_3343788893.png" target="_blank"></a>

效果图

一共五个笔画,五个角度

用Python画的五角星就画出来了.

Pthon语言龟叔给Python的定位是“优雅”、“明确”、“简单”,所以Python程序看上去总是简单易懂,初学者学Python,不但入门容易,而且将来深入下去,可以编写那些非常非常复杂的程序。

                                                                                  2017年11月3日 王宇林