網站首頁 編程語言 正文
安裝pygame
安裝 pygame 的最佳方法是使用 pip 工具(python 使用它來安裝包)。請注意,在最新版本中,這與 python 一起提供。我們使用 –user 標志告訴它安裝到主目錄,而不是全局。
python -m pip install -U pygame --user
要查看它是否有效,請運行包含的示例之一:
python -m pygame.examples.aliens
如果可行,我們就可以開始了!
在pygame窗口上顯示圖像有四個基本步驟:
- 使用pygame 的display.set_mode()方法創建一個顯示表面對象。
- 使用 pygame 的 image.load() 方法創建一個 Image 表面對象,即在其上繪制圖像的表面對象。
- 使用pygame顯示表面對象的blit()方法將圖像表面對象復制到顯示表面對象。
- 使用 pygame 的display.update()方法在 pygame 窗口上顯示顯示表面對象。
使用 PyGame 顯示圖像
在這里,我們首先導入所需的庫,然后設置圖像的寬度和高度,然后創建該尺寸的顯示表面,然后在 image.load() 函數中給出所需圖像的路徑,最后遍歷列表事件對象。
# 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()
總結
原文鏈接:https://blog.csdn.net/qq_39132095/article/details/128046484
相關推薦
- 2022-11-08 OpenCV圖像特征提取之Shi-Tomasi角點檢測算法詳解_C 語言
- 2022-11-23 C語言學習之關鍵字的示例詳解_C 語言
- 2024-01-15 Stream流 - 獲取Stream和轉換操作(含基本數據類型流)
- 2022-04-07 一篇文章帶你學習Python3的高階函數_python
- 2022-04-27 Python線程之同步機制實際應用場景舉例說明_python
- 2022-11-05 Nginx配置文件中location配置的多種場景_nginx
- 2022-07-08 C#四種計時器Timer的區別和用法_C#教程
- 2022-05-06 Python基于LightGBM進行時間序列預測_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支