日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

使用PyGame顯示圖像的四種方案實(shí)例代碼_python

作者:堅(jiān)果的博客 ? 更新時(shí)間: 2023-02-03 編程語言

安裝pygame

安裝 pygame 的最佳方法是使用 pip 工具(python 使用它來安裝包)。請注意,在最新版本中,這與 python 一起提供。我們使用 –user 標(biāo)志告訴它安裝到主目錄,而不是全局。

python -m pip install -U pygame --user

要查看它是否有效,請運(yùn)行包含的示例之一:

python -m pygame.examples.aliens

如果可行,我們就可以開始了!

在pygame窗口上顯示圖像有四個(gè)基本步驟:

  • 使用pygame 的display.set_mode()方法創(chuàng)建一個(gè)顯示表面對象。
  • 使用 pygame 的 image.load() 方法創(chuàng)建一個(gè) Image 表面對象,即在其上繪制圖像的表面對象。
  • 使用pygame顯示表面對象的blit()方法將圖像表面對象復(fù)制到顯示表面對象。
  • 使用 pygame 的display.update()方法在 pygame 窗口上顯示顯示表面對象。

使用 PyGame 顯示圖像

在這里,我們首先導(dǎo)入所需的庫,然后設(shè)置圖像的寬度和高度,然后創(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()

總結(jié)

原文鏈接:https://blog.csdn.net/qq_39132095/article/details/128046484

欄目分類
最近更新