Python 遊戲之旅(Pygame)
阿新 • • 發佈:2018-11-25
Pygame是跨平臺Python模組,專為電子遊戲設計,包含影象、聲音。建立在SDL基礎上,允許實時電子遊戲研發而無需被低階語言(如機器語言和組合語言)束縛。基於這樣一個設想,所有需要的遊戲功能和理念都(主要是影象方面)都完全簡化為遊戲邏輯本身,所有的資源結構都可以由高階語言提供。
Pygame的程式設計其實可以理解為迴圈加事件實現。
安裝:
pip3 install pygame
測試:(可忽略)
python -m pygame.examples.aliens
畫一個厲害的畫玩玩:
生成一個最簡視窗:
1 import pygame # 匯入pygame庫 2 from sys import exit # 匯入sys庫中的exit函式 3 4 # 初始化pygame 5 pygame.init() 6 7 #建立一個視窗,引數((寬度,高度)) 8 screen = pygame.display.set_mode((700,450)) 9 # 設定視窗標題 10 pygame.display.set_caption("東小東遊戲視窗") 11 12 # 載入背景圖 13 background = pygame.image.load('imgx/wa.jpg')14 15 # 事件迴圈 16 while True: 17 # 繪製背景 18 screen.blit(background, (0, 0)) 19 20 # 更新螢幕 21 pygame.display.update() 22 23 # 所有事件的監聽,處理 24 for event in pygame.event.get(): 25 # 遊戲退出事件 26 if event.type == pygame.QUIT: 27 # 退出程式 28 exit()
畫畫實現:
實現文字顯示、事件監聽、畫大部分圖形,其中顯示文字時需要中文字型庫支援,可直接將對應的字型拷貝到目錄下,方便程式的移植,而不要固定寫Windows字型庫的路徑,所以相對路徑是最好的解決方法。
Windows文字型檔所在位置:C:\Windows\Fonts
1 import pygame # 匯入pygame庫 2 from sys import exit # 匯入sys庫中的exit函式 3 from math import pi 4 import pygame.freetype 5 6 # 初始化pygame 7 pygame.init() 8 #建立一個視窗,引數((寬度,高度)) 9 screen = pygame.display.set_mode((700,450)) 10 #設定視窗標題 11 pygame.display.set_caption("東小東遊戲視窗") 12 13 14 #顏色物件,引數(R,G,B,Alphal) 15 co1=pygame.Color(255,255,20,10) 16 co2=pygame.Color(255,0,0,255) 17 co3=pygame.Color(0,255,0,255) 18 cotextbg=pygame.Color(0,255,0,200) 19 cotext=pygame.Color(255,255,255,255) 20 21 # 填充背景色 22 screen.fill(co1) 23 24 #顯示文字方法1,需要一直重新整理 25 #繪製文字,引數(字型,預設大小) 26 #ftext111=pygame.font.SysFont("arial", 16) 27 #引數:(文字,是否取消鋸齒,前景色,背景色) 28 #textviewx111=ftext111.render("wwww",True,(100,100,50),(0,255,255,50)) 29 30 #繪製文字222,不能使用中文路徑,引數(字型,預設大小),返回矩形物件 31 ftext222=pygame.freetype.Font("fontx/fontext.ttf",20) 32 #方法2 33 #引數(視窗,左上角位置,文字,前景,背景,旋轉角度[0,359],大小),返回矩形物件 34 textrect222=ftext222.render_to(screen,(620,420),"東小東",fgcolor=cotext,bgcolor=cotextbg,rotation=0,size=20) 35 #方法3,需要一直重新整理,返回viwe和矩形物件 36 texview333,texrect333=ftext222.render("東小東2",fgcolor=cotext,bgcolor=cotextbg,rotation=20,size=50) 37 38 39 #------ 以下形狀都會返回矩形物件 -------- 40 # 繪製矩形,引數(視窗,顏色,(左上角x,左上角y,w,h),寬度),寬度為0 則填充 41 rectx = pygame.draw.rect(screen, (255,255,255), (330, 289, 30, 20), 0) 42 rectx = pygame.draw.rect(screen, (255,255,255), (380, 289, 30, 20), 0) 43 rectx = pygame.draw.rect(screen, co2, (270, 270, 200, 50), 5) 44 # 繪製圓形,引數(視窗,顏色,圓形座標(x,y),半徑,寬度) 45 circlex = pygame.draw.circle(screen, co2, (250, 200), 50, 0) 46 circlex = pygame.draw.circle(screen, co2, (450, 180), 50, 1) 47 48 # 繪製直線,引數(視窗,顏色,起點座標(x,y),結束點座標,寬度) 49 #linex = pygame.draw.line(screen, co2, (20, 20), (100, 30), 1) 50 # 無鋸齒線,斜線去掉鋸齒 51 #aalinex = pygame.draw.aaline(screen, co2, (30, 30), (100, 50), 1) 52 53 # 橢圓,使用外切矩形畫,引數(視窗,顏色,矩形的(左上角x,左上角y,w,h),寬度 54 ell = pygame.draw.ellipse(screen, co2, (100, 40, 500, 350), 1) 55 56 # 繪製多線,第一的結束點必然會連線第二的起始點... 57 # 列表引數為:第一條直線起始點,第一條直線結束點,第二條..... 58 #listline = [(10, 100), (10, 200), (20, 100), (20, 300)] 59 # 引數false表示結束點是否要連線最開始的起始點 60 #linexss = pygame.draw.lines(screen, co2, False, listline, 2) 61 62 # 繪製弧線 63 alrectx = pygame.draw.arc(screen, co3, (344, 244, 60, 20), 0 * pi, 1 * pi, 3) 64 65 # 事件迴圈 66 while True: 67 #顯示文字方法1 68 #screen.blit(textviewx111,(20,20)) 69 #顯示文字方法3 70 #screen.blit(texview333,(200,200)) 71 72 # 更新螢幕 73 pygame.display.update() 74 # 所有事件的監聽,處理 75 for event in pygame.event.get(): 76 # 遊戲退出事件 77 if event.type == pygame.QUIT: 78 # 退出程式 79 exit()
壁球小遊戲基本實現及Pygame的其他內容補充:
1 import pygame # 匯入pygame庫 2 from sys import exit # 匯入sys庫中的exit函式 3 # 初始化pygame 4 pygame.init() 5 6 7 #定義窗體的寬高 8 winWidth=700 9 winHeight=450 10 11 #定義圖片每次移動的x,y軸畫素 12 ballX=1 13 ballY=1 14 15 #重新整理最大幀速率,while迴圈的速度 16 fpsx=19 17 18 #初始化幀速率物件 19 timefpsx=pygame.time.Clock() 20 21 #全屏顯示需呼叫函式 22 #獲取到電腦螢幕的大小並賦值給視窗大小 23 def setfull(): 24 global winHeight,winWidth 25 winRoot=pygame.display.Info() 26 winWidth=winRoot.current_w 27 winHeight=winRoot.current_h 28 29 30 31 #建立一個視窗,引數((寬度,高度),顯示模式) 32 #顯示模式 33 34 mod=pygame.RESIZABLE #螢幕大小可調,需監聽pygame.VIDEORESIZE事件 35 36 #mod=pygame.NOFRAME #無邊框 37 38 #mod=pygame.FULLSCREEN #全屏,需開啟下面setfull函式 39 #setfull() 40 41 screen = pygame.display.set_mode((winWidth,winHeight),mod) 42 43 # 設定視窗標題及標題內容 44 pygame.display.set_caption("東小東遊戲視窗--壁球") 45 #設定標題圖片 46 titleiconx=pygame.image.load("imgx/yzm.jpg") 47 pygame.display.set_icon(titleiconx) 48 49 # 載入背景圖 50 backgroundx = pygame.image.load('imgx/wa.jpg') 51 52 #載入需要移動的圖片 53 ballimgx=pygame.image.load("imgx/ball.png") 54 #生成與物件外切的矩形,生成的矩形物件可以獲取到x,y,寬高的資訊 55 ballrectx=ballimgx.get_rect() 56 57 58 # 事件迴圈 59 while True: 60 #設定幀速率 61 timefpsx.tick(fpsx) 62 # 繪製背景 63 screen.blit(backgroundx, (0, 0)) 64 65 #判斷視窗是否被顯示,如果最小化則為false 66 if pygame.display.get_active(): 67 #移動圖片,引數(x軸每次移動畫素,y軸每次移動畫素) 68 ballrectx=ballrectx.move(ballX,ballY) 69 70 #判斷圖片是否移動到邊緣,達到邊緣時將移動的X或Y方向取反 71 if ballrectx.left<0 or ballrectx.right>winWidth: 72 ballX=-ballX 73 if ballrectx.top<0 or ballrectx.bottom>winHeight: 74 ballY=-ballY 75 76 #繪製移動的圖片 77 screen.blit(ballimgx,ballrectx) 78 79 # 更新螢幕 80 pygame.display.update() 81 82 83 # 所有事件的監聽(事件佇列只能快取128個事件),處理 84 for event in pygame.event.get(): 85 # 遊戲退出事件 86 if event.type == pygame.QUIT: 87 # 退出程式 88 exit() 89 90 #滑鼠事件 91 #滑鼠按下事件 92 if event.type == pygame.MOUSEBUTTONDOWN: 93 print("滑鼠按下,座標為:",event.pos,",滑鼠按下鍵為:",event.button) 94 #滑鼠擡起事件 95 if event.type == pygame.MOUSEBUTTONUP: 96 print("滑鼠擡起") 97 #滑鼠移動 98 if event.type ==pygame.MOUSEMOTION: 99 print("滑鼠移動中......當前座標:",event.pos,"相對位置:",event.rel,"滑鼠三鍵狀態:",event.buttons) 100 101 #鍵盤按鍵事件,字母只會輸出小寫的編碼 102 #鍵盤按下按鍵事件 103 if event.type ==pygame.KEYDOWN: 104 print("鍵盤按下鍵為:",event.key,"按鍵按下的模式標誌為:",event.mod) 105 #如果按下ESC鍵則退出程式 106 if event.key==27: 107 exit() 108 #遊戲處理:上(273)、下(274)、左(276)、右(275) 109 #實現按下哪個方向按鍵即向哪個方向移動 110 if event.key == 273: 111 ballY =-1*abs(ballY) 112 elif event.key == 274: 113 ballY=abs(ballY) 114 elif event.key ==276: 115 ballX=-1*abs(ballY) 116 elif event.key ==275: 117 ballX=abs(ballX) 118 if event.key == 32:#空格鍵,使無邊框 119 screen = pygame.display.set_mode((winWidth, winHeight),pygame.NOFRAME) 120 121 if event.type == pygame.KEYUP: 122 print("鍵盤按鍵擡起:",event.key) 123 124 125 126 if event.type ==pygame.VIDEORESIZE: 127 print("螢幕大小有改變:",event.size) 128 winWidth=event.size[0] 129 winHeight=event.size[1] 130 screen = pygame.display.set_mode((winWidth, winHeight), mod)
自定義事件處理:
可繫結標準事件,但傳遞引數不一定需要標準,可在標準事件捕獲到自定義事件:
1 eventx=pygame.event.Event(pygame.KEYDOWN,{"ww":33}) 2 pygame.event.post(eventx) 3 4 # 所有事件的監聽,處理 5 for event in pygame.event.get(): 6 if event.type ==pygame.KEYDOWN:#鍵盤按鍵事件 7 print(event.ww) #輸出33
Pygame官網:https://www.pygame.org/docs/
參考:嵩天教授的Python遊戲開發教程(pygame)