作为一个项目,我正在尝试制作一个每隔十分之一秒闪烁随机颜色的屏幕。
这是我到目前为止的代码:
import pygame, random
from threading import Timer
background_colour = (random.randint(0,255),random.randint(0,255),random.randint(0,255))
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Rainbow!!!')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
它打开一个随机背景的屏幕。但是我为下一步尝试了很多方法。一点帮助?
答案 0 :(得分:2)
在运行之前请注意它包含快速闪烁的颜色
您需要每十分之一秒更改变量background_colour
一个实现预期结果的简单程序:
import pygame
import random
window_x = 300
window_y = 200
def get_rand_colour():
colour_r = random.randint(0,255)
colour_g = random.randint(0,255)
colour_b = random.randint(0,255)
return (colour_r,colour_g,colour_b)
screen = pygame.display.set_mode((window_x,window_y))
pygame.display.set_caption("Rainbow!")
clock = pygame.time.Clock()
done = False
counter = 0
colour = get_rand_colour()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
counter += 1
if counter > 3:
colour = get_rand_colour()
counter = 0
screen.fill(colour)
pygame.display.flip()
clock.tick(30)
pygame.quit()
希望这会有所帮助:)