編碼的世界 / 優質文選 / 文明

Python中的turtle初探


2022年5月04日
-   

turtle
Python自帶了一個turtle庫,就像名字turtle說的那樣,你可以創建一個turtle,然後這個turtle可以前進,後退,左轉,這個turtle有一條尾巴,能夠放下和抬起,當尾巴放下的時候,turtle走過的地方就留下了痕跡,也就是這只畫筆的原理。
下面的表格是基本的一些turtle的方法,這裏簡單列舉了一點。
命令
解釋
turtle.Screen()
返回一個singleton object of a TurtleScreen subclass
turtle.forward(distance)
向當前畫筆方向移動distance像素長
turtle.backward(distance)
向當前畫筆相反方向移動distance像素長度
turtle.right(degree)
順時針移動degree°
turtle.left(degree)
逆時針移動degree°
turtle.pendown()
移動時繪制圖形,缺省時也為繪制
turtle.goto(x,y)
將畫筆移動到坐標為x,y的位置
turtle.penup()
移動時不繪制圖形,提起筆,用於另起一個地方繪制時用
turtle.speed(speed)
畫筆繪制的速度範圍[0,10]整數
turtle.circle()
畫圓,半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
下面是一個很簡單的turtle的例子,我們使用turtle來畫一個螺旋的圖案,這個函數采用遞歸的方法,每次遞歸的畫筆減小了5個單位長度,進而形成了一個向內螺旋的圖案。
import turtle
my_turtle = turtle.Turtle()
my_win = turtle.Screen()
def draw_spiral(my_turtle, line_len):
if line_len > 0 :
my_turtle.forward(line_len) # turtle前進
my_turtle.right(90) # turtle向右轉
draw_spiral(my_turtle, line_len - 5) #turtle繼續前進向右轉
draw_spiral(my_turtle, 100)
my_win.exitonclick()

畫一顆樹
接下來,我們用turtle來畫一顆樹。過程是這樣的:
branch_len為樹枝的長度,這裏的turtle也是采用遞歸的方法,在樹枝需要分叉的地方建立一顆新的子樹,而且是左右兩顆子樹,右子樹的長度比左子樹的長度要少5個單位。
import turtle
def tree(branch_len, t):
if branch_len > 5:
t.forward(branch_len)
t.right(20)
tree(branch_len - 15, t)
t.left(40)
tree(branch_len - 10, t)
t.right(20)
t.backward(branch_len)
def main():
t = turtle.Turtle()
my_win = turtle.Screen()
t.left(90)
t.up()
t.backward(100)
t.down()
t.color("green")
tree(75, t)
my_win.exitonclick()
main()

The Sierpinski triangle illustrates a three-way recursive algorithm. The procedure for drawing a Sierpinski triangle by hand is simple. Start with a single large triangle. Divide this large triangle into four new triangles by connecting the midpoint of each side. Ignoring the middle triangle that you just created, apply the same procedure to each of the three corner triangles

import turtle
def draw_triangle(points, color, my_turtle):
my_turtle.fillcolor(color)
my_turtle.up()
my_turtle.goto(points[0][0],points[0][1])
my_turtle.down()
my_turtle.begin_fill()
my_turtle.goto(points[1][0], points[1][1])
my_turtle.goto(points[2][0], points[2][1])
my_turtle.goto(points[0][0], points[0][1])
my_turtle.end_fill()
def get_mid(p1, p2):
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2)
def sierpinski(points, degree, my_turtle):
color_map = ['blue', 'red', 'green', 'white', 'yellow',
'violet', 'orange']
draw_triangle(points, color_map[degree], my_turtle)
if degree > 0:
sierpinski([points[0],
get_mid(points[0], points[1]),
get_mid(points[0], points[2])],
degree-1, my_turtle)
sierpinski([points[1],
get_mid(points[0], points[1]),
get_mid(points[1], points[2])],
degree-1, my_turtle)
sierpinski([points[2],
get_mid(points[2], points[1]),
get_mid(points[0], points[2])],
degree-1, my_turtle)
def main():
my_turtle = turtle.Turtle()
my_win = turtle.Screen()
my_points = [[-100, -50], [0, 100], [100, -50]]
sierpinski(my_points, 3, my_turtle)
my_win.exitonclick()
main()

