iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用PyGame显示图像的四种方案实例代码
  • 578
分享到

使用PyGame显示图像的四种方案实例代码

pygame 显示图片pygame 显示图像pygame 图像 2022-12-27 18:12:54 578人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录安装pygame使用 PyGame 显示图像总结安装pygame 安装 pygame 的最佳方法是使用 pip 工具(python 使用它来安装包)。请注意,在最新版本中,这与

安装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()

总结

到此这篇关于使用PyGame显示图像的四种方案的文章就介绍到这了,更多相关PyGame显示图像内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 使用PyGame显示图像的四种方案实例代码

本文链接: https://www.lsjlt.com/news/175986.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作