In this Pygame Tutorial we want to learn about Giving Title to your Pygame game, if you are creating a game with Pygame, one of the important things you want to do is give it a title. title of your game will be the first thing players see when they start the game, so it is essential to make it nice and descriptive title. in this article we want to learn how to give your Pygame game a title using pygame.display.set_caption() function.
First of all, you need to importPygame module and initialize it with the pygame.init() function.
1 2 |
import pygame pygame.init() |
After that the Pygame is initialized you can set caption or title of your game display window using pygame.display.set_caption() function. this function takes string as an argument, which will be the title of your game. for example:
1 |
pygame.display.set_caption("Codeloop Game") |
After setting the caption of your game, you need to update Pygame display for the changes to take effect. you can do this with pygame.display.update() function.
1 |
pygame.display.update() |
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import pygame # Initialize Pygame pygame.init() # Set dimensions of display window display_width = 800 display_height = 600 game_display = pygame.display.set_mode((display_width, display_height)) # Set caption of display window pygame.display.set_caption("Codeloop Game") # Update the display pygame.display.update() # Game loop game_exit = False while not game_exit: for event in pygame.event.get(): if event.type == pygame.QUIT: game_exit = True # Your game code here # Update display pygame.display.update() # Quit Pygame pygame.quit() quit() |
This code initializes Pygame and sets dimensions of the display window. after that it sets the title of the display window using pygame.display.set_caption(), and updates the display using pygame.display.update(). and finally the code enters a game loop that continually updates the display until the user closes the window.
Run the complete code and this will be the result
Subscribe and Get Free Video Courses & Articles in your Email