Python 海龜繪圖(Python Graphics with turtle)

若您覺得文章寫得不錯,請點選文章上的廣告,來支持小編,謝謝。

本文會介紹Python中的Turtle繪圖功能,並搭配線上版的Trinketrepl.it(Python turtle)來呈現繪圖結果。(題外話:Trinket也有提供積木拖曳的設計方式)

文字(Text)

可以使用write這個指令來輸出文字。Python官方的指令說明如下:
urtle.write(argmove=Falsealign="left"font=("Arial"8"normal"))
Parameters:
  • arg – object to be written to the TurtleScreen
  • move – True/False
  • align – one of the strings “left”, “center” or right”
  • font – a triple (fontname, fontsize, fonttype)
Write text - the string representation of arg - at the current turtle position according to align (“left”, “center” or right”) and with the given font. If move is true, the pen is moved to the bottom-right corner of the text. By default, move is False.
範例:


直線(Line)

畫直線的方式有很多種,例如可以使用 forward 向前或則使用 backward 以及goto這三個指令。搭配迴圈以及left、right就可以畫出長方形(Rectangle)、正方形(Square)等圖形了。


圓形(Circle)

Python 有提供畫圓形的指令,Python官方的指令說明如下:





turtle.circle(radiusextent=Nonesteps=None)
Parameters:
  • radius – a number
  • extent – a number (or None)
  • steps – an integer (or 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.
As the circle is approximated by an inscribed regular polygon, steps determines the number of steps to use. If not given, it will be calculated automatically. May be used to draw regular polygons.
奧運五色環範例:

沒有留言: