【问题标题】:Randomizing Color Python Fractal随机化颜色 Python 分形
【发布时间】:2014-02-24 06:12:49
【问题描述】:

我目前正在研究分形生成,我已经按照我想要的规格构建并运行分形,尽管我希望为其添加随机着色。目前

在程序运行后,我将整个分形显示为随机颜色,但我希望分形的每个级别甚至每条腿都是不同的随机颜色,我正在使用 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)

很抱歉,如果代码没有通过 sys 退出和 pygame 退出进行 100% 优化,因为我一直在尝试将代码保持在最低限度。

【问题讨论】:

    标签: python random colors rgb


    【解决方案1】:

    最简单的方法:

    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)
    

    【讨论】:

    • 太完美了。我非常感谢它,它产生了非常优雅的结果,我非常感谢它。
    猜你喜欢
    • 1970-01-01
    • 2014-05-21
    • 2018-10-05
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多