Reference:
python中的turtle庫繪制圖形
1. 前奏: 在用turtle繪制圖形時,需要安裝對應python的解釋器以及IDE,我安裝的是pycharm,在安裝完pycharm後,在pycharm安裝相應庫的模塊,繪圖可以引入turtle模塊
python中關於turtle庫的學習筆記
一.基礎概念 1.畫布:畫布就是turtle為我們展開用於繪圖區域, 我們可以設置它的大小和初始位置.常用的畫布方法有兩個:screensize()和setup(). (1)turtle.screen
python中的turtle庫(圖形繪畫庫)
turtle繪圖的基礎知識:1. 畫布(canvas) 畫布就是turtle為我們展開用於繪圖區域,我們可以設置它的大小和初始位置. 設置畫布大小 turtle.screensize(canvwidt
使Python中的turtle模塊畫圖兩只小羊
turtle.circle(radius, extent=None, steps=None) 描述: 以給定半徑畫圓 參數: radius(半徑); 半徑為正(負),表示圓心在畫筆的左邊(右邊)畫圓
Python中的網絡掃描大殺器Scapy初探
Python中的網絡掃描大殺器Scapy初探     最近經歷了Twisted的打擊,這個網絡編程實在看不懂,都摸不透它的內在邏輯,看來網絡編程不是那麼好弄的.還好,看到了scapy,這種網絡的大殺器
2016/1/2 Python中的多線程(1):線程初探
-恢複內容開始- 新年第一篇,繼續Python. 先來簡單介紹線程和進程. 計算機剛開始發展的時候,程序都是從頭到尾獨占式地使用所有的內存和硬件資源,每個計算機只能同時跑一個程序.後來引進了一
Python學習之turtle庫和蟒蛇繪制程序
Python的函數庫 Python語言與C語言Java類似,可以大量使用外部函數庫包含在安裝包中的函數庫:. 比如math, random, turtle等其他函數庫,其他函數庫用戶根據代碼需求自行安
《python解釋器源碼剖析》第2章python中的int對象
2.0 序 在所有的python內建對象中,整數對象是最簡單的對象.從對python對象機制的剖析來看,整數對象是一個非常好的切入點.那麼下面就開始剖析整數對象的實現機制 2.1 初識PyLongOb
python中庫引用與import
在蟒蛇繪制函數中,多有turtle.   ,稱它為.的編碼風格 庫引用 擴充python程序功能的方式 使用import保留字完成,采用 隨機推薦
ReactJS學習筆記(一)
1.依賴的資源:   &l
開啟flask調試
直接將app.debug = True時,程序出錯並沒有出現調試界面. 按照如下設置,flask+uwsgi情況下,python報錯時會在瀏覽器中提示錯誤信息.方便調試. from werkzeug.
C# int轉byte[],byte[]轉int
第一種方法: byte數組轉int u = (uint)(b[0] | b[1] << 8 |b[2] << 16 | b[3] << 24); int轉byte數
Scrum會議1
小組名稱:天天向上 項目名稱:連連看 參會成員:王森(Master)張金生 張政 欒驕陽 時間:2016.10.16 會議內容: 1.連連看遊戲選取圖片素材. 2.連連看的界面設計 3.連連看具體的功
轉 UINavigationItem UINavigationBar 關系分析
原文:http://blog.csdn.net/luoyeffcs/article/details/16106707 目錄 1.關系分析 2.關系綜述 3.概念點 4.疑問 1.關系分析 UIBarI
windows下Eclipse安裝Perl插件教程
windows下Eclipse安裝Perl插件教程 想用eclipse編寫perl.網上看了很多資料.但EPIC插件的下載連接都失效了.無奈,只好自己動手寫個教程記錄一下. 准備工作: 安裝好Ecli
Matlab內置函數
[原創]Matlab.NET混編技巧之——找出Matlab內置函數   Matlab與.NET的混合編程,掌握了基本過程,加上一定的開發經驗和算法基礎,肯定不難.反之,有時候一個小錯誤,可能抓破腦袋,
phpcms v9 後台首頁 去掉團隊信息等版權
phpcmslanguageszh-cnadmin.lang.php $LANG['main_product_team'] = 'PHPCMS開發團隊'; phpcmsmodulesadmi
js禁止選中&lpar;網頁複制&rpar;
document.onselectstart=new Function("event.returnValue=false");//禁止選中(無法複制) document.oncon
解決問題SyntaxError&colon; Unexpected token import
ES6語法的模塊導入導出(import/export)功能,我們在使用它的時候,可能會報錯: SyntaxError: Unexpected token import 語法錯誤:此處不應該出現impo

熱門文章