Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to change screen background color in Pygame?

  • Difficulty Level : Easy
  • Last Updated : 01 Oct, 2020

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

  • 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))
  
# Initialing 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))
  
# Initialing RGB Color 
color = (0, 0, 255)
  
# Changing surface color
surface.fill(color)
pygame.display.flip()

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!