[TOC]
### 課前準確:
* 在`Windows`系統里安裝`python`,參考 [安裝python](http://www.hmoore.net/k12edu/k_12/785427#Windowspython_4)
*****
### 本節課參考資料
* `Teach Your Kids to Code.pdf` 第八章:計數器和動漫
*****
### 一個簡單的靜態動漫
```
#chapter8_1.py
import pygame #導入動漫工具pygame
pygame.init() #初始化,相當于打開電視
screen = pygame.display.set_mode([800,600]) #建立一個動漫基地
keep_going = True
GREEN = (0,255,0) # RGB color triplet for GREEN #給一個球涂上綠色
radius = 50 #定義球的大小
while keep_going: #檢查是不是要繼續動漫
for event in pygame.event.get():
if event.type == pygame.QUIT: #從動漫基地尋找是不是要退出的信號
keep_going = False # 如果有退出的信號,告訴keep_going
pygame.draw.circle(screen, GREEN, (100,100), radius) # 畫球
pygame.display.update()
pygame.quit() #關閉動漫
```

*****
### 復雜一點的靜態笑臉動漫
```
#chapter8_2
import pygame
pygame.init()
screen = pygame.display.set_mode([800,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp") #和上面那個例子的區別是,我們不再畫圖,而是直接導入一張笑臉
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
screen.blit(pic, (100,100))
pygame.display.update()
pygame.quit() # Exit
```

*****
### 一個會動的笑臉動漫
```
#chapter8_3.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([600,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock() # Timer for animation
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += 1 # 移動笑臉
picy += 1 # 移動笑臉
screen.fill(BLACK) # 用黑板擦把笑臉移動的痕跡抹掉
screen.blit(pic, (picx,picy))
pygame.display.update()
timer.tick(60) # 每秒鐘顯示60幅畫面
pygame.quit()
```

*****
### 一個加速了、可以回彈的笑臉動漫
```
#chapter8_4.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([600,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock()
speed = 5 #給動漫加速
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += speed
picy += speed
if picx <= 0 or picx + pic.get_width() >= 600:
speed = -speed # 回彈
screen.fill(BLACK)
screen.blit(pic, (picx,picy))
pygame.display.update()
timer.tick(60)
pygame.quit()
```

*****
### 一個更加活躍的笑臉動漫
```
#chapter8_5
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
keep_going = True
pic = pygame.image.load("CrazySmile.bmp")
colorkey = pic.get_at((0,0))
pic.set_colorkey(colorkey)
picx = 0
picy = 0
BLACK = (0,0,0)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
while keep_going: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
picx += speedx
picy += speedy
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0 or picy + pic.get_height() >= 600:
speedy = -speedy
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
```

*****
### 課程小結
`pygame` 是動漫開發工具,使用這個工具我們可以知道動漫是怎么實現的。