天天看點

用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日 王宇林