
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Change the Pygame Icon
While building video games, we often try to setup an image or logo for that developed game. Python provides a function in pygame named, set_icon(), which sets the icon as desired.
Pygame is a set of modules that allows us to develop the games and multimedia applications. This is built in top of the SDL(Simple Direct Media Layer) library which has low access to the audio keyboard, mouse, joystick and graphics hardware through OpenGL and Direct3D.
Syntax
Following is the syntax for setting the icon for the game -
pygame_icon = pygame.image_load("image_name") pygame.display.set_icon(pygame_icon)
Where,
pygame_icon is the name of the variable.
pygame is the library.
image_load is the function to load an image.
image_name is the image we uploaded.
display.set_icon is the function to set the required icon.
For setting up the icon for the developed game we have to follow the steps given below.
First we have to install the pygame library
pip install pygame
On successful installation we will get the following output -
Collecting pygame Downloading pygame-2.3.0-cp39-cp39-win_amd64.whl (10.6 MB) ---------------------------------------- 10.6/10.6 MB 4.5 MB/s eta 0:00:00 Installing collected packages: pygame Successfully installed pygame-2.3.0 Note: you may need to restart the kernel to use updated packages.
Now we have to initialize the Graphical user interface (GUI) of pygame.
pygame.init()
The output of the above snippet is as follows.
(5, 0)
Next we have to set the dimensions of the constructed GUI game by specifying the width and height
size = pygame.display.set_mode([5,5]) print(size)
The below is the output of setting the dimensions of the GUI game.
<Surface(5x5x32 SW)>
Now pass the image that we want to set as the icon to the image.load() function.
img = pygame.image.load('image.png') print(img)
The below is the output of uploading the image as per our requirement.
<Surface(5x5x32 SW)>
Next we have to pass the uploaded image to the display.set_icon() function then the image will be set at the top left corner when the game is running.
pygame.display.set_icon(img)
Now we have to set the running status of the game as True.
running = True
Now we have set the things which we want to perform while the game is running.
while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False
Next we have to fill the background with the specified color by passing the RGB colors to the fill() function.
size.fill((150,150,0)
Now we have to update the changes whatever we done.
pygame.display.update()
At last we have to quit the GUI game
pygame.quit()
Complete Example
Now let's see the overall code at once.
import pygame pygame.init() size = pygame.display.set_mode([200,200]) img = pygame.image.load('image.png') pygame.display.set_icon(img) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False size.fill((200,200,0)) pygame.draw.circle(size, (0, 0, 0), (100, 100), 100) pygame.display.update() pygame.quit()
Output
The following is the output of the icon we set at the top left of the window. In the output we can observe the icon on left side top.

Example
Let's see another example to set the icon the GUI console.
import pygame pygame.init() size = pygame.display.set_mode([400,200]) img = pygame.image.load('download.jpg') pygame.display.set_icon(img) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False size.fill((200,200,0)) pygame.draw.circle(size, (0, 0, 0), (100, 120), 50) pygame.display.update() pygame.quit()
Output
The following is the output of the icon placed at the left top of the window.
