使用PyGame显示图像的四种方案-创新互联

使用 PyGame 显示图像 安装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()

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

当前标题:使用PyGame显示图像的四种方案-创新互联
网站地址:https://www.cdcxhl.com/article16/epidg.html

成都网站建设公司_创新互联,为您提供网站排名小程序开发软件开发定制开发网站导航App开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站