Random Color Window with Pygame

  • Share this:

Code introduction


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


Technology Stack : pygame, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import pygame
import random

def generate_random_color():
    # This function generates a random color using pygame's random module
    def get_random_color():
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    # Create a surface with a random color
    screen = pygame.display.set_mode((200, 200))
    pygame.display.set_caption("Random Color Generator")
    pygame.init()
    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()                
              
Tags: