Python - Display images with PyGame

PythonServer Side ProgrammingProgramming

Pygame is a multimedia library for Python for making games and multimedia applications. In this article we will see how to use the pygame module to paint a picture on the screen taking into consideration, its height, width and position in the pygame window.

In the below program we initialize the pygame module and then define the mode and the caption for the image. Next we load the image and define the coordinates. The screen.blit function paints the screen while the while loop keeps listening the end of the game is clicked.


import pygame
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()
# coordinates of the image
x = 10;
y = 20;
screen.blit(TPImage, (x, y))
# paint screen one time
running = True
while (running): # loop listening for end of game
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False
# loop over, quite pygame


Running the above code gives us the following result −

Updated on 25-Jan-2021 07:29:48