1. 程式人生 > >【Python學習】之 畫小樹

【Python學習】之 畫小樹

def tree(plist, l, a, f):
    """ plist is list of pens
    l is length of branch
    a is half of the angle between 2 branches
    f is factor by which branch is shortened
    from level to level."""
    if l > 5: #
        lst = []
        for p in plist:
            p.forward(l)#沿著當前的方向畫畫Move the turtle forward by the specified distance, in the direction the turtle is headed.
q = p.clone()#Create and return a clone of the turtle with same position, heading and turtle properties. p.left(a) #Turn turtle left by angle units q.right(a)# turn turtle right by angle units, nits are by default degrees, but can be set via the degrees() and radians() functions.
lst.append(p)#將元素增加到列表的最後 lst.append(q) tree(lst, l*f, a, f) def main(): p = Turtle() p.color("green") p.pensize(5) #p.setundobuffer(None) p.hideturtle() #Make the turtle invisible. It’s a good idea to do this while you’re in the middle of doing some complex drawing,
#because hiding the turtle speeds up the drawing observably. #p.speed(10) # p.getscreen().tracer(1,0)#Return the TurtleScreen object the turtle is drawing on. p.speed(10) #TurtleScreen methods can then be called for that object. p.left(90)# Turn turtle left by angle units. direction 調整畫筆 p.penup() #Pull the pen up – no drawing when moving. p.goto(0,-200)#Move turtle to an absolute position. If the pen is down, draw line. Do not change the turtle’s orientation. p.pendown()# Pull the pen down – drawing when moving. 這三條語句是一個組合相當於先把筆收起來再移動到指定位置,再把筆放下開始畫 #否則turtle一移動就會自動的把線畫出來 #t = tree([p], 200, 65, 0.6375) t = tree([p], 200, 65, 0.6375) main()

效果圖

這裡寫圖片描述

turtle的函式呼叫

這裡寫圖片描述
這裡寫圖片描述
這裡寫圖片描述

相關推薦

Python學習 小樹

