[TOC]
### 課前準確:
* 在`Windows`系統里安裝`python`,參考 [安裝python](http://www.hmoore.net/k12edu/k_12/785427#Windowspython_4)
*****
### 本節課參考資料
* `Teach Your Kids to Code.pdf` 第十章:人類最早的電子游戲
*****
### 1972 年電子游戲誕生

這張圖是人類最早期的游戲,距離現在有40多年了。今天我們也重溫一下那些40年前的驚喜。當然經過40多年的進步,我們可以把這個游戲畫得更好看一些。
*****
### 打笑臉游戲基礎版
```
# SmileyPong1.py
import pygame
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Pong")
keepGoing = 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)
WHITE = (255,255,255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)
while keepGoing: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
picx += speedx
picy += speedy
if picx <= 0 or picx + pic.get_width() >= 800:
speedx = -speedx
if picy <= 0:
speedy = -speedy
if picy >= 500:
lives -= 1
speedy = -speedy
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
# 畫木板
paddlex = pygame.mouse.get_pos()[0]
paddlex -= paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
# Check for paddle bounce
if picy + pich >= paddley and picy + pich <= paddley + paddleh \
and speedy > 0:
if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + \
paddlew:
points += 1
speedy = -speedy
# 在屏幕上寫上還能復活幾次,得了多少分
draw_string = "Lives: " + str(lives) + " Points: " + str(points)
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
```

*****
### 打笑臉游戲升級版
在基礎版再添加一些功能:
* 如果復活次數用完了,就顯示游戲結束
* 增加一個可以重新開始游戲的快捷鍵
* 重新開始時,球的速度從一個很慢的速度開始
* 球不能卡在屏幕的地板上(上節課同學們發現了這個bug)
```
# SmileyPong2.py
import pygame # Setup
pygame.init()
screen = pygame.display.set_mode([800,600])
pygame.display.set_caption("Smiley Pong")
keepGoing = 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)
WHITE = (255,255,255)
timer = pygame.time.Clock()
speedx = 5
speedy = 5
paddlew = 200
paddleh = 25
paddlex = 300
paddley = 550
picw = 100
pich = 100
points = 0
lives = 5
font = pygame.font.SysFont("Times", 24)
while keepGoing: # Game loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_F1: # F1 = New Game
points = 0
lives = 5
picx = 0
picy = 0
speedx = 5
speedy = 5
picx += speedx
picy += speedy
if picx <= 0 or picx >= 700:
speedx = -speedx * 1.1
if picy <= 0:
speedy = -speedy + 1
if picy >= 500:
lives -= 1
speedy = -5
speedx = 5
picy = 499
screen.fill(BLACK)
screen.blit(pic, (picx, picy))
# Draw paddle
paddlex = pygame.mouse.get_pos()[0]
paddlex -= paddlew/2
pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh))
# Check for paddle bounce
if picy + pich >= paddley and picy + pich <= paddley + paddleh \
and speedy > 0:
if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + \
paddlew:
speedy = -speedy
points += 1
# Draw text on screen
draw_string = "Lives: " + str(lives) + " Points: " + str(points)
# Check whether the game is over
if lives < 1:
speedx = speedy = 0
draw_string = "Game Over. Your score was: " + str(points)
draw_string += ". Press F1 to play again. "
text = font.render(draw_string, True, WHITE)
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.y = 10
screen.blit(text, text_rect)
pygame.display.update()
timer.tick(60)
pygame.quit() # Exit
```

### 課程小結
這節課我們仍然使用 `pygame` 完成一些有趣的游戲,包括:
* 仿照打乒乓球做一個游戲
* 顯示游戲當前的狀態和結果
* 結束一個游戲以及重啟一個游戲