Skip to content
Related Articles

Related Articles

Improve Article

How to change the PyGame icon?

  • Last Updated : 30 Apr, 2021

While building a video game, do you wish to set your image or company’s logo as the icon for a game? If yes, then you can do it easily by using set_icon() function after declaring the image you wish to set as an icon. Read the article given below to know more in detail. 

Syntax:

pygame_icon = pygame.image.load('#Enter the image')
pygame.display.set_icon(pygame_icon)

Approach:

Step 1: First, import the library Pygame.

import pygame

Step 2: Now, construct the GUI game.

pygame.init()

Step 3: Further, set the dimensions of your GUI game.



screen = pygame.display.set_mode([#width of game, #height of game])

Step 4: Next, take the image as input that we wish to set as an icon.

img = pygame.image.load('#Enter the image')

Step 5: Then, set the image as an icon. The icon set will appear in the top-left corner when the game is in running state.

pygame.display.set_icon(img)

Step 6: Later on, set the running value for running the game.

running = True

Step 7: Set the things which you want your game to do when it is in a running state

while running:
   for event in pygame.event.get():
  • Step 7.1: Once the app is in a running state, make it quit if the user wants to quit.
       if event.type == pygame.QUIT:
           running = False
  • Step 7.2: Moreover, set the background color which you wish to see in your app.
   screen.fill(# RGB Value of Color)
  • Step 7.3: Then, make your app do whatever you want it do while being in a running state.
  • Step 7.4: After doing everything you wish to do, update the changes done.
   pygame.display.update()

Step 8: Finally, quit the GUI game

pygame.quit()

Below is the implementation.

Python




# Python program to change 
# the Pygame icon
  
# Import the library Pygame
import pygame
  
# Construct the GUI game
pygame.init()
  
# Set dimensions of game GUI
screen = pygame.display.set_mode([600, 400])
  
# Take image as input
img = pygame.image.load('gfg_image.jpg')
  
# Set image as icon
pygame.display.set_icon(img)
  
# Set running value
running = True
  
# Setting what happens when game 
# is in running state
while running:
    for event in pygame.event.get():
  
        # Close if the user quits the game
        if event.type == pygame.QUIT:
            running = False
  
    # Set the background color
    screen.fill((255, 255, 0))
  
    # Draw a circle on the screen
    pygame.draw.circle(screen, (0, 0, 0), (300, 200), 75)
  
    # Update the GUI game
    pygame.display.update()
  
# Quit the GUI game
pygame.quit()

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :