Random Color Generator with Pygame

  • Share this:

Code introduction


This code uses the pygame library to create a window that displays random colors. The color changes every second.


Technology Stack : pygame

Code Type : Game

Code Difficulty : Intermediate


                
                    
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")
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        
        color = get_random_color()
        screen.fill(color)
        pygame.display.flip()
        pygame.time.delay(1000)

    pygame.quit()                
              
Tags: