使用PyGame顯示圖像的四種方案實例代碼
安裝pygame
安裝 pygame 的最佳方法是使用 pip 工具(python 使用它來安裝包)。請注意,在最新版本中,這與 python 一起提供。我們使用 –user 標志告訴它安裝到主目錄,而不是全局。
python -m pip install -U pygame --user
要查看它是否有效,請運行包含的示例之一:
python -m pygame.examples.aliens
如果可行,我們就可以開始了!
在pygame窗口上顯示圖像有四個基本步驟:
- 使用pygame 的display.set_mode()方法創(chuàng)建一個顯示表面對象。
- 使用 pygame 的 image.load() 方法創(chuàng)建一個 Image 表面對象,即在其上繪制圖像的表面對象。
- 使用pygame顯示表面對象的blit()方法將圖像表面對象復制到顯示表面對象。
- 使用 pygame 的display.update()方法在 pygame 窗口上顯示顯示表面對象。
使用 PyGame 顯示圖像
在這里,我們首先導入所需的庫,然后設置圖像的寬度和高度,然后創(chuàng)建該尺寸的顯示表面,然后在 image.load() 函數(shù)中給出所需圖像的路徑,最后遍歷列表事件對象。
# importing required library import pygame # activate the pygame library . pygame.init() X = 800 Y = 800 # create the display surface object # of specific dimension..e(X, Y). scrn = pygame.display.set_mode((X, Y)) # set the pygame window name pygame.display.set_caption('image') # create a surface object, image is drawn on it. imp = pygame.image.load("avatar.jpeg").convert() # Using blit to copy content from one surface to other scrn.blit(imp, (0, 0)) # paint screen one time pygame.display.flip() status = True while (status): # iterate over the list of Event objects # that was returned by pygame.event.get() method. for i in pygame.event.get(): # if event object type is QUIT # then quitting the pygame # and program both. if i.type == pygame.QUIT: status = False # deactivates the pygame library pygame.quit()
總結
到此這篇關于使用PyGame顯示圖像的四種方案的文章就介紹到這了,更多相關PyGame顯示圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python 中 AttributeError: ‘NoneType‘ obje
Python “AttributeError: ‘NoneType’ object has no attribute” 發(fā)生在我們嘗試訪問 None 值的屬性時,例如 來自不返回任何內(nèi)容的函數(shù)的賦值, 要解決該錯誤,請在訪問屬性之前更正分配,本文通過示例給大家說明錯誤是如何發(fā)生的,感興趣的朋友一起看看吧2023-08-08淺談python標準庫--functools.partial
這篇文章主要介紹了python標準庫--functools.partial,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03