Random Colors with Shapes

Hello, @Panda, and welcome to the Processing Forum!

We need additional information in order to provide you with concrete advice. However, the information that you did provide suggests that you may already be learning Python, and that you have learned about lists. The following assumes that this is true.

For each shape to have its own fill color, call the fill() function each time you are about to draw a shape.

Since a shape is described by multiple attributes, such as vertex coordinates and perhaps colors, we need to know how the information about each shape is being stored in your array (list). Have you learned about objects (Python classes)? Defining a class for your shapes may be useful for organizing your code. Then you would be able to create a list of shape objects.

You may benefit by posting the code that you have thus far. If you do so, it is important that you format the code for posting. See Guidelines—Asking Questions for advice on how to do this.

Below is an example, formatted for posting, that defines a Tri class for triangles, creates a list of Tri objects, and redraws them during each frame with a fill color that is chosen randomly for each triangle. Please feel free to ask any questions that you may have.

class Tri(object):
    # a triangle with coordinates and fill attributes
    def __init__(self, x1, y1, x2, y2, x3, y3, f):
        # vertex coordinates
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.x3 = x3
        self.y3 = y3
        # fill color; a color object
        self.f = f 
    def display(self):
        # draw this Tri
        pushStyle()
        fill(self.f)
        triangle(self.x1, self.y1, self.x2, self.y2, self.x3, self.y3)
        popStyle()
    def set_fill(self, f):
        # set a fill color for this Tri
        self.f = f
        
def random_color():
    # return a random color (red, green blue, alpha)
    return color(random(256), random(256), random(256), random(256))

def setup():
    size(400, 400)
    global tri_list
    tri_list = [Tri(random(width), random(height),
                    random(width), random(height),
                    random(width), random(height),
                    random_color())
                for i in range(4)]

    frameRate(1)
    
def draw():
    for tri in tri_list:
        tri.display()
        tri.set_fill(random_color())

EDITED on April 2, 2021 to use pushStyle() and popStyle() in the code.

2 Likes