Random Color Window Generator with Pygame

  • Share this:

Code introduction


This function uses the pygame library to create a window and fills it with a random color each time it is run.


Technology Stack : The package and technology stack used by the code[English]

Code Type : The type of code

Code Difficulty :


                
                    
def random_color():
    import random
    import pygame

    def get_random_color():
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    pygame.init()
    screen = pygame.display.set_mode((200, 200))
    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()