By default, whenever you create a basic game window in Pygame there is a default icon on the top-left corner. This is a small image or “icon” which displays the Pygame Logo by default. You might want to change this default behavior though, so in this tutorial we will explain how to change the default Pygame Icon.
Changing Pygame Icon
A step-by-step on how to write a Pygame application, where we create a Window with a custom Icon. We will be using the Icon (Our website logo) shown below in our application.
You can use a wide variety of filetypes for this purpose, such as .png
, .jpg
and .ico
.
import pygame
import sys
pygame.init()
First we write some basic setup code for the imports, and the initialization of the pygame engine.
screen = pygame.display.set_mode([400, 300])
Next we create our Pygame Display Window, on which everything is drawn.
icon = pygame.image.load("icon.jpg")
Now we load the required image file.
pygame.display.set_icon(icon)
pygame.display.set_caption("CodersLegacy")
And now we use the set_icon()
function to over-ride the default icon. For good measure we also changed the default text on the Window, using set_caption()
.
while True:
for event in pygame.event.get():
# Event Handling for the QUIT event
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill([100, 255, 100])
pygame.display.update()
This is the game loop code, where we have our Event Loop and where all the Screen updating and drawing takes place.
Complete Code
Here is the complete code that we discussed earlier, along with its output.
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode([400, 300])
icon = pygame.image.load("icon.jpg")
pygame.display.set_icon(icon)
pygame.display.set_caption("CodersLegacy")
while True:
for event in pygame.event.get():
# Event Handling for the QUIT event
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill([100, 255, 100])
pygame.display.update()
This marks the end of the “How to change the Pygame Icon?” Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.