Customize the Pygame Window|Python

Customize the Pygame Window|Python

In the previous article, we learned how to create a game window using pygame and python. But it was definitely not what we all wanted. Of course, we need to Customize the Pygame Window by adding a background color, change the title, and adding an icon. So in this article, we will learn how to apply these things.

Changing the title of the window using pygame:

As we previously saw that by default the title of our game window/screen was “pygame window”.

Notice the default title of the window. It is set as “pygame window”

So, to change that title, we need to use a function from pygame’s display module because the display module controls the display of the window/screen and the function that we require is called set_caption.

set_caption sets the current window caption/title. It takes two arguments that are title and icon title. For this course, we don’t need to worry about icon titles so we won’t be using them. We will be passing only one argument that is the title as a string.

The code to change the title of the window is given below:

pygame.display.set_caption("Copy Assignment")
#Add it before the loop
Your Final code window must be looking like this.

Now, let’s try to run it.

Output window: Notice the change in title. It is now “Copy Assignment”.

Notice the title change. From “pygame window” it is now set to “Copy Assignment”.

Customize the Pygame Window: Background Color

By default we get a black background colour with our game window. So, to change it we will be using our screen variable where we stored our window.

The function that we are gonna use is fill(). It fills screen with a solid colour.

fill() takes 3 arguments that are:

fill(color, rect=None, special_flags=0) -> Rect

Fill the Surface with a solid color. If no rect argument is given the entire Surface will be filled. The rect argument will limit the fill to a specific area. The fill will also be contained by the Surface clip area.

The colour argument can be either a RGB sequence, a RGBA sequence or a mapped colour index. If using RGBA, the Alpha (A part of RGBA) is ignored unless the surface uses per-pixel alpha (Surface has the SRCALPHA flag).

Here is an example:

fill((255,0,0)) will give you a solid red colour, fill((0,255,0)) will give you a solid green colour, and fill((0,0,255)) will give you a solid blue colour.

So we can make different colour combinations just by changing the values of RGB colour code.



Now lets change the colour of our window by writing the following code.

# Inside the while loop after the for-loop, write the following
screen.fill((167,145,55))

Now, if you run it, you will not be able to see any changes because we haven’t updated the changes in our display window. So to do that we need to use a function from the display module that updates the display.

So we will be updating our display every time the while-loop executes.

To update the display, we need to write the following code:

pygame.display.update()
#Inside the while loop

You final code must be looking like this:

Screenshot of your final code window

Now, let’s run it!

You output window must have been looking like this:

Notice the changed background colour.

Customize the Pygame Window: Icon of window

To customize the icon of the window, we first need to load an icon image into a variable, and in order to do that, we will use the image module of pygame. And the function that we are gonna use from the image module is load(). It takes one argument where we need to put the path of the image as a string.

The syntax of the command that we are gonna use is:

icon = pygame.image.load('<path&gt')

Now, as we load the image inside the variable, now it’s time to set it as the icon of the window.

To do so, we need to use a function from the display module of pygame, that is set_icon(). It takes one argument, that is the image that we want to put as the icon. So, to set the icon, we need to write the following command.

pygame.display.set_icon(icon)

Now try to run it. Notice the change in icon of the pygame window.

You whole code should look like this:

import pygame

pygame.init()


screen = pygame.display.set_mode((800,600))
# Title
pygame.display.set_caption("Copy Assignment")
icon = pygame.image.load("task.png")
pygame.display.set_icon(icon)
isRunning = True

while(isRunning ==True):
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
screen.fill((167,145,55))
pygame.display.update()

So, in this article, we learned how to Customize the Pygame Window. If you have any queries, do send them through comments. For further details about customizing the display of the pygame window, check out the documentation: Pygame Display Docs.


Also Read:


  • Games in Python | Assignment Expert
  • Creating a Pong Game using Python Turtle
    Introduction Today, we are going to create a Pong Game using Python Turtle. Pong is a well-known computer game that is similar to table tennis. The two players in this game control the two paddles on either side of the game window. To hit the moving ball, they move the paddles up and down. A…
  • Balloon Shooter Game using Python PyGame
    We all know the importance of the pygame library in terms of game development. We know that Pygame is a collection of Python modules for making video games. It is a collection of computer graphics and sound libraries for the Python programming language. Pygame is well suited to the development of client-side applications that may…
  • Complete PyGame Tutorial and Projects
    Without a doubt, we can say that Python is a multi-purpose language. Ranging from basic programming to its application in Data Science, from Machine Learning to Artificial Intelligence, and also in Developing Games. In simple words, it’s the language that is incomparable.With knowing the advantages of Python, today, in this article of Complete PyGame Tutorial…
  • Flappy Bird In Python Pygame with source code
    OVERVIEW OF THE PROJECT Hi there, welcome to the copyassignment hope you all are doing wonderful. Today, we are here to make a very interesting game Flappy Bird In Python Pygame. Hope you are familiar with this entertaining and very fascinating game. We would see how we can code the game Flappy bird from Scratch….

Share:
Avatar of ankit dwivedi

Author: ankit dwivedi