How to add color breezing effect using pygame?

Pygame is a python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different sprites. Pygame is not particularly best for designing games as it is very complex to use doesn’t have a proper GUI like unity but it definitely builds logic for further complex projects.

Installation

Before initializing pygame library we need to install it. To install it type the below command in the terminal.

pip install pygame

Color Breezing effect

Pygame contains color coding in the format of a tuple that contains three values, these values indicate the intensities of the three core colors i.e. red, blue, green. The values of the individual colors can be changed in order to make another unique color. As the values of the tuple are mutable at the run time too, it gives us the flexibility to add some color effects to make our game/application more unique and beautiful.

one of the color effects is a breezing effect, a breezing effect is an effect in which the color changes from one shade to another smoothly without sudden or abrupt change. These effects can be seen in the RGB keyboard and mouses.

Example:-

filter_none

edit
close

play_arrow

link
brightness_4
code

import pygame
import random
import sys
  
  
# initializing the constructor
pygame.init()
  
# setting up variable screen
screen = pygame.display.set_mode((720,720))
  
# three arguments of the color tuple
c1 = random.randint(0,255)
c2 = random.randint(0,255)
c3 = random.randint(0,255)
  
# setting up variable clock
clock = pygame.time.Clock()
  
while True:
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
              
    # increases the shade of
    # the current color
    if 0 < c1 < 255:
        c1 += 1
          
    # if value of c1 exceeds 
    # 255 it resets it to 0
    elif c1 >= 255:
        c1 -= 255
          
    # if value of c1 preceeds 0
    # it is increamented by 3
    elif c1 <= 0:
        c1 += 3
          
    # sets game fps to 60
    clock.tick(60)
      
    # sets bg color to tuple
    # (c1,c2,c3)
    screen.fill((c1,c2,c3))
      
    # updates the frames of 
    # the game 
    pygame.display.update()

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.