I Love Python. I Learn Python. I Teach Python. I Am a Python.

2014/07/10

教小孩 學 Python 程式語言,用 中文 及 英文。(01)

教小孩 學 Python 程式語言,用 中文 及 英文。

book 4 cover

LEVEL 1.5

Ch.1 - Introducing turtle
介紹 龜 畫圖

  •    >>> import the turtle module
    • 引入 龜模組
  •    >>> make your turtle move around in all directions
    • 叫你的 龜 在各個方向 四處移動
  •    >>> change what the turtle looks like.
    • 更改 龜 的外觀




https://gist.github.com/renyuanL/544f6ca7270bc7fde3a1
'''
ryHello_world.py
呂仁園, 2014/07/18
本程式需要中文龜模組 turtle_tc.py
https://github.com/renyuanL/pythonTurtleInChinese/blob/master/turtle_tc.py
'''
from turtle_tc import *
#
# 設定筆大小以及形狀
#
筆大小(5); 形狀(龜形)
#
# 畫字母 H
#
左轉(90); 前進(100); 後退(50)
右轉(90); 前進(40)
左轉(90); 前進(50); 後退(100)
#
# 移動到下個字母位置
#
提筆();右轉(90);前進(40);左轉(90); 下筆()
#
# 畫字母 i
#
前進(50); 提筆(); 前進(25); 下筆()
#
# 等著操作視窗界面
# 目前只提供【結束】功能
# 請點擊視窗右上角之 結束紐[X]。
#
進入主迴圈()




Ch.2 - Shapes, colour and repetition
形狀、顏色、重複

  •    >>> draw circles, squares and polygons
    • 畫 圓、方、多邊形
  •    >>> change the colour of your lines
    • 更改 線色
  •    >>> fill your shapes with colour
    • 填滿顏色於形狀之中
  •    >>> make Python do more with less code
    • 叫 Python 做多一點事、寫少一點程式碼

Ch.3 - Getting creative
來點創意

  •    >>> change the colour of the turtle, the background and the pen
    • 更改 龜、 背景 以及 筆 的顏色
  •    >>> fill shapes with colour
    • 把形狀填滿顏色
  •    >>> change the speed of the turtle
    • 更改 龜 速度
  •    >>> organise your code so that it is easy to experiment with.
    • 整理組織程式碼,使其易於實驗
https://gist.github.com/renyuanL/b11b92e9596cd92bbb7a
'''
ryFractal_tree.py
本程式需要中文龜模組 turtle_tc.py
https://github.com/renyuanL/pythonTurtleInChinese/blob/master/turtle_tc.py
呂仁園,2014/07/08
'''
from turtle_tc import *
寬度= 10
角度= 10
長度= 80
長度差= 10
速度(0) # 1: 慢, 10: 快, 0: 最快
def 畫樹(枝寬, 枝長):
if 枝長 <= 10: # 最終長度
return
else:
if 枝長 <= 20: # 綠枝長度
顏色(綠)
else:
顏色(紅)
筆大小(枝寬); 前進(枝長)
右轉(角度); 畫樹(枝寬 *0.8, 枝長 -長度差)
左轉(角度 *2); 畫樹(枝寬 *0.8, 枝長 -長度差)
右轉(角度); 後退(枝長); 顏色(紅)
提筆(); 左轉(90); 後退(200); 下筆();
畫樹(寬度, 長度)
進入主迴圈()






Building an art app:

建立 1 個 美術 應用程式

Ch.4 - From small beginnings
從小處開始


  •    >>> add background images to tkinter projects
    • 在 tkinter 專案中,加上背景圖。
  •    >>> write some more functions
    • 再多寫一些函數,增加程式的功能。
  •    >>> call functions with key presses on your keyboard.
    • 用鍵盤上的按鍵來呼叫那些函數。

Ch.5 - Red lines are not enough
只有紅線不夠


  •    >>> change the line colour
    • 改變線的顏色。
  •    >>> change the line thickness
    • 改變線的寬度。
  •    >>> attach functions to buttons instead of key presses
    • 改用滑數按鈕(button)來控制函數。
  •    >>> adjust the size of buttons.
    • 調整按鈕的大小。

Ch.6 - Stamping and painting
加上蓋印以及徒手畫


  •    >>> organise buttons into frames
    • 用「框」 來整理按鈕。
  •    >>> make a function to add an image to your canvas
    • 寫一個函數來加圖於 畫布 中。
  •    >>> attach functions to mouse movements.
    • 用滑鼠的「移動」來連結 函數。


