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
import pygame
pygame.init()
screen = pygame.display.set_mode([ 600 , 400 ])
img = pygame.image.load( 'gfg_image.jpg' )
pygame.display.set_icon(img)
running = True
while running:
for event in pygame.event.get():
if event. type = = pygame.QUIT:
running = False
screen.fill(( 255 , 255 , 0 ))
pygame.draw.circle(screen, ( 0 , 0 , 0 ), ( 300 , 200 ), 75 )
pygame.display.update()
pygame.quit()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...