How To Set Background Color In Pygame

  • For using background color we will use screen.fill(color)
  • In the color we can use rgb or hexadecimal color whatever we want want.
  • In this example we will fill the screen with this color rgb(255, 250, 240)
  • So our code will be screen.fill((255, 250, 240))



import pygame,sys
from pygame.locals import*
pygame.init()


screen=pygame.display.set_mode((600,400))
game_running=True
while game_running:
    for event in pygame.event.get():
        if event.type==QUIT:            
            pygame.quit()
            sys.exit()



    screen.fill((255,250,240))
    pygame.display.update()


Lets write the step by step full code and explanation

First import necessary library

  • import pygame,sys
    from pygame.locals import*

And then initiate the pygame

  • pygame.init()

Set the our game display resolution

  • screen=pygame.display.set_mode((400,400))

Set the game in a loop so it will continue running until you exit or game finish

  • game_running=True
    while game_running:

    your logic in this loop

In the loop we will fill the display with color

  • screen.fill((r,g,b))
  • r means red ( value from 0 to 255
  • g means green ( value from 0 to 255
  • b means blue ( value from 0 to 255)

when we change the value its creates a color mixture and with rgb value

Each color channel (Red, Green, Blue) can have an intensity value ranging from 0 (no color) to 255 (maximum intensity).

Since each of the three colors (R, G, B) has 256 possible intensity levels, the total number of potential color combinations is:

256 (Red values) * 256 (Green values) * 256 (Blue values) = 16,777,216 colors

  • screen.fill((255, 250, 240))

after adding the color we will update the display so if we update the color later it will also update the display color

we will update the display with pygame.display.update()

Try the code by yourself. Run the code and get the result. You can find 48+ pygame examples on github. You May also have some questions in your mind now. Let’s answer the questions!

How Many Times The Code Will Update The Color?

Answer: In the example we have not set frame rate. So if we do not set the frame rate, it will update based on your device capacity.

How Can I Set The Frame Rate?

You can add the frame rate by using pygame.time.Clock.tick

so if we want to set the frame rate 60fps we just have to add tick inside while loop tick(60)

Then the current code will be



import pygame,sys
from pygame.locals import*
pygame.init()
clock=pygame.time.Clock()

screen=pygame.display.set_mode((600,400))
game_running=True
while game_running:
	clock.tick(60) # we set the frame rate to 60
	for event in pygame.event.get():
		if event.type==QUIT:            
			pygame.quit()
			sys.exit()

	screen.fill((255,250,240))
	pygame.display.update()


How do I know How many frame rate my pygame program is running?

Setting the frame rate set the limit the frame rate the program to refresh the display. But it does not means if you set it 60fps it will run 60fps. If the program has complex calculation or complexity it may run lower frame rate then you set.

So we can get the actual frame rate in pygame by pygame.time.Clock.get_fps

Now our program should be



import pygame,sys
from pygame.locals import*
pygame.init()
clock=pygame.time.Clock()

screen=pygame.display.set_mode((600,400))
game_running=True
while game_running:
	clock.tick(60) # we set the frame rate to 60
	print(clock.get_fps()) # this line will show the actual frame rate
	for event in pygame.event.get():
		if event.type==QUIT:            
			pygame.quit()
			sys.exit()

	screen.fill((155,250,240)) #add or change RGB color background
	pygame.display.update()



This program will print the actual frame rate.

More about setting color in Pygame

  • Pygame has build in color list
  • you can use the color by using pygame.Color(color_name)
  • Check out which color names in the pygame color list
  • for example if your color is lavender
  • pygame.Color(lavender)
  • We can fill the screen with this color screen.fill(pygame.Color(lavender))

Related Posts

Send Desktop notification from pygame
April 29, 2024

For notification handling we will use plyer library We will add this functionality in our pygame code The complete code import pygame import sys from pygame.locals import * from plyer import notification pygame.init() clock = pygame.time.Clock() user_display = pygame.display.Info() width, height = 800, 600 screen = pygame.display.set_mode((width, height)) background = (255, 255, 255) red = […]

Pygame Project: Create the simplest digital clock
April 28, 2024

We will use python datetime module for our clock; We get the current time and separate the time into hour minutes and second. First get the current time Get the time in hour, minute and second Now got our clock time components. It will be like this import datetime current_time = datetime.datetime.now() hour = current_time.hour […]

Simulate Bird Flocking Behavior with pygame: Algorithm and Code Explained
March 23, 2024

Bird flocking is a wonderful example of how simple behaviors can lead to complex, beautiful patterns in nature. We’ll learn the key principles behind this flocking behavior and write a python program to simulate this wonderful phenomenon. The principles Bird Flocking Simulation Algorithm Setting Up the Simulation Each Frame of the Simulation The pygame example […]