How to change the PyGame icon? (original) (raw)

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