Python - 使用 PyGame 显示图像
pythonserver side programmingprogramming
Pygame 是一个用于制作游戏和多媒体应用程序的 Python 多媒体库。在本文中,我们将了解如何使用 pygame 模块在屏幕上绘制图片,同时考虑其在 pygame 窗口中的高度、宽度和位置。
在下面的程序中,我们初始化 pygame 模块,然后定义图像的模式和标题。接下来,我们加载图像并定义坐标。screen.blit 函数绘制屏幕,而 while 循环则不断监听游戏结束时的点击。
示例
import pygame pygame.init() w = 300; h = 300 screen = pygame.display.set_mode((w, h)) pygame.display.set_caption('Tutorialspoint Logo') TPImage = pygame.image.load("E:\Python3\tutorialspoint.png").convert() # 图像坐标 x = 10; y = 20; screen.blit(TPImage, (x, y)) # 绘制一次屏幕 pygame.display.flip() running = True while (running): # 循环监听游戏结束 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 循环结束,相当 pygame pygame.quit()
输出
运行上述代码得到以下结果 −