def tree(plist, l, a, f): """ plist is list of pens l is length of branch a is half of th

Python學習 Turtle庫

(一)簡介 (二)練習 import turtle def main(): turtle.pensize(3) turtle.penup()

機器學習 xgboost python ubuntu部署

安裝XGBoost-Python pip install xgboost --index https://pypi.mirrors.ustc.edu.cn/simple/ 安裝基本開發工具: $ sudo apt install git build-essential python-dev pyth

Python學習Python解決漢諾塔問題

次數 代碼 int 解題思路 move python學習 求解 color 印度 參考文章:http://www.cnblogs.com/dmego/p/5965835.html 一句話:學程序不是目的,理解就好;寫代碼也不是必然,省事最好;拿也好,查也好,解決問題就好

python學習使用python寫一個2048小遊戲

ast stc 遊戲 多少 wan nbsp 小遊戲 效果 參考 個人博客:jerwang.cn 沒有參考其他代碼,效果圖: 話不多少,源代碼: https://github.com/jerustc/Python/blob/master/2048.py【python學

Python學習Python中的數據類型精度問題

類型 一次 /usr logs int 第一次 pytho 整數 問題 Python真的很神奇。。。神奇到沒有直接的數據類型概念,並且精度可以是任意精度。想當初,第一次接觸OI算法時,寫得第一個算法就是高精度加法,搗鼓了半天。一切在Python看來,僅僅三行代碼即可完成。

python學習今天看看學習 %d ,%s, %f 等用法,下面的學習例子是說輸入名字、年齡、工作,工資。並給出65歲退休還差多久的計算

msg ear end style 資料 科學 一個 保留 value 今天看看學習 %d ,%s, %f 等用法。%d 是占位符整數,%s 是占位符,%f 是浮點數。下面的學習例子是說輸入名字、年齡、工作,工資。並給出65歲退休還差多久的計算。重點在於用占位符來print

Python學習列表

分片 替換 每一個 數組 reverse code last ast 包含 最近準備系統學習python,為了以後能及時查找到,先記錄下. 先說下啥叫序列,之前在用,感覺這個概念有個模糊,今天特意看了下,序列是python中最基本的數據結構,序列中的每一個元素都被分配一個序

Python + uiaotumator2Android—APP自動化簡易例子

layout div ext widget service .py safety wid pre 上代碼: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/08/31 09:43 #

Python學習解決pandas中打印DataFrame行列顯示不全的問題

需要 pandas pre pytho 如果 clas panda 顯示不全 可能 在使用pandas的DataFrame打印時,如果表太長或者太寬會自動只給前後一些行列,但有時候因為一些需要,可能想看到所有的行列。 所以只需要加一下的代碼就行了。 #顯示所有列 pd.se

Python學習程序運行完發送郵件提醒

直接 登錄 word 提醒 地址 install python3 ont world 有時候我們運行一個需要跑很長時間的程序,不管是在雲主機還是本地主機上運行,我們都不可能一直守在電腦面前等。所以想到使用郵件來通知提醒。 示例代碼如下 # -*- coding: utf-8

Python學習使用Pyinstaller將py檔案匯出為exe檔案

PyInstaller其實就是把python解析器和你自己的指令碼打包成一個可執行的檔案,但是它不是跨編譯的,也就是說在Windows下用PyInstaller生成的exe只能執行在Windows下,在Linux下生成的只能執行在Linux下。 Pyinstaller的安裝 直接使用pip install

Mac系統 + Python + Django開發一個釋出會系統Django模型(二) Mac系統 + Mysql安裝Mysql資料庫 Python + Mysql用pymysql庫連線Mysql資料庫並進行增刪改查操作

上一部分給大家介紹Django的檢視。 接下來繼續來了解Django框架,來看第二部分,此部分是對資料庫的操作。   目錄: 一、設計系統表 二、admin後臺管理 三、基本資料訪問(SQLite資料庫) 四、Django配置MySQL   &

Python學習csv庫

csv(Comma-Separated Values, 逗號分割值)是儲存表格資料的常用檔案格式。 它每一行都用一個換行符分隔,列與列之間用逗號分隔。 Python的csv庫可以非常簡單地修改csv檔案,甚至從零開始建立一個csv檔案。 比如: import csv csvFile = open("

Python學習字元編碼

先說兩個基礎知識。 (1)計算機內部,資料是由0,1組成的; (2)計算機最小的資料單位,就是一個二進位制單位即bit,接下來就是8個二進位制單位表示一個位元組(Byte)。 1 ASCII碼 ASCII碼(American Standard Code for Information Intercha

Python學習安裝與配置方法

Python安裝 作為熱門榜上上竄最快,也是當今一款非常流行的語言python!讓我們一起來學習吧 1.下載 官網下載地址:https://www.python.org/downloads/ 這裡我們選擇下載到E盤,下載完成後雙擊。 2.安裝 (

Python學習request庫

.html pri less tps python-r mac os part 絕對路徑 pytho Requests庫(https://www.python-requests.org/)是一個擅長處理那些復雜的HTTP請求、cookie、header(響應頭和請求頭)等內

python學習生成200個兌換碼

# coding UTF-8 import random words = [] keys = [] # 生成字元列表 for i in range(0,10): words.append(st

Python學習Jupyter Lab目錄外掛安裝

Jupyter Lab目錄外掛安裝 當然首先你得有python和已經安裝了jupyter lab。 1 安裝jupyter_contrib_nbextensions 首先先安裝jupyter_contrib_nbextensions,其專案地址是https://github.com/ipython-co

Python學習第三方庫安裝後仍然import失敗,提示ModuleNotFoundError:No module named'XXX'

安裝pycharm後,import numpy,執行後提示 ModuleNotFoundError:No module named'XXX' 開啟命令列,輸入pip install numpy後,成功安裝,但再次執行仍然提示錯誤。 原因猜測是第三方庫的安裝路徑與imp