Random Color Generator on Click

  • Share this:

Code introduction


This function creates a window using the pygame library and fills it with a random color every time the window is clicked.


Technology Stack : pygame, Random number generation

Code Type : Function

Code Difficulty : Intermediate


                
                    
import pygame
import random

def create_random_color():
    def get_random_color():
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    
    screen = pygame.display.set_mode((500, 500))
    pygame.display.set_caption("Random Color Generator")
    
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        
        screen.fill(get_random_color())
        pygame.display.flip()
    
    pygame.quit()