Python | Display images with PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.
Command to install pygame :
pip install pygame
How to display images with PyGame ?
There are four basic steps to displaying images on the pygame window :
- Create a display surface object using display.set_mode() method of pygame.
- Create a Image surface object i.e. surface object in which image is drawn on it, using image.load() method of pygame.
- Copy the image surface object to the display surface object using blit() method of pygame display surface object.
- Show the display surface object on the pygame window using display.update() method of pygame.
Display images using PyGame
Here we are first importing the required library and then setting the width and height of the image then creating the display surface for that size then give the path of the required image in then image.load() function and then finally iterate over the list of event objects.
Python3
import pygame
pygame.init()
X = 600
Y = 600
scrn = pygame.display.set_mode((X, Y))
pygame.display.set_caption( 'image' )
imp = pygame.image.load( "C:\\Users\\DELL\\Downloads\\gfg.png" ).convert()
scrn.blit(imp, ( 0 , 0 ))
pygame.display.flip()
status = True
while (status):
for i in pygame.event.get():
if i. type = = pygame.QUIT:
status = False
pygame.quit()
|
Output:
Display images with PyGame
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...