- 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))