https://gist.github.com/renyuanL/89106c0e4899544d0ad8
'''
ryBonusArtApp2.py
ry美術應用.py
呂仁園, 2014/07/10
'''
# 改自 BonusArtApp2.py
import tkinter
import random
隨機整數= random.randint
#
# 中文別名
#
窗類= 視窗類= tkinter.Tk
圖類= 圖像類= tkinter.PhotoImage
布類= 畫布類= tkinter.Canvas
框類= 畫框類= tkinter.Frame
鈕類= 按鈕類= tkinter.Button
全部= tkinter.ALL
左邊= tkinter.LEFT
#
# 全域 (global) 變數
#
布寬= 600
布高= 400
布色= "black"
座標x= 布寬/2
座標y= 布高
線色= "red"
線寬= 5
線長= 5
此線= None
此圖= None
#
# 啟動一個 窗,
# 從此就可使用其他相關的元件
#
窗= 窗類()
窗.title("美術應用")
#
# 準備好一些 美美 的 圖檔,在此為一些 *.gif 檔案
#
背景= 圖類(file= "treescape.gif")
紅線= 圖類(file= "red.gif")
藍線= 圖類(file= "blue.gif")
綠線= 圖類(file= "green.gif")
黃線= 圖類(file= "yellow.gif")
黑線= 圖類(file= "black.gif")
白線= 圖類(file= "white.gif")
加號= 圖類(file= "plus.gif")
減號= 圖類(file= "minus.gif")
小樹= 圖類(file= "tree.gif")
小康= 圖類(file= "campbell.gif")
小艾= 圖類(file= "ayesha.gif")
小傑= 圖類(file= "jeff.gif")
小可= 圖類(file= "krysta.gif")
小漢= 圖類(file= "hana.gif")
小荷= 圖類(file= "jose.gif")
小莉= 圖類(file= "leela.gif")
#
# 設計一些 函數 (動作)
#
def 選紅線():
global 線色
global 此圖
此圖 = None
線色 = "red"
def 選綠線():
global 線色
global 此圖
此圖 = None
線色 = "green"
def 選藍線():
global 線色
global 此圖
此圖 = None
線色 = "blue"
def 選黃線():
global 線色
global 此圖
此圖 = None
線色 = "yellow"
def 選黑線():
global 線色
global 此圖
此圖 = None
線色 = "black"
def 選白線():
global 線色
global 此圖
此圖 = None
線色 = "white"
def 減小():
global 線寬
global 線長
if (線寬 > 5):
線寬 -= 5
線長 -= 5
def 加大():
global 線寬
global 線長
if (線寬 <= 50):
線寬 += 5
線長 += 5
def 選小樹():
選人(小樹)
def 選小康():
選人(小康)
def 選小艾():
選人(小艾)
def 選小傑():
選人(小傑)
def 選小可():
選人(小可)
def 選小漢():
選人(小漢)
def 選小荷():
選人(小荷)
def 選小莉():
選人(小莉)
def 畫(e):
'''
這個 e 會夾帶 滑鼠座標 .x, .y 傳進來
'''
if (此圖 != None):
布.create_image(e.x, e.y, image= 此圖)
else:
寬= 線寬/2
x1= e.x - 寬
y1= e.y - 寬
x2= e.x + 寬
y2= e.y + 寬
布.create_oval(x1, y1, x2, y2, fill= 線色)
def 選人(img):
global 此圖
此圖= img
def 製造群眾():
n=0
while n < 100:
選擇= 隨機整數(1, 7)
x= 隨機整數 ( 0, 布寬)
y= 隨機整數 ( 0, 布高)
if 選擇 == 1:
布.create_image(x, y, image= 小艾)
elif 選擇 == 2:
布.create_image(x, y, image= 小康)
elif 選擇 == 3:
布.create_image(x, y, image= 小傑)
elif 選擇 == 4:
布.create_image(x, y, image= 小可)
elif 選擇 == 5:
布.create_image(x, y, image= 小漢)
elif 選擇 == 6:
布.create_image(x, y, image= 小荷)
else:
布.create_image(x, y, image= 小莉)
n += 1
def 擦掉全部():
布.delete(全部)
布.create_image(布寬/2, 布高/2, image= 背景)
#
# 開始造各種元件,包含:
#
# 窗、布、框、鈕
#
布= 布類(bg= 布色, height= 布高, width= 布寬)
布.create_image(布寬/2, 布高/2, image= 背景)
布.bind("<B1-Motion>", 畫)
布.bind("<Button-1>", 畫)
上框= 框類(窗)
中框= 框類(窗)
下框= 框類(窗)
鈕群= []
鈕= 鈕類(上框, image= 紅線, command= 選紅線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 綠線, command= 選綠線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 藍線, command= 選藍線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 黃線, command= 選黃線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 黑線, command= 選黑線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 白線, command= 選白線); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 加號, command= 加大); 鈕群 += [鈕]
鈕= 鈕類(上框, image= 減號, command= 減小); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小康, command= 選小康); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小艾, command= 選小艾); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小傑, command= 選小傑); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小可, command= 選小可); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小漢, command= 選小漢); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小荷, command= 選小荷); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小莉, command= 選小莉); 鈕群 += [鈕]
鈕= 鈕類(中框, image= 小樹, command= 選小樹); 鈕群 += [鈕]
鈕= 鈕類(下框, text= '製造群眾', command= 製造群眾); 鈕群 += [鈕]
鈕= 鈕類(下框, text= '擦掉全部', command= 擦掉全部); 鈕群 += [鈕]
#
# 把所有元件包裝安置起來,各就各位。
#
布.pack()
上框.pack()
中框.pack()
下框.pack()
for 鈕 in 鈕群:
鈕.pack(side= 左邊)
#
# 一切準備就緒,讓程式進入 主迴圈。
#
窗.mainloop()





沒有留言:

張貼留言