Table of contents
1.
Introduction 
2.
Installation
3.
Displaying the Images
4.
Implementation in Pygame
4.1.
Steps to Run the Code
5.
Frequently Asked Questions
5.1.
What is the use of pygame?
5.2.
What are some examples of pygame games?
5.3.
Name some alternatives to Pygame.
5.4.
Is it worth learning Pyame?
6.
Conclusion
Last Updated: Mar 27, 2024

Display the Images With PyGame

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Pygame is a suite of Python modules that may be used to make video games across several platforms. It includes Python-compatible sound and graphics libraries. It's now up to the developer's creativity or necessity to figure out what kind of game they want to produce with this framework.

This article will show how to use the pygame module to display the images with Pygame on the screen, considering its height, width, and position in the pygame window.

Installation

To display the images in pygame, we first need to install the pygame using the following command in our Anaconda Command prompt.

To install pygame, use the following command:

pip install pygame

 

The basic procedures for displaying graphics in the pygame window are as follows:

  • Using the pygame display.set mode() method, create a display surface object.
  • By using the image. load () method of pygame, create an Image surface object, i.e., a surface object with an image drawn on it.
  • Using the blit() method of the pygame display surface object, copy the image surface object to the display surface object.
  • The display.update() method of pygame shows the display surface object on the pygame window.

Image Sampling

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!!

Live masterclass