https://www.bilibili.com/video/BV1XW411E7vb?p=1
一、python的IDE(集成開發(fā)環(huán)境)工具
IDE工具
二、pygame 模塊的安裝(以pycharm為例)以及簡單介紹
如果你的電腦使用pycharm編輯器,可以通過以下方式安裝:
pip install pygame
pip是python的包管理工具,就是第三方庫安裝、下載的管家。
Pygame是Python最經(jīng)典的2D游戲開發(fā)第三方庫,也支持3D游戲開發(fā)。
pygame適用于游戲邏輯驗證、游戲入門及系統(tǒng)演示驗證;pygame是一種游戲開發(fā)引擎,基本邏輯具有參考價值。
三、小試牛刀
1.pygame模塊中自帶演示小游戲
python -m pygame.examples.aliens
2.pygame最小開發(fā)框架
sys是Python的標(biāo)準(zhǔn)庫
sys提供Python運行時環(huán)境變量的操控,sys.exit()用于退出結(jié)束游戲并退出
#導(dǎo)入pygame和sys庫
import pygame
import sys
#初始化init()及設(shè)置
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame游戲之旅")
#獲取事件并逐類響應(yīng)>>>重復(fù)執(zhí)行
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
3.壁球小游戲(展示型)與圖像的基本使用
關(guān)鍵要素
小球的鏈接
https://python123.io/PY15/PYG02-ball.gif
笛卡爾坐標(biāo)系
分析
surface對象
Rect對象
矩形移動
壁球的反彈運動
背景顏色填充
讓圖像在另一個圖像上
#導(dǎo)入pygame和sys
import pygame,sys
#初始化設(shè)置
pygame.init()
#定義窗口寬度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景顏色
BLACK = 0,0,0
#設(shè)置窗口的寬度和高度
screen = pygame.display.set_mode(size)
#設(shè)置游戲的名稱
pygame.display.set_caption("Python壁球")
#導(dǎo)入圖片
ball = pygame.image.load("ball.png")
#獲得矩形圖形
ballrect = ball.get_rect()
#獲取事件并逐類響應(yīng)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#設(shè)置角色的偏移量,即在橫軸方向移動x像素,縱軸方向移動y像素,xy為整數(shù)
ballrect = ballrect.move(speed[0],speed[1])
#壁球的反彈運動
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top<0 or ballrect.bottom > height:
speed[1] = -speed[1]
#顯示窗口背景填充為color顏色,采用RGB色彩體系
screen.fill(BLACK)
#通過Rect對象引導(dǎo)對壁球的繪制
screen.blit(ball, ballrect)
#刷新屏幕
pygame.display.update()
4.壁球小游戲(節(jié)奏型)與屏幕的幀率設(shè)置
關(guān)鍵要素
操作時間
控制幀速度
#導(dǎo)入pygame和sys
import pygame,sys
#初始化設(shè)置
pygame.init()
#定義窗口寬度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景顏色
BLACK = 0,0,0
#設(shè)置窗口的寬度和高度
screen = pygame.display.set_mode(size)
#設(shè)置游戲的名稱
pygame.display.set_caption("Python壁球")
#導(dǎo)入圖片
ball = pygame.image.load("ball.png")
#獲得矩形圖形
ballrect = ball.get_rect()
fps = 300#每秒幀數(shù)參數(shù)
#創(chuàng)建一個Clock對象,用于操作時間
fclock = pygame.time.Clock()
#獲取事件并逐類響應(yīng)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#設(shè)置角色的偏移量,即在橫軸方向移動x像素,縱軸方向移動y像素,xy為整數(shù)
ballrect = ballrect.move(speed[0],speed[1])
#壁球的反彈運動
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top<0 or ballrect.bottom > height:
speed[1] = -speed[1]
#顯示窗口背景填充為color顏色,采用RGB色彩體系
screen.fill(BLACK)
#通過Rect對象引導(dǎo)對壁球的繪制
screen.blit(ball, ballrect)
#刷新屏幕
pygame.display.update()
fclock.tick(fps)
5.壁球小游戲(操控型)與鍵盤的基本使用
需求要素
鍵盤敲擊事件
#導(dǎo)入pygame和sys
import pygame,sys
#初始化設(shè)置
pygame.init()
#定義窗口寬度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景顏色
BLACK = 0,0,0
#設(shè)置窗口的寬度和高度
screen = pygame.display.set_mode(size)
#設(shè)置游戲的名稱
pygame.display.set_caption("Python壁球")
#導(dǎo)入圖片
ball = pygame.image.load("ball.png")
#獲得矩形圖形
ballrect = ball.get_rect()
fps = 300#每秒幀數(shù)參數(shù)
#創(chuàng)建一個Clock對象,用于操作時間
fclock = pygame.time.Clock()
#獲取事件并逐類響應(yīng)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]-1)*int(speed[0]/abs(speed[0])))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0]>0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] +1 if speed[1] > 0 else speed - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
#設(shè)置角色的偏移量,即在橫軸方向移動x像素,縱軸方向移動y像素,xy為整數(shù)
ballrect = ballrect.move(speed[0],speed[1])
#壁球的反彈運動
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top<0 or ballrect.bottom > height:
speed[1] = -speed[1]
#顯示窗口背景填充為color顏色,采用RGB色彩體系
screen.fill(BLACK)
#通過Rect對象引導(dǎo)對壁球的繪制
screen.blit(ball, ballrect)
#刷新屏幕
pygame.display.update()
fclock.tick(fps)
四、pygame屏幕繪制機制
pygame使用pygame.display來控制pygame的游戲屏幕,在pygame中只有一個游戲屏幕,如果我們想要切換到多個屏幕,我們只能在此屏幕消失后,生成新的屏幕。
使用笛卡爾坐標(biāo)系,左上方是坐標(biāo)原點,橫方向是x軸,縱方向是y軸。屏幕的尺寸以像素為單位。
屏幕繪制的重要函數(shù)
pygame.display.set_mode()可以設(shè)置游戲屏幕的尺寸,以及相關(guān)的模式。
屏幕模式函數(shù)
注意:每種顯示方式要配合相應(yīng)的處理機制
Pygame窗口標(biāo)題和圖標(biāo)設(shè)置
標(biāo)題和圖標(biāo)函數(shù)
pygame中surface對象是唯一的與圖像對應(yīng)的對象
pygame窗口感知和刷新設(shè)置
屏幕控制函數(shù)
flip()函數(shù)會重新繪制窗口,update()函數(shù)僅僅會繪制變化的地方。
五、pygame的事件處理機制
注意:unicode碼與平臺有關(guān),不推薦使用
鼠標(biāo)事件及類型的使用
move()函數(shù)的參數(shù)xy是相對位置
事件處理重要函數(shù)
在pygame的設(shè)計中,事件隊列只能存儲108個事件,會將所有新到的事件丟棄掉。
#每幀刷新一次,并產(chǎn)生一次事件
#導(dǎo)入pygame和sys庫
import pygame
import sys
#初始化init()及設(shè)置
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame事件處理")
fps = 1
fclock = pygame.time.Clock()
num = 1
#獲取事件并逐類響應(yīng)>>>重復(fù)執(zhí)行
while True:
uevent = pygame.event.Event(pygame.KEYDOWN,{"unicode":123,"key":pygame.K_SPACE,"mod":pygame.KMOD_ALT})
pygame.event.post(uevent)
num = num+1
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.unicode =="":
print("[KEYDOWN{}]".format(num),"#",event.key,event.mod)
else:
print("[KEYDOWN{}]".format(num),event.unicode,event.key,event.mod)
pygame.display.update()
fclock.tick(fps)
六.pygame的色彩與圖形繪制機制
色彩表達(dá)
一些常用顏色
#導(dǎo)入pygame和sys
import pygame,sys
#初始化設(shè)置
pygame.init()
#圖標(biāo)導(dǎo)入并設(shè)置圖標(biāo)
icon=pygame.image.load("flower.png")#load()函數(shù)生成的是surflower圖形對象,保存到變量里
pygame.display.set_icon(icon)
#定義窗口寬度和高度
size = width,height =600,400
#速度列表
speed = [1,1]
#配置背景顏色
BLACK = 0,0,0
#設(shè)置窗口的寬度和高度
screen = pygame.display.set_mode(size)
#設(shè)置游戲的名稱
pygame.display.set_caption("Python壁球")
#導(dǎo)入圖片
ball = pygame.image.load("ball.png")
#獲得矩形圖形
ballrect = ball.get_rect()
fps = 300#每秒幀數(shù)參數(shù)
#創(chuàng)建一個Clock對象,用于操作時間
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black")
#定義通道函數(shù)
def RGBChannel(a):
return 0 if a<0 else (255 if a>255 else int(a))
#獲取事件并逐類響應(yīng)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0]-1)*int(speed[0]/abs(speed[0])))
elif event.key == pygame.K_RIGHT:
speed[0] = speed[0] + 1 if speed[0]>0 else speed[0] - 1
elif event.key == pygame.K_UP:
speed[1] = speed[1] +1 if speed[1] > 0 else speed[1] - 1
elif event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[1] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
elif event.type == pygame.VIDEORESIZE:
size = width,height=event.size[0],event.size[1]
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
still = True
elif event.type == pygame.MOUSEBUTTONUP:
still = False
if event.button == 1:
ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
ballrect = ballrect.move(event.pos[0]-ballrect.left,event.pos[1]-ballrect.top)
#將屏幕控制函數(shù)
if pygame.display.get_active() and not still:
#設(shè)置角色的偏移量,即在橫軸方向移動x像素,縱軸方向移動y像素,xy為整數(shù)
ballrect = ballrect.move(speed[0],speed[1])#move()函數(shù)處理的是相對位置
#壁球的反彈運動
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.right>width and ballrect.right + speed[0]>ballrect.right:
speed[0] = -speed[0]
if ballrect.top<0 or ballrect.bottom > height:
speed[1] = -speed[1]
if ballrect.bottom>height and ballrect.bottom + speed[1] > ballrect.bottom:
speed[1] = -speed[1]
#建立顏色通道
bgcolor.r = RGBChannel(ballrect.left*255/width)
bgcolor.g = RGBChannel(ballrect.top*255/height)
bgcolor.b = RGBChannel(min(speed[0],speed[1])*255/max(speed[0],speed[1]))
#顯示窗口背景填充為color顏色,采用RGB色彩體系
# screen.fill(BLACK)
screen.fill(bgcolor)
#通過Rect對象引導(dǎo)對壁球的繪制
screen.blit(ball, ballrect)
#刷新屏幕
pygame.display.update()
fclock.tick(fps)
pygame的文字繪制機制
freetype()是繪制文字的增強方法,我們必須使用import將它引用