pygame安裝方法:http://www.cnblogs.com/GraceSkyer/p/8004004.html
1.Pygame 簡介與安裝
Python優秀的第三方庫
SDL:各個操作系統上提供的一種能直接與計算機外設打交道的一個第三方庫
在這個庫基礎上做了相關封裝
安裝:在win平台:“以管理員身份運行”cmd,執行pip install pygame
win平台:“以管理員身份運行”cmd,執行python -m pygame.examples.aliens
Pygame 是Python最經典的2D游戲開發第三方庫,也支持3D游戲開發
Pygame 是一種游戲開發引擎,基本邏輯具有參考價值
==============================================================================
2.Pygame 最小開發框架及最小游戲
最小開發框架與極簡開發框架:
源代碼:
1 # Pygame的Hello World程序,包含了Pygame最小開發框架。 2 import pygame,sys 3 4 # 對pygame內部各功能模塊進行初始化創建及變量設置,默認調用 5 pygame.init() 6 # 初始化顯示窗口,第一個參數是一個二元組,分別表示窗口的寬度和高度 7 screen = pygame.display.set_mode((600, 400)) 8 # 設置顯示窗口的標題內容,參數是一個字符串類型 9 pygame.display.set_caption("Pygame游戲之旅") 10 11 while True: 12 # 從pygame的事件隊列中取出事件,並從隊列中刪除該事件,例如:鍵盤按下是一個事件 13 for event in pygame.event.get(): 14 # 獲得事件類型,並逐類響應;pygame.QUIT是pygame中定義的退出事件常量 15 if event.type == pygame.QUIT: 16 sys.exit() 17 # 對顯示窗口進行更新,默認窗口全部重繪 18 pygame.display.update()

sys是python的標准庫
sys提供python運行時環境變量的操控
sys.exit()用於退出結束游戲並退出
==============================================================================
3.壁球小游戲(展示型)與圖像的基本使用

需求到實現的三個關鍵要素:
1 壁球:游戲需要一個壁球,通過圖片引入
2 壁球運動:壁球能上下左右運動(每次向右及向下移動1個像素)
3 壁球反彈:壁球能在上下左右邊緣反彈(到達邊緣后,速度取反即可)
通過鏈接下載壁球圖片:https://python123.io/PY15/PYG02-ball.gif
圖片保存為gif格式,和.py文件保存在同一個文件夾中。
注意坐標軸體系:窗口左上角坐標(0,0),橫軸正向向右,縱軸正向向下。
源代碼:
1 # Pygame壁球小游戲(展示型) 2 import pygame,sys 3 4 pygame.init() 5 size = width, height = 600, 400 6 speed = [1,1] 7 BLACK = 0, 0, 0 8 9 screen = pygame.display.set_mode(size) 10 pygame.display.set_caption("Pygame壁球") 11 12 ball = pygame.image.load("PYG02-ball.gif") 13 ballrect = ball.get_rect() 14 15 while True: 16 for event in pygame.event.get(): 17 if event.type == pygame.QUIT: 18 sys.exit() 19 ballrect = ballrect.move(speed[0], speed[1]) 20 if ballrect.left < 0 or ballrect.right > width: 21 speed[0] = - speed[0] 22 if ballrect.top < 0 or ballrect.bottom > height: 23 speed[1] = - speed[1] 24 25 screen.fill(BLACK) 26 screen.blit(ball, ballrect) 27 pygame.display.update()








==============================================================================
4.壁球小游戲(節奏型)與屏幕的幀率設置
需求:壁球可以按照一定速度運動
需求到實現的關鍵要素:
壁球速度:如何控制壁球的運動速度
每次循環壁球運動一步;控制循環間隔即可控制速度;(展示型)在盡最大能力運動
pygame.time.Clock() 創建一個Clock對象,用於操作時間
Clock.tick(fps) 控制幀速度,即窗口刷新速度,表示每秒fps次幀刷新
視頻中每次展示的靜態圖像稱為幀
源代碼:
1 # Pygame壁球小游戲(節奏型) 2 import pygame,sys 3 4 pygame.init() 5 size = width, height = 600, 400 6 speed = [1,1] 7 BLACK = 0, 0, 0 8 screen = pygame.display.set_mode(size) 9 pygame.display.set_caption("Pygame壁球") 10 ball = pygame.image.load("PYG02-ball.gif") 11 ballrect = ball.get_rect() 12 fps = 300 13 fclock = pygame.time.Clock() 14 15 while True: 16 for event in pygame.event.get(): 17 if event.type == pygame.QUIT: 18 sys.exit() 19 ballrect = ballrect.move(speed[0], speed[1]) 20 if ballrect.left < 0 or ballrect.right > width: 21 speed[0] = - speed[0] 22 if ballrect.top < 0 or ballrect.bottom > height: 23 speed[1] = - speed[1] 24 25 screen.fill(BLACK) 26 screen.blit(ball, ballrect) 27 pygame.display.update() 28 fclock.tick(fps)
==============================================================================
5.壁球小游戲(操控型)與鍵盤的基本使用
需求:通過鍵盤的上下左右控制壁球運動速度,規則如下:
↑:縱向絕對速度增加1個像素【縱向加速】
↓:【縱向減速】
←:【橫向減速】
→:【橫向加速】
需求到實現的關鍵要素:
鍵盤使用:如何獲取鍵盤的操作事件
速度調節:根據對應按鍵調節壁球運動速度
鍵盤使用
Pygame采用事件來對應鍵盤操作
獲取事件將得到鍵盤輸入
不同按鍵編寫操作函數即可

源代碼:
1 # Pygame壁球小游戲(操控型) 2 import pygame,sys 3 4 pygame.init() 5 size = width, height = 600, 400 6 speed = [1,1] 7 BLACK = 0, 0, 0 8 screen = pygame.display.set_mode(size) 9 pygame.display.set_caption("Pygame壁球") 10 ball = pygame.image.load("PYG02-ball.gif") 11 ballrect = ball.get_rect() 12 fps = 300 13 fclock = pygame.time.Clock() 14 15 while True: 16 for event in pygame.event.get(): 17 if event.type == pygame.QUIT: 18 sys.exit() 19 elif event.type == pygame.KEYDOWN: 20 if event.key == pygame.K_LEFT: 21 speed[0] = speed[0] if speed[0] == 0 else (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0])) 22 elif event.key == pygame.K_RIGHT: 23 speed[0] = speed[0] + 1 if speed[0] > 0 else speed[0] - 1 24 elif event.key == pygame.K_UP: 25 speed[1] = speed[1] + 1 if speed[1] > 0 else speed[1] - 1 26 elif event.key == pygame.K_DOWN: 27 speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1])) 28 ballrect = ballrect.move(speed) 29 if ballrect.left < 0 or ballrect.right > width: 30 speed[0] = - speed[0] 31 if ballrect.top < 0 or ballrect.bottom > height: 32 speed[1] = - speed[1] 33 34 screen.fill(BLACK) 35 screen.blit(ball, ballrect) 36 pygame.display.update() 37 fclock.tick(fps)
