1 說明:
=====
1.1 pygame顯示圖片的方法,熟悉pygame的各種參數(shù),動(dòng)畫設(shè)置,由淺入深,小白秒懂,值得收藏。
1.2 圖片來源:今日頭條正版免費(fèi)圖庫(kù),向女神高圓圓致敬,僅供學(xué)習(xí)。
1.3 環(huán)境:python3.8+pygame 1.9.6+微軟編輯器vscode+深度操作系統(tǒng)deepin-linux。
gyy1.jpeg
gyy2.jpeg
gyy3.jpeg
2 基本圖片顯示:
============
2.1 代碼:
#第1步:模塊導(dǎo)入import pygame,sys#可調(diào)用參數(shù),比如:RESIZABLE,窗口大小可調(diào)節(jié)from pygame.locals import * #第2步:初始化pygame.init()#窗口標(biāo)題名:支持中文pygame.display.set_caption('show pic')#讀取或加載圖片img = pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg') #獲取位圖的寬和高width,height = img.get_size()#定義屏幕和設(shè)置窗口大小,這樣的寬和高的設(shè)置符合圖片大小screen = pygame.display.set_mode([width,height],RESIZABLE, 32)#背景顏色填充screen.fill([255,255,255])#顯示圖片和指定坐標(biāo)點(diǎn)0,0左上角開始screen.blit(img,[0,0])#更新整個(gè)待顯示的 Surface 對(duì)象到屏幕上#pygame.display.flip()#更新部分內(nèi)容顯示到屏幕上,如果沒有參數(shù),則與flip功能相同(上一條)pygame.display.update()#當(dāng)使用OpenGL的時(shí)候,不能使用update()來更新窗口,需要使用flip() 來更新#第3步:退出設(shè)置running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()
2.2 圖:簡(jiǎn)單,圖省略。
3 圖片縮小顯示:
============
3.1 代碼:
import pygame,sys#可調(diào)用參數(shù),比如:RESIZABLE,窗口大小可調(diào)節(jié)from pygame.locals import * pygame.init()pygame.display.set_caption('show pic')#讀取或加載圖片img = pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy3.jpeg')#縮小比率ratio=0.5#獲取位圖的寬和高width,height = img.get_size()#增加處# 獲得圖像的位置矩形img_rect = img.get_rect()#縮放50%比例顯示sizeimg = pygame.transform.smoothscale(img,\ (int(img_rect.width*ratio),\ int(img_rect.height*ratio)))#定義屏幕和設(shè)置窗口大小,這樣的寬和高的設(shè)置符合圖片大小screen = pygame.display.set_mode([width,height],RESIZABLE, 32)#背景顏色填充,白色填充screen.fill([255,255,255])#修改bug#screen.fill([0,0,0]) #改成黑色填空即可#screen.blit(img,[0,0])screen.blit(sizeimg,[0,0]) #修改處pygame.display.update()#退出設(shè)置running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()
3.2 操作效果圖:
3.3 附贈(zèng)知識(shí)點(diǎn):保真的圖片大小縮放修改,代碼。
# -*- coding: utf-8 -*-#不失真的圖片修改:放大和縮小from PIL import Imagedef ResizeImage(filein, fileout, width, height, type): ''' filein: 輸入圖片 fileout: 輸出圖片 width: 輸出圖片寬度 height:輸出圖片高度 type:輸出圖片類型(png, gif, jpeg...) ''' img = Image.open(filein) out = img.resize((width, height),Image.ANTIALIAS) out.save(fileout, type)#需要修改的圖片和路徑filein = '/home/xgj/Desktop/pygame-pic/gyy2.jpeg'#生成圖片和路徑fileout = '/home/xgj/Desktop/pygame-pic/gyy22.png'#需要修改的寬和高width = 200height = 120type1 = 'png'if __name__ == '__main__': ResizeImage(filein, fileout, width, height, type1)
===高級(jí)一點(diǎn)===
4 自動(dòng)從下往上移動(dòng)圖片:
===================
4.1 代碼:
#第1:導(dǎo)入模塊import pygameimport sys#第2步:初始化定義pygame.init()#創(chuàng)建游戲窗口 大小為2000 * 1200#繪制背景圖像screen = pygame.display.set_mode((2000,1200))#第3步:加載圖片#加載圖像數(shù)據(jù),背景圖片大小最好2000,1200,用上面代碼保真進(jìn)行圖片大小修改#注意不加背景圖片會(huì)出現(xiàn)上升圖片下的拉伸變形和圖片的一張一張疊加上升#注意python3已經(jīng)不需要rbg = pygame.image.load(r'/home/xgj/Desktop/pygame-pic/sky1.png')#通過blit來繪制圖像,從游戲窗口screen的(0,0)處開始繪制screen.blit(bg, (0,0))#加載圖片img = pygame.image.load(r'/home/xgj/Desktop/pygame-pic/gyy2.jpeg')width,height = img.get_size()#定義rect記錄移動(dòng)的圖片的初始位置#圖片的下面坐標(biāo)位置300,0img_rect = pygame.Rect(300, 0, width, height)#第4步:刷新和時(shí)鐘#在繪制完所有圖像后,再統(tǒng)一調(diào)用update方法pygame.display.update()#創(chuàng)建時(shí)鐘對(duì)象clock = pygame.time.Clock()#第5步:循環(huán)while True: # 遍歷所有的事件 for event in pygame.event.get(): # 如果單擊關(guān)閉窗口,則退出 if event.type == pygame.QUIT: sys.exit() #時(shí)鐘 clock.tick(60) #修改移動(dòng)圖片的位置 img_rect.y -= 1 #判斷移動(dòng)圖片位置 if img_rect.y <= 0: img_rect.y = 1200 #窗口的高 #調(diào)用blit方法繪制圖像 screen.blit(bg, (0,0))#在每次繪制移動(dòng)圖片之前,再將背景繪制一下 screen.blit(img, img_rect) #調(diào)用update方法更新顯示 pygame.display.update()pygame.quit()
4.2 背景圖自己下載,來自今日頭條免費(fèi)正版圖庫(kù),修改大小可用到上面保真圖片修改代碼。
4.3 效果圖:由于gif太大,不能上傳,故截屏。
5 按方向鍵調(diào)整圖片移動(dòng)方向:
======================
5.1 代碼:
#第1步:導(dǎo)入模塊import pygame,sys#第2步:加載圖片和初始化#讀取圖片img=pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg')#獲取圖片的寬和高picwidth,picheight = img.get_size()#初始化pygame.init()#窗口標(biāo)題screen=pygame.display.set_caption('按鍵盤方向鍵移動(dòng)圖片')#窗口大小winwidth=2000winheight=1200screen=pygame.display.set_mode([winwidth,winheight])#默認(rèn)字體和大小設(shè)置my_font=pygame.font.SysFont(None,22)#屏幕白色255,255,255screen.fill([0,0,0]) #背景顏色設(shè)置黑色#第3步:定義相關(guān)函數(shù)#定義函數(shù):加載圖片def loadimg(xloc,yloc): locationxy=[xloc,yloc] screen.blit(img,locationxy) pygame.display.flip()#定義函數(shù):加載文字def loadtext(xloc,yloc): textstr='location:'+str(xloc)+','+str(yloc) text_screen=my_font.render(textstr, True, (255, 0, 0)) #50和50是坐標(biāo),位于左上角 screen.blit(text_screen, (50,50))#第4步:定義主函數(shù)def main(): #文字加載位置 loadtext(310,0) #上下移動(dòng)參數(shù) looper=1200 #水平移動(dòng)參數(shù) shuip=2000 #循環(huán) while True: #退出設(shè)置 for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() #功能鍵定義 elif event.type == pygame.KEYDOWN: #按鍵↑功能定義 if event.key==pygame.K_UP: looper=looper-50 #pic的高 if looper<-picheight: #高 looper=winheight #按鍵↓功能定義 if event.key==pygame.K_DOWN: looper=looper+50 #高 if looper>winheight: looper=-picheight #按鍵←功能定義 if event.key == pygame.K_LEFT: shuip=shuip-50 #pic的寬 if shuip<-picwidth: shuip=winwidth #按鍵→功能定義 if event.key == pygame.K_RIGHT: shuip=shuip+50 if shuip>winwidth: shuip=0 #再次屏幕填充黑色 screen.fill([0,0,0]) loadtext(shuip,looper) loadimg(shuip,looper)#第5步:if __name__=='__main__': main()
5.2 效果圖:
聯(lián)系客服