Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Turtle 模組.

Similar presentations


Presentation on theme: "Python Turtle 模組."— Presentation transcript:

1 Python Turtle 模組

2 進入python的烏龜世界 1. 2. 3. 4. 或直接按F5執行

3 開始畫畫囉 from turtle import * for i in range(50): forward(400) left(110)
done()

4 移動指令介紹 forward(p):向前方移動 p 個像素距離 backward(p):向後方移動 p 個像素距離
right(t):順時針旋轉 t 度 right( 45 ) left(t):逆時針旋轉 t 度 left(60) 牛刀小試 請用程式畫出 “田”及

5 提筆、下筆與移動 penup():提筆之後路徑不會被畫出 pendown():下筆之後路徑會被畫出
goto(x,y) :將游標移動到(x , y)的位置 forward(20) penup() pendown() 牛刀小試 請用程式寫出”中山”

6 改變形狀、顏色 shape(s): 將游標設為 s 形狀 shapesize(s): 將游標大小在 x 及 y 方向均延展 s 比例
s: 'arrow', 'turtle', 'circle', 'square', 'triangle', 'classic‘ shape(‘turtle’) 會將游標變成烏龜樣式 shapesize(s): 將游標大小在 x 及 y 方向均延展 s 比例 color(c, b): 將游標輪廓及內部分別設為 c 及 b 顏色 c, b: 'red', 'yellow', 'green', …, (R,G, B) pensize(s): 將畫筆粗細設為 s 整數 牛刀小試 請用程式”將游標換成烏龜”並畫出一個”粗框的藍正方形”

7 使用函式 要怎麼畫出下圖?

8 使用函式 forward(100) right(90) penup() goto(50,50) pendown() 要怎麼畫出下圖?

9 使用函式 注意這邊要空四格 forward(100) right(90) penup() def square(): goto(50,50)
pendown() def square(): forward(100) right(90) 注意這裏要有冒號 牛刀小試 練習一下編寫square函式

10 使用函式 有了square函式,但要怎麼畫出下圖?

11 使用函式 有了square函式,但要怎麼畫出下圖? forward(100) right(90) OK forward(200)

12 使用函式 forward(100) right(90) def square(s): forward(s) right(90)

13 使用函式 def square(s): forward(s) right(90) 能不能再精簡一點?

14 使用for迴圈 def square(s): forward(s) right(90) for i in range(4):
注意這裏要有冒號 1 for i in range(4): forward(s) right(90) 2 3 4 注意這邊要空四格

15 使用for迴圈 range([start], stop[, step]) 產生一個從 start 到 stop 的串列(list)
forward(s) right(90) i = 2 i = 3 i = 4 for i in range(4): forward(s) right(90) for i in [0,1,2,3]: forward(s) right(90) 相當於 達到重複四次 牛刀小試 請用for迴圈畫出一個”正五邊形”、”正六邊形”

16 動動腦 請善用for迴圈畫出正 n 邊形,n = 3, 4, 5, 6, … 9 提示:
def polygon( s, n ): # s代表邊長,n代表要畫正n邊形

17 圓形 circle ( radius ) :畫出半徑為 radius 的圓
circle ( radius, extent ):畫出半徑為radius,角度為extent的弧 circle( 50, 45 ) circle (50) circle (50,90) circle (50,180) circle (100,-180)

18 Turtle Olympics 牛刀小試

19 動動腦 如何畫同心圓 (半徑分別為50,100,150,200)

20 加入顏色 begin_fill() :要開始畫填入色彩的圖形囉 end_fill():結束要填入色彩的圖形了

21 Random 來亂一下 要先 import random random.randint(a, b):得到一個介於a與b之間的數字
random.choice([元素]):從自訂的串列中隨機取得元素 random.choice([“read”,”blue”]) 第一次執行 第二次執行

22 利用簡單圖案作圖 def petal(s): circle(s,90) left(90) def flower(s):
for i in range(4): petal(s) def fish(s): left(120) 牛刀小試 請畫出10朵花和魚,隨機決定出現的位置和大小

23 動動腦 隨機填滿花的顏色


Download ppt "Python Turtle 模組."

Similar presentations


Ads by Google