How to change the PyGame icon?
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 Pygameimport pygame # Construct the GUI gamepygame.init() # Set dimensions of game GUIscreen = pygame.display.set_mode([600, 400]) # Take image as inputimg = pygame.image.load('gfg_image.jpg') # Set image as iconpygame.display.set_icon(img) # Set running valuerunning = True # Setting what happens when game # is in running statewhile 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 gamepygame.quit() |
Output:
![]()


