Random Color Generator Using Pygame

  • Share this:

Code introduction


This code uses the pygame library to create a window where the background color will change randomly.


Technology Stack : pygame, Random number generation

Code Type : The type of code

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

    pygame.init()
    screen = pygame.display.set_mode((100, 100))
    pygame.display.set_caption("Random Color Generator")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        screen.fill(get_random_color())
        pygame.display.flip()

    pygame.quit()