Random Color Generator Function

  • Share this:

Code introduction


This function defines a function to generate a random color. It uses the random module from the pygame library to generate random numbers and returns an RGB tuple as the color.


Technology Stack : pygame, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
import pygame
import random

def random_color():
    # This function returns a random color as a tuple (R, G, B)
    def generate_color():
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    return generate_color

# Usage example
random_color_generator = random_color()
print(random_color_generator())  # Outputs a random color                
              
Tags: