2014-02-24 132 views
1

我目前正在研究分形生成,並且我已經創建了分形並運行到我想要的規格,但我正在尋找將隨機顏色添加到它。目前隨機化顏色Python分形

我有整個分形顯示爲程序運行後的隨機顏色,但我希望每個級別甚至每個分形的腿都是不同的隨機顏色,我使用pygame來生成這個分形和顏色的RGB值。

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) 

對不起,如果代碼不是100%優化的sys退出和pygame退出,因爲我一直試圖保持代碼降到最低。

回答

0

最簡單的方法:

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) 
+0

這是完美的。我非常感謝,它取得了非常優雅的成果,我非常感謝它。 –