电竞比分网-中国电竞赛事及体育赛事平台

分享

女神高圓圓,python的pygame圖片多種顯示

 老友mk09qda3vs 2020-09-22

1 說明:

=====

1.1 pygame顯示圖片的方法,熟悉pygame的各種參數(shù),動畫設置,由淺入深,小白秒懂,值得收藏。

1.2 圖片來源:今日頭條正版免費圖庫,向女神高圓圓致敬,僅供學習。

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步:模塊導入import pygame,sys#可調用參數(shù),比如:RESIZABLE,窗口大小可調節(jié)from pygame.locals import * #第2步:初始化pygame.init()#窗口標題名:支持中文pygame.display.set_caption('show pic')#讀取或加載圖片img = pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg') #獲取位圖的寬和高width,height = img.get_size()#定義屏幕和設置窗口大小,這樣的寬和高的設置符合圖片大小screen = pygame.display.set_mode([width,height],RESIZABLE, 32)#背景顏色填充screen.fill([255,255,255])#顯示圖片和指定坐標點0,0左上角開始screen.blit(img,[0,0])#更新整個待顯示的 Surface 對象到屏幕上#pygame.display.flip()#更新部分內容顯示到屏幕上,如果沒有參數(shù),則與flip功能相同(上一條)pygame.display.update()#當使用OpenGL的時候,不能使用update()來更新窗口,需要使用flip() 來更新#第3步:退出設置running = Truewhile running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = Falsepygame.quit()

2.2 圖:簡單,圖省略。

3 圖片縮小顯示:

============

3.1 代碼:

import pygame,sys#可調用參數(shù),比如:RESIZABLE,窗口大小可調節(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)))#定義屏幕和設置窗口大小,這樣的寬和高的設置符合圖片大小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()#退出設置running = Truewhile running:    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = Falsepygame.quit()

3.2 操作效果圖:

女神高圓圓,python的pygame圖片多種顯示

3.3 附贈知識點:保真的圖片大小縮放修改,代碼。

# -*- 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)

===高級一點===

4 自動從下往上移動圖片:

===================

4.1 代碼:

#第1:導入模塊import pygameimport sys#第2步:初始化定義pygame.init()#創(chuàng)建游戲窗口 大小為2000 * 1200#繪制背景圖像screen = pygame.display.set_mode((2000,1200))#第3步:加載圖片#加載圖像數(shù)據(jù),背景圖片大小最好2000,1200,用上面代碼保真進行圖片大小修改#注意不加背景圖片會出現(xiàn)上升圖片下的拉伸變形和圖片的一張一張疊加上升#注意python3已經不需要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記錄移動的圖片的初始位置#圖片的下面坐標位置300,0img_rect = pygame.Rect(300, 0, width, height)#第4步:刷新和時鐘#在繪制完所有圖像后,再統(tǒng)一調用update方法pygame.display.update()#創(chuàng)建時鐘對象clock = pygame.time.Clock()#第5步:循環(huán)while True:    # 遍歷所有的事件    for event in pygame.event.get():        # 如果單擊關閉窗口,則退出        if event.type == pygame.QUIT:            sys.exit()    #時鐘    clock.tick(60)    #修改移動圖片的位置    img_rect.y -= 1    #判斷移動圖片位置    if img_rect.y <= 0:        img_rect.y = 1200 #窗口的高    #調用blit方法繪制圖像    screen.blit(bg, (0,0))#在每次繪制移動圖片之前,再將背景繪制一下    screen.blit(img, img_rect)    #調用update方法更新顯示    pygame.display.update()pygame.quit()

4.2 背景圖自己下載,來自今日頭條免費正版圖庫,修改大小可用到上面保真圖片修改代碼。

4.3 效果圖:由于gif太大,不能上傳,故截屏。

女神高圓圓,python的pygame圖片多種顯示

5 按方向鍵調整圖片移動方向:

======================

5.1 代碼:

#第1步:導入模塊import pygame,sys#第2步:加載圖片和初始化#讀取圖片img=pygame.image.load('/home/xgj/Desktop/pygame-pic/gyy1.jpeg')#獲取圖片的寬和高picwidth,picheight = img.get_size()#初始化pygame.init()#窗口標題screen=pygame.display.set_caption('按鍵盤方向鍵移動圖片')#窗口大小winwidth=2000winheight=1200screen=pygame.display.set_mode([winwidth,winheight])#默認字體和大小設置my_font=pygame.font.SysFont(None,22)#屏幕白色255,255,255screen.fill([0,0,0]) #背景顏色設置黑色#第3步:定義相關函數(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是坐標,位于左上角 screen.blit(text_screen, (50,50))#第4步:定義主函數(shù)def main(): #文字加載位置 loadtext(310,0) #上下移動參數(shù) looper=1200 #水平移動參數(shù) shuip=2000 #循環(huán) 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_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 效果圖:

女神高圓圓,python的pygame圖片多種顯示

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多