<font color=#008000> >作者: 吳姿瑩 >更新:2022.09.23 </font>[color=#008000] # Lesson03: 安裝Vpython ###### tags: `運動科學模擬` `多元選修` `VPython` `安裝` ## :memo:安裝Python 在google搜尋列上搜尋python,通常第一個網址就是[python官方網址](https://www.python.org/),目前最新下載版本為3.10.7,請根據自己的硬體設備下載合適的版本。 ##### 1. Python下載點: https://www.python.org/downloads/release/python-364/ <a href="https://imgur.com/0KTkkjO"><img src="https://i.imgur.com/0KTkkjO.png" title="source: imgur.com" /></a> ##### 2. 下載合適的版本:(建議下載executable版本) <a href="https://imgur.com/LNDrl8J"><img src="https://i.imgur.com/LNDrl8J.png" title="source: imgur.com" /></a> 目前VPtyon可使用python 3.8, 3.9, 3.10版本,所以可以選擇3.10.7版本下載。VPython版本舊版Vpython6以及新版VPython7,不同版本語法會有些微不同,這堂課使用版本為VPython 7 :heavy_check_mark:新版:Python 3.X + VPython 7 ##### 3. 安裝Python(至下載資料夾中找到Python程式檔) <a href="https://imgur.com/450Ikt9"><img src="https://i.imgur.com/450Ikt9.png" title="source: imgur.com" /></a> ##### 4. 使用**系統管理員**身分執行(按右鍵執行) <a href="https://imgur.com/ZVTLpwo"><img src="https://i.imgur.com/ZVTLpwo.png" title="source: imgur.com" /></a> ##### 5. 安裝Python選項 建議勾選視窗中最下方的選項 Add Python 3.6 to PATH,在安裝之後如果要從指令介面執行 Python 時 Windows 才能在 PATH 當中找到對應的指令。 <a href="https://imgur.com/WWqA1cB"><img src="https://i.imgur.com/WWqA1cB.png" title="source: imgur.com" /></a> ##### 6. 安裝中畫面 <a href="https://imgur.com/uUQtCqq"><img src="https://i.imgur.com/uUQtCqq.png" title="source: imgur.com" /></a> ##### 7. 安裝Python成功畫面 <a href="https://imgur.com/HohEdiw"><img src="https://i.imgur.com/HohEdiw.png" title="source: imgur.com" /></a> ## :memo:測試Python ##### 1. 在Windows開始工具列搜尋Python,選取IDLE,開啟和Python 直譯器同時安裝的整合式開發環境。 <a href="https://imgur.com/kUIny9z"><img src="https://i.imgur.com/kUIny9z.png" title="source: imgur.com" /></a> ##### 2.預設會開啟Python Shell,可在這裡輸入指令或程式碼。學程式通常喜歡用 Hello World 當作第一支程式,可在此輸入 ```javascript= print ("hi") ``` 按下ENTER後,就會得到輸出hi的字。恭喜得到第一個程式碼 :smile: <a href="https://imgur.com/HdDteJD"><img src="https://i.imgur.com/HdDteJD.png" title="source: imgur.com" /></a> ##### 3.開啟新檔使用**存檔為.py檔案**,方便以後可以存檔修改。 Python Shell的工具列選取 File -> New File 或按 **Ctrl+N**,輸入程式碼後再按 **Ctrl+S** 存檔,再按下**F5** 執行程式。 <a href="https://imgur.com/rNYnFPi"><img src="https://i.imgur.com/rNYnFPi.png" title="source: imgur.com" /></a> <a href="https://imgur.com/5rwx9qW"><img src="https://i.imgur.com/5rwx9qW.png" title="source: imgur.com" /></a> 按下**F5** 執行程式,會在Python Shell中顯示結果! <a href="https://imgur.com/lcZ6WlD"><img src="https://i.imgur.com/lcZ6WlD.png" title="source: imgur.com" /></a> :::spoiler **學生活動** 1.請為剛剛的第一支程式存檔,並將.py檔放置於***雲端硬碟*** 2.剛剛第一支程式碼,請截圖至google classroom作業 ::: ## :memo:安裝VPython 在google搜尋列上搜尋VPython,則會找到[VPython網站](https://vpython.org/),目前VPython 已更新至[VPython 7](https://www.glowscript.org/docs/VPythonDocs/index.html) - 舊站VPython 6:https://vpython.org/ - 新站VPython 7:https://www.glowscript.org/docs/VPythonDocs/index.html <a href="https://imgur.com/P8B6bRU"><img src="https://i.imgur.com/P8B6bRU.png" title="source: imgur.com" /></a> 安裝新版使用Python自動安裝對應的VPython。 ##### 1. 安裝VPython 在 Windows 開始選單搜尋 Windows PowerShell,並使用**系統管理身分**執行, <a href="https://imgur.com/oatmR3R"><img src="https://i.imgur.com/oatmR3R.png" title="source: imgur.com" /></a> ##### 2.打開 Windows PowerShell,輸入以下指令 ``` pip3 install vpython ``` <a href="https://imgur.com/YuOh1Mj"><img src="https://i.imgur.com/YuOh1Mj.png" title="source: imgur.com" /></a> ##### 3.倘若有**紅字標示Command**,You should consider upgrading via the 'python -m pip install --upgrade pip',則表示需要更新pip,請在指令列中輸入以下指令 ``` python -m pip install --upgrade pip ``` <a href="https://imgur.com/RksIz4Q"><img src="https://i.imgur.com/RksIz4Q.png" title="source: imgur.com" /></a> ##### 4.在安裝一次VPython,在指令列中輸入已下指令 ``` pip3 install vpython ``` <a href="https://imgur.com/KwOnBsn"><img src="https://i.imgur.com/KwOnBsn.png" title="source: imgur.com" /></a> ##### 5.最後可更新VPython,,在指令列中輸入已下指令 ``` pip3 install vpython --upgrade ``` 未來倘若有VPython更新版本,可至Windows PowerShell輸入指令更新即可。 <a href="https://imgur.com/LOrt1kE"><img src="https://i.imgur.com/LOrt1kE.png" title="source: imgur.com" /></a> ## :memo:測試VPython 請下載[測試.py](https://github.com/TzuYingWu/Vpython-demo/blob/1edfa40b917047b5784c0552ba9bbf335664340f/00%20%E5%AE%89%E8%A3%9D%E6%B8%AC%E8%A9%A6/%E5%AE%89%E8%A3%9D.py),複製程式碼至Python Shell <a href="https://imgur.com/KbdNmae"><img src="https://i.imgur.com/KbdNmae.png" title="source: imgur.com" /></a> - 程式碼如下: ```javascript= from vpython import * """ 1. 參數設定, 設定變數及初始值 """ size = 0.1 # 木塊邊長 L = 1 # 地板長度 v = 0.03 # 木塊速度 t = 0 # 時間 dt = 0.01 # 時間間隔 """ 2. 畫面設定 """ scene = canvas(title="1D Motion", width=800, height=600, x=0, y=0, center=vec(0, 0.1, 0), background=vec(0, 0.6, 0.6)) floor = box(pos=vec(0, 0, 0), size=vec(L, 0.1*size, 0.5*L), color=color.blue) cube = box(pos=vec(-0.5*L + 0.5*size, 0.55*size, 0), size=vec(size, size, size), color=color.red, v=vec(v, 0, 0)) gd = graph(title="x-t plot", width=600, height=450, x=0, y=600, xtitle="t(s)", ytitle="x(m)") gd2 = graph(title="v-t plot", width=600, height=450, x=0, y=1050, xtitle="t(s)", ytitle="v(m/s)") xt = gcurve(graph=gd, color=color.red) vt = gcurve(graph=gd2, color=color.red) """ 3. 物體運動部分, 木塊到達地板邊緣時停止執行 """ while(cube.pos.x <= 0.5*L- 0.5*size): rate(1000) cube.pos.x += v*dt xt.plot(pos = (t, cube.pos.x)) vt.plot(pos = (t, cube.v.x)) t += dt print("t = ", t) ``` 按下**F5**執行動畫,可以看到在網頁會開啟一個畫面,執行程式碼的動畫,即是安裝成功! <iframe width="560" height="315" src="https://www.youtube.com/embed/GQpnDfmfuVM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> ## :memo:語法資料庫 學習程式語言需要了解各種程式的語法,可善用查詢、執行,可以得到更多的效果! VPython語法: https://www.glowscript.org/docs/VPythonDocs/index.html Python語法:https://python-doc-tw.github.io/library/index.html 台大石明豐教授建置的網站:https://vphysics.ntu.edu.tw// 期待各位可以越寫越順手、越玩越深入!