我想以更简单的方式随机改变背景颜色(Pygame)

时间:2013-11-16 19:51:50

标签: python pygame

我希望随机时间通过红色和绿色随机改变更改以更简单的方式不涉及.wait函数,(现在是几秒钟和几秒)我有

    self.time = random.randint(1,3)
    self.bgcolor = random.randint(1,2)
    if self.time == 1:
        self.x = 30
        self.wait = 1
    if self.time == 2:
        self.x = 30
        self.wait = 2
    if self.time == 3:      
        self.x = 30
        self.wait = 3
    if self.bgcolor == 1:   

        self.screen.fill(self.red)
        time.sleep(self.time)


        pygame.display.flip()



    if self.bgcolor == 2:
        self.screen.fill(self.green)
        time.sleep(self.time)


        pygame.display.flip() 

1 个答案:

答案 0 :(得分:0)

你可以这样做:

from random import choice, randint

self.screen.fill(choice([self.red, self.green]))
self.wait = self.time = randint(1, 3)
time.sleep(self.wait)