<!doctype html>Python游戲開發入門
Python游戲開發入門
Pygame簡介與安裝
1.Pygame安裝
pip install pygame
2.檢測pygame是否安裝成功
python -m pygame.examples.aliens
Pygame最小開發框架及最小游戲
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame壁球")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
壁球小游戲(展示型)與圖像的基本使用
screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
pygame.image.load(filename):將filename路徑下的圖像載入游戲,支持JPG,PNG,GIF(非動畫)等13種常用圖片格式
Surface對象:
ball.get_rect()
Pygame使用內部的定義的Surface對象表示所有載入的圖像,其中get_rect()方法返回一個覆蓋圖像的矩形Rect對象.
Rect對象:
Rect對象有一些重要的屬性,例如:top,bottom,left,right表示上下左右
width,height表示寬度和高度
ballrect.move(x,y)
矩形移動一個偏移量(x,y),即在橫軸方向移動x像素,縱軸方向移動y像素,(x,y為整數)
screen.fill(color)
顯示窗口背景填充為color顏色,采用RGB色彩體系.由於壁球不斷運動,運動后原有位置將默認填充白色,因此需要不斷刷新背景顏色.
screen.blit(src,dest)
將一個圖像繪制在另一個圖像上,即將src繪制到dest位置上.通過Rect對象引導對壁球的繪制.
壁球小游戲(節奏型)與屏幕的幀率設置
需求:壁球可以按照一定的速度運動
import pygame,sys
pygame.init()
size = width,height=1200,800
speed = [1,1]
BLACK =0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 600
fclock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
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]
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
fps = 600 #Frames per Secends 每秒幀率
fclock = pygame.time.Clock()
pygame.time.Clock()
創建一個Clock對象,用於操作時間
clock.tick(framerate)
控制幀速度,即窗口刷新速度,例如:clock.tick(100)表示每秒鍾100次幀刷新,視頻中每次展示的靜態圖像稱為幀.
壁球小游戲(操控型)與鍵盤的基本使用
鍵盤使用:如何獲取鍵盤的操作事件
熟讀調節:根據對應按鍵調節壁球運動速度
import pygame,sys
pygame.init()
size = width,height=600,400
speed = [1,1]
BLACK =0,0,0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()
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
if event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[0] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
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]
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
Pygame屏幕繪制機制
游戲全屏
游戲屏幕大小可調節
游戲屏幕無邊框
更改游戲標題欄內容
屏幕尺寸和模式
pygame.display.set_mode() 設置相關屏幕模式
pygame.display.Info() 生成屏幕相關信息
窗口標題和圖標
pygame.display.set_caption() 生成標題信息
pygame.display.set_icon() 設置圖標信息
pygame.display.get_caption() 獲得圖標
圖標感知與刷新
pygame.display.get_active()
pygame.display.flip()
pygame.display.update()
Pygame屏幕尺寸和模式設置
pygame.display.set_mode(r=(0,0),flags=0) 設置相關屏幕模式
r是游戲屏幕的分辨率,采用(width,height)方式輸入
flags用來控制分辨率,可用|組合使用,常用顯示標簽如下:
pygame.RESIZABLE 窗口大小可調
注意:大小可調時要有尺寸變化的響應
對擴張顯示頁面的刷新
對擴張/縮小顯示界面的游戲響應
pygame.NOFRAME 窗口沒有邊界顯示
pygame.FULLSCREEN 窗口全屏顯示
注意:每種顯示方式要配合相應的處理機制
pygame.display.Info() 生成屏幕相關信息
產生一個顯示信息對象VideoInfo,表達當前屏幕的參數信息,在.set_mode()之前調用,則顯示當前系統顯示參數信息
參數很多,其中有兩個十分重要,如下:
current_w: 當前顯示模式或窗口的像素寬度
current_h: 當前顯示模式或窗口的像素高度
import pygame,sys
pygame.init()
gInfo = pygame.display.Info()
size = width,height=gInfo.current_w,gInfo.current_h
speed = [1,1]
BLACK =50,50,20
screen = pygame.display.set_mode(size,pygame.FULLSCREEN)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 600
fclock = pygame.time.Clock()
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[0] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
elif event.key == pygame.K_ESCAPE:
sys.exit()
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]
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
Pygame.VIDEORESIZE:這是一種窗口大小更改的事件,事件發生后,返回event.size元組,包含新窗口的寬度和高度
.size[0] 寬度,也可以使用event.w
.size[1] 高度,也可以使用event.h
返回參數僅在事件發生時有用
elif event.type == pygame.VIDEORESIZE:
size = width,height = event.w,event.h
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
Pygame窗口標題和圖標設置
pygame.display.set_caption(title,icontitle=None)
title設置窗口的標題內容
icontitle設置圖標化后的小標題,小標題可選,部分系統沒有
pygame.display.get_caption()
返回當前設置窗口的標題及小標題內容
返回結構為(title,icontitle)
該函數與游戲交互邏輯配合,可以根據游戲情節修改標題內容
pygame.display.set_icon()
設置窗口的圖標效果
圖標是一個Surface對象
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
Pygame窗口感知和刷新運用
pygame.display.get_active()
當窗口在系統中顯示(屏幕繪制/非圖標化)時返回True,否則返回False;
該函數可以用來判斷是否游戲窗口被最小化;
進一步,判斷后可以暫停游戲,或改變游戲響應模式
pygame.display.flip() 整個屏幕重新繪制
pygame.display.update() 僅重新繪制發生改變的部分,
Pygame事件處理機制
響應用戶鍵盤,鼠標等外設操作
響應屏幕的尺寸和模式變化
響應游戲情節的特定觸發條件
產生一些觸發事件......
Pygame事件隊列
緩存並派發所有事件
用戶對事件逐一處理
原則上先到先處理
pygame.event.EventType
本質上是一種封裝后的數據類型(對象),是Pygame的一個類,表示事件類型
事件類型只有屬性,沒有方法.
用戶可以自定義新的事件類型
Pygame提供了6種類型的事件及相應的屬性
系統事件
事件類型 | 屬性 | |
---|---|---|
系統事件 | QUIT | none |
ACTIVEEVENT | gain,state | |
鍵盤事件 | KEYDOWN | unicode,key,mod |
KEYUP | key,mod | |
鼠標事件 | MOUSEMOTION | pos,rel,buttons |
MOUSEBUTTONUP | pos,button | |
MOUSEBUTTONDOWN | pos,button | |
游戲桿 | JOYAXISMOTION | joy,axis,value |
JOYBALLMOTION | joy,ball,rel | |
JOYHATMOTION | joy,hat,value | |
JOYBUTTONUP | joy,button | |
JOYBUTTONDOWN | joy,button | |
窗口 | VIDEORESIZE | size,w,h |
VIDEOEXPOSE | none | |
用戶定義 | USEREVENT | code |
實例:鍵盤落下事件及屬性
pygame.event.KEYDOWN
event.unicode
event.key
event.mod
處理事件
pygame.event.get()
pygame.event.poll()
pygame.event.clear()
操作事件隊列
pygame.event.set_blocked()
pygame.event.get_blocked()
pygame.event.set_allowed()
生成事件
pygame.event.post()
pygame.event.Event()
鍵盤事件及類型的使用
鍵盤按下事件 pygame.event.KEYDOWN
event.unicode 按鍵的Unicode碼
注意:Unicode碼與平台相關,不推薦使用
event.key 按鍵的常量名稱
event.mod 按鍵修飾符的組合值
鍵盤釋放事件 pygame.event.KEYUP
event.key
event.mod 修飾符的按位或運算
event.mod = KMOD_ALT | KMOD_SHIFT
import pygame,sys
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame事件處理")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.unicode == "":
print("[KEYDOWN]:","#",event.key,event.mod)
else:
print("[KEYDOWN]:", event.unicode, event.key, event.mod)
pygame.display.update()
鼠標事件及類型的使用
pygame.event.MOUSEMOTION 鼠標移動事件
event.pos 鼠標當前坐標值(x,y),相對於窗口左上角
event.rel 鼠標相對運動距離(X,Y),相對於上次事件
event.buttons 鼠標按鈕狀態(a,b,c),相對於鼠標的三個鍵,鼠標移動時,這三個鍵處於按下狀態,對應的位置值為1,反之則為0.
pygame.event.MOUSEBUTTONUP 鼠標鍵釋放事件
event.pos 鼠標當前坐標值(x,y),相對於窗口左上角
event.button 鼠標按下鍵編號n,取值1/2/3,分別對應三個鍵
pygame.event.MOUSEBUTTONDOWN 鼠標鍵按下事件
event.pos 鼠標當前坐標值(x,y),相對於窗口左上角
event.button 鼠標按下鍵編號n,取值為整數,左鍵為1,中鍵為2,右鍵為3,設備相關
壁球小游戲(鼠標型)
import pygame,sys
pygame.init()
size = width,height=1200,800
speed = [1,1]
BLACK =50,50,20
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 600
fclock = pygame.time.Clock()
still = False
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
if event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[0] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
elif event.type == pygame.VIDEORESIZE:
size = width,height = event.w,event.h
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)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0],speed[1])
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]
screen.fill(BLACK)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
Pygame事件處理函數
處理事件
pygame.event.get()
從事件隊列中獲得事件列表,即獲得所有被隊列的事件
for event in pygame.event.get():
ifevent.type == pygame.QUIT:
sys.exit()
pygame.event.poll()
從事件隊列中獲得一個事件,事件獲取后將從事件隊列中刪除,如果事件隊列為空,則返回 event.NOEVENT
while True:
event = pygame.event.poll()
pygame.event.clear()
從事件隊列中刪除事件,默認刪除所有事件
該函數與pygame.event.get()類似,區別僅是不對事件事件進行處理
可以增加參數,刪除某類或某些事件:
pygame.event.clear(type)
pygame.event.clear(typelist)
事件隊列同時僅能存儲128個事件,當事件隊列滿時,更多事件將被丟棄
操作事件隊列
設置事件隊列能夠緩存事件的類型
pygame.event.set_blocked(type or typelist)
控制哪些事件不允許被保存到事件隊列中
pygame.event.get_blocked(type)
測試某個事件類型是否被事件隊列所禁止,如果事件類型被禁止,則返回True,否則返回False
pygame.event.set_allowed(type or typelist)
控制哪些事件允許被保存到事件隊列中
生成事件
pygame.event.post(Event)
產生一個事件,並將其放入事件隊列
一般用於放置用戶自定義事件(pygame.USEREVENT)
也可用於放置系統定義事件(如鼠標或鍵盤等),給定參數
pygame.event.Event(type,dict)
創建一個給定類型的事件
其中,事件的屬性和值采用字典類型復制,屬性名采用字符串形式,如果創建已有事件,屬性需要一致
Pygame色彩機制
色彩表達 Pygame.Color
Color類用於表達色彩,使用RGB或RGBA色彩模式,A可選
Color類可以用色彩名字,RGBA值,HTML色彩格式等方式定義.
Color(name) 例如:Color("grey")
Color(r,g,b,a) 例如:Color(190,190,190,255)
Color(rgbvalue) 例如:Color("#BEBEBEFF")
pygame.Color.r 獲得Color類的紅色值r
pygame.Color.g 獲得Color類的綠色值g
pygame.Color.b 獲得Color類的藍色值b
pygame.Color.a 獲得Color類的alpha值a
pygame.Color.normalize 將RGBA各通道值歸一到0~1之間
需求:根據壁球運動改變背景顏色
import pygame,sys
pygame.init()
size = width,height=1200,800
speed = [1,1]
BLACK =50,50,20
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
icon = pygame.image.load("PYG03-flower.png")
pygame.display.set_icon(icon)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.gif")
ballrect = ball.get_rect()
fps = 600
fclock = pygame.time.Clock()
still = False
bgcolor = pygame.Color("black")
def RGBChannel(a):
return 0 if a<0 else (255 if a>255 else int(a))
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
if event.key == pygame.K_DOWN:
speed[1] = speed[1] if speed[0] == 0 else (abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
elif event.type == pygame.VIDEORESIZE:
size = width,height = event.w,event.h
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)
if pygame.display.get_active() and not still:
ballrect = ballrect.move(speed[0],speed[1])
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],1))
screen.fill(bgcolor)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
Pygame圖形繪制機制
pygame.draw
圖形繪制后,返回一個矩形Rect類表示該形狀
pygame.Rect
表達一個矩形區域的類,用於存儲坐標和長度信息,矩形的左上角坐標以及長度寬度
Pygame利用Rect類來操作圖形/圖像元素
Rect類提供了如下屬性,返回一個數值或一個代表坐標的元組
x,y,w,h,size,width,height
top,left,bottom,right
topleft,bottomleft,topright,bottomright
midtop,midleft,midbottom,midright
center,centerx,centery
Rect類提供了如下的方法,用來操作Rect類
.copy(),.move(),.inflate(),.clamp(),.clip(),
.union(),.unionall(),.fit(),
.normalize(),.contains(),.collidepoint()
.colliderect(),.collidelist(),.collidelistall(),
.collidedict(),.collidedictall()
pygame.draw
.rect() 矩形, .line() 直線
.polygon() 多邊形, .lines() 連續多線
.circle() 圓形, .aaline() 無鋸齒線
.ellipse() 橢圓形, .aalines() 連續無鋸齒線
.arc() 橢圓弧形
pygame.draw.rect(Surface,color,Rect,width=0)
#Surface 矩形的繪制屏幕
#Color 矩形的繪制顏色
#Rect 矩形的繪制區域
#width=0 繪制邊緣的寬度,默認為0,即填充圖形
pygame.draw.polygon(Surface,color,pointlist,width=0)
#Surface 多邊形的繪制屏幕
#Color 多邊形的繪制顏色
#pointlist 多邊形頂點坐標列表
#width=0 繪制邊緣的寬度,默認為0,即填充圖形
pygame.draw.circle(Surface,color,pos,radius,width=0)
#Surface 圓形的繪制屏幕
#Color 圓形的繪制顏色
#pos 圓形圓心坐標
#radius 圓形半徑
#width=0 繪制邊緣的寬度,默認為0,即填充圖形
pygame.draw.ellipse(Surface,color,Rect,width=0)
#Surface 橢圓形的繪制屏幕
#Color 橢圓形的繪制顏色
#Rect 橢圓形的繪制區域(橢圓形外切矩形)
#width=0 繪制邊緣的寬度,默認為0,即填充圖形
pygame.draw.arc(Surface,color,Rect,start_angle,stop_angle,width=0)
#Surface 橢圓弧線的繪制屏幕
#Color 橢圓弧線的繪制顏色
#Rect 橢圓弧線的繪制區域
#start_angle橢圓弧線繪制的起始角度(橫向右側為0度)
#stop_angle 橢圓弧線繪制的結束角度
#width=0 繪制邊緣的寬度,默認為0,即填充圖形
pygame.draw.line(Surface,color,start_pos,stop_pos,width=1)
#Surface 直線的繪制屏幕
#Color 直線的繪制顏色
#start_pos 直線繪制的起始點
#stop_pos 直線繪制的結束點
#width=0 繪制邊緣的寬度,默認為1,
pygame.draw.lines(Surface,color,closed,pointlist,width=1)
#Surface 連續多線的繪制屏幕
#Color 連續多線的繪制顏色
#closed 如果為True,起止節點間自動增加閉合直線
#pointlist 連續多線的頂點坐標列表
#width=0 繪制邊緣的寬度,默認為1
pygame.draw.aaline(Surface,color,start_pos,stop_pos,blend=1)
#Surface 無鋸齒線的繪制屏幕
#Color 無鋸齒線的繪制顏色
#start_pos 無鋸齒線繪制的起始點
#stop_pos 無鋸齒線繪制的結束點
#blend=1 不為0時,與線條所在背景顏色進行混合
pygame.draw.aalines(Surface,color,closed,pointlist,blend=1)
#Surface 連續無鋸齒線的繪制屏幕
#Color 連續無鋸齒線的繪制顏色
#closed 如果為True,起止節點間自動增加閉合直線
#pointlist 連續無鋸齒線的頂點坐標列表
#blend=1 不為0時,與線條所在背景顏色進行混合
import pygame,sys
from math import pi
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame圖形繪制")
GOLD = 255,251,0
RED = pygame.Color("red")
WHITE = 255,255,255
GREEN = pygame.Color("green")
#r1rect = pygame.draw.rect(screen,GOLD,(100,100,200,100),5)
#r2rect = pygame.draw.rect(screen,RED,(210,210,200,100),0)
e1rect = pygame.draw.ellipse(screen,GREEN,(50,50,500,300),3)
c1rect = pygame.draw.circle(screen,GOLD,(200,180),50,5)
c2rect = pygame.draw.circle(screen,RED,(400,180),30)
plist = [(295,170),(285,250),(260,280),(340,280),(315,250),(305,170)]
l1rect = pygame.draw.lines(screen,GREEN,True,plist,2)
a1rect = pygame.draw.arc(screen,GOLD,(200,220,200,100),1.4*pi,1.9*pi,3)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
Pygame文字繪制機制
pygame.freetype
向屏幕上繪制特定字體的文字
文字不能直接print(),而是用像素根據字體點陣圖繪制
pygame.freetype額外import引用
import pygame,sys
import pygame.freetype
pygame.freetype.font
根據字體和字號生成一個Font對象
Font.render_to()
Font.render()
使用Font對象的render*方法繪制具體文字
Font類型
pygame.freetype.Font(file,size=0)
#file 字體類型名稱或路徑
#size 字體的大小
Font類的繪制方法
Font.render_to(surf,dest,text,fgcolor=None,bgcolor=None,rotation=0,size=0)
#返回一個rect類型
#surf 繪制字體的平面,Surface對象
#dest 在平面中具體的位置
#text 繪制的文字內容
#fgcolor文字顏色
#bgcolor背景顏色
#rotation逆時針的旋轉角度,取值0-359,部分字體可旋轉
#size 字體大小,賦值該參數嘉慶覆蓋Font中的設定值
import pygame,sys
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame文字繪制")
GOLD = 255,251,0
f1 = pygame.freetype.Font("C:\Windows\Fonts\ygyxsziti2.0.ttf",36)
f1.render_to(screen,(200,160),"Hello,World!",fgcolor=GOLD,size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.update()
Font.render(text,fgcolor=None,bgcolor=None,rotation=0,size=0)
#(Surface,Rect)
#text 繪制的文字內容
#fgcolor文字顏色
#bgcolor背景顏色
#rotation逆時針的旋轉角度,取值0-359,部分字體可旋轉
#size 字體大小,賦值該參數嘉慶覆蓋Font中的設定值
import pygame,sys
import pygame.freetype
pygame.init()
screen = pygame.display.set_mode((600,400))
pygame.display.set_caption("Pygame文字繪制")
GOLD = 255,251,0
f1 = pygame.freetype.Font("C:\Windows\Fonts\ygyxsziti2.0.ttf",36)
f1surf,f1rect=f1.render("世界和平!",fgcolor=GOLD,size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(f1surf,(200,160))
pygame.display.update()
Pygame繪圖機制原理精髓
pygame.Surface
繪圖層,或繪圖平面,或圖層
用於表示圖形,文字或圖像的繪制效果
與當前屏幕主圖層可以並列存在
如果不繪制在主圖層上,則不會被顯示
每一個游戲只有一個主圖層
pygame.Rect
矩形區域
對應於當前主圖層的某個具體區域
相當於某個矩形區域的指針或標識信息
可以指定圖層繪制在某個矩形區域中
主圖層
由pygame.display.set_mode()生成的Surface對象
size = width,height=1200,800
screen = pygame.display.set_mode(size)
在主圖層上繪制其他圖層是用blit()方法
screen.blit(ball,ballrect)
ball ---> pygame.Surface
ballrect---> pygame.Rect
壁球小游戲(文字型)
import pygame,sys
import pygame.freetype
pygame.init()
size = width,height=1200,800
speed = [1,1]
GOLD = 250,251,0
BLACK =50,50,20
pos = [230,160]
fps =300
fclock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字繪制")
#ball = pygame.image.load("PYG02-ball.gif")
#ballrect = ball.get_rect()
f1 = pygame.freetype.Font("C:\Windows\Fonts\hakuyoxingshu7000.TTF",36)
f1rect = f1.render_to(screen,pos,"壁球",fgcolor=GOLD,size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = -speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = -speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1]
screen.fill(BLACK)
#screen.blit(ball,ballrect)
f1rect = f1.render_to(screen,pos,"壁球",fgcolor=GOLD,size=50)
pygame.display.update()
fclock.tick(fps)
import pygame,sys
import pygame.freetype
pygame.init()
size = width,height=1200,800
speed = [1,1]
GOLD = 250,251,0
BLACK =50,50,20
pos = [230,160]
fps =300
fclock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame文字繪制")
#ball = pygame.image.load("PYG02-ball.gif")
#ballrect = ball.get_rect()
f1 = pygame.freetype.Font("C:\Windows\Fonts\hakuyoxingshu7000.TTF",36)
f1surf,f1rect = f1.render("壁球",fgcolor=GOLD,size=50)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if pos[0] < 0 or pos[0] + f1rect.width > width:
speed[0] = -speed[0]
if pos[1] < 0 or pos[1] + f1rect.height > height:
speed[1] = -speed[1]
pos[0] = pos[0] + speed[0]
pos[1] = pos[1] + speed[1]
screen.fill(BLACK)
#screen.blit(ball,ballrect)
f1surf, f1rect = f1.render("壁球", fgcolor=GOLD, size=50)
screen.blit(f1surf,(pos[0],pos[1]))
pygame.display.update()
fclock.tick(fps)