嵌套for循环不能按我想要的方式工作

时间:2012-02-16 01:40:32

标签: python-3.x

我正在使用嵌套的for循环使颜色在pygame中逐渐变化。我的问题是变化只发生在蓝色上。为什么????这是代码:

while True:         
    for a in range(256):
        for b in range(256):
            for c in range(256):
                for event in pygame.event.get():
                    if event.type == QUIT:
                        pygame.quit()
                        sys.exit()
                text = basicFont.render('Hello world!', True, (a, b, c), black)
                textRect = text.get_rect()
                textRect.centerx = windowSurface.get_rect().centerx
                textRect.centery = windowSurface.get_rect().centery
                windowSurface.fill(black)
                windowSurface.blit(text, textRect)
                pygame.display.update()
                mainClock.tick(40)

for循环仅适用于'a'

1 个答案:

答案 0 :(得分:0)

您可以通过在循环期间打印出a,b和c的值来了解会发生什么:

for a in range(256):
    for b in range(256):
        for c in range(256):
            print(a, b, c)

这将给出c(蓝色)的结果首先从0到255,然后b增加到1,然后c跳回到0,然后再次增加到255,然后b变为2,并且c再次从0到255。

我怀疑你在杀死它之前没有让程序有时间影响绿色。而且由于需要65536次重画才能开始影响红色,这需要相当长的时间。

问题是你实际上目标是什么样的效果。

相关问题