Tuesday, October 7, 2025
HomeLanguagesHow to change screen background color in Pygame?

How to change screen background color in Pygame?

Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language.

Functions Used:

  • pygame.init(): This function is used to initialize all the pygame modules.
  • pygame.display.set_mode(): This function is used to initialize a screen for display.
  • fill(): This method is used to fill the display with the color specified.

Example 1: This example sets the screen background color to red.

Python3




# Importing the library
import pygame
 
# Initializing Pygame
pygame.init()
 
# Initializing surface
surface = pygame.display.set_mode((400, 300))
 
# Initializing RGB Color
color = (255, 0, 0)
 
# Changing surface color
surface.fill(color)
pygame.display.flip()


Output:

Example 2: This example uses RGB color to set the screen color to blue.

Python3




# Importing the library
import pygame
 
# Initializing Pygame modules
pygame.init()
 
# Initializing surface
surface = pygame.display.set_mode((400, 300))
 
# Initializing RGB Color
color = (0, 0, 255)
 
# Changing surface color
surface.fill(color)
pygame.display.flip()


Output:

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS