Displaying the Images
We will see the step-by-step procedure to display the images in Pygame.
Step 1: Import pygame at the top of your program's file by the following given command.
Import pygame
Step 2: Before utilising the essential pygame modules, you must first initialise them.
pygame.init()
Step 3: Set the width and height variables in step three.
displayWidth = 700
displayHeight = 500
Step 4: Make a display surface object with enough space for the image to be displayed.
surface= pygame.display.set_mode((displayWidth, displayHeight ))
Step 5: Set the window's caption
pygame.display.set_caption('Image’s Caption.')
Step 6: Make a surface item for the image to be drawn on.
displayImage = pygame.image.load(r'D:\Users\user\Pictures\image.jpg')
Step 7: An infinite loop is utilised to project the image on the screen indefinitely, stopping only when the window is closed.
while True :
// The RGB value 255, 255, 255 is for the White colour.
surface.fill((255,255,255))
surface.blit(displayImage, (0, 0))
for event in pygame.event.get() :
if event.type == pygame.QUIT :
pygame.quit()
quit()
pygame.display.update()
Implementation in Pygame
Here is the implementation to display the images in Pygame.
# import the pygame module
import pygame
# initialising Pyame library.
pygame.init()
# assigning values to displayHeight and displayWidth
displayHeight = 700
displayWidth = 500
display_surface = pygame.display.set_mode((displayHeight, displayWidth ))
# set the pygame window name
pygame.display.set_caption('Coding Ninjas Logo')
# create a surface object, image is drawn on it.
image = pygame.image.load(r'D:\programming\CodingNinjas\Pygame\Coding_Ninjas_logo.jpeg')
# infinite loop
while True:
# fill the surface object with white colour
display_surface.fill((255, 255, 255))
# to display surface object at (0, 0) coordinate.
display_surface.blit(image, (0, 0))
for event in pygame.event.get() :
# if the event object type is QUIT then quitting the pygame
if event.type == pygame.QUIT :
pygame.quit()
# quit the program also.
quit()
# Draws the surface object to the screen.
pygame.display.update()
Steps to Run the Code
To see the output of our program, consider the following steps:
Step 1: Open the Anaconda Prompt and set the correct path to our Pygame program.

Step 2: To run the program, use the following command and press the enter key.
Python filename.py ( For example, Python Ninjas.py )

Output:

Frequently Asked Questions
What is the use of pygame?
Pygame is a cross-platform collection of Python modules for making video games. It is a collection of computer graphics and sound libraries for the Python programming language.
What are some examples of pygame games?
Pygame has been used in several notable games.
- Frets on Fire.
- Dangerous High School Girls in Trouble.
- Save the Date, IndieCade 2013 Finalist.
- Drawn Down Abyss
Name some alternatives to Pygame.
Some of the alternatives to python are:
- Blit
- Godot
- Kivy
- Electron
- Unity
Is it worth learning Pyame?
Pygame is an excellent tool for beginners to get comfortable with programming and game production and feel successful when creating games. It's also a fantastic tool for quick prototyping.
Conclusion
In this article, we discussed the following topics:
- Installation of Pygame module.
- Steps to display the images in Pygame.
- Implementation of our code to display the images in Pygame.
-
Steps to run our program to display the images in Pygame.
We hope this blog has enhanced your knowledge regarding Pygame. Check out our article on 19 Best Python Frameworks and learn more about Python by visiting the Python category at Coding Ninjas.
Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!
Do upvote our blog to help other ninjas grow.
We wish you Good Luck! Keep coding and keep reading Ninja!!