Question

I am currently working on a fractal generation, and I've got the fractal built and functioning to my desired specifications, although I am looking to add randomize coloring to it. Currently

I have the whole fractal to show as a random color after the program has ran, but I would like for each level or even each leg of the fractal to be a different random color, I am using pygame to generate this fractal and an RGB value for the color.

import pygame
import random
fractal_level = 3
colblue = (0,61,103)
colbk = (0,0,0)


color = random.sample(xrange(0,255), 3)
def fract_draw(x, y, width, height, count):

    pygame.draw.line(screen,color,[x + width*.25,height//2+y],[x + width*.75,height//2+y],2)
    pygame.draw.line(screen,color,[x+width*.25,(height*.5)//2+y],[x+width*.25,(height*1.5)//2+y],2)
    pygame.draw.line(screen,color,[x + width*.75,(height*.5)//2+y],[x + width*.75,(height*1.5)//2+y],2)

    if count > 0:
        count -= 1
        fract_draw(x, y,                           width // 2, height // 2, count)
        fract_draw(x + width // 2, y,              width // 2, height // 2, count)
        fract_draw(x, y + width // 2,              width // 2, height // 2, count)
        fract_draw(x + width // 2, y + width // 2, width // 2, height // 2, count)


pygame.init()
size = [750, 750]
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
screen.fill(colblue)

fract_draw(0, 0, 750, 750, fractal_level)      
pygame.display.flip()
clock.tick(20)

Sorry if the code is not 100% optimized with a sys exit and pygame exit as I've been attempting to keep the code down to a minimum.

Was it helpful?

Solution

Simplest way:

import pygame
import random
fractal_level = 3
colblue = (0,61,103)
colbk = (0,0,0)

def fract_draw(x, y, width, height, count):

    color = random.sample(xrange(0,255), 3)
    pygame.draw.line(screen,color,[x + width*.25,height//2+y],[x + width*.75,height//2+y],2)
    color = random.sample(xrange(0,255), 3)
    pygame.draw.line(screen,color,[x+width*.25,(height*.5)//2+y],[x+width*.25,(height*1.5)//2+y],2)
    color = random.sample(xrange(0,255), 3)
    pygame.draw.line(screen,color,[x + width*.75,(height*.5)//2+y],[x + width*.75,(height*1.5)//2+y],2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top