简体   繁体   中英

In pygame, how do you make the background continuously change color between red, green, blue, black , and white in a loop

I have the general outline code. But i need a background which can change color once you run the program at an reasonable speed between the colors red, green, blue, black and white. I know it has to do with the while loop, i just do not know how to incorporate it.

from __future__ import division
import pygame


pygame.init()


width = 640
height = 480
size = (width, height)
screen = pygame.display.set_mode(size)
background = (0,0,0) 

fps = 60 


clock = pygame.time.Clock()

while True:

    clock.tick(fps) 


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

    screen.blit(background)
    pygame.display.flip()
    clock.tick(1)

If you already know that colors are composite of red, green and blue that will help you already, those are called color channels and change between 0 and 255. I would first take a paper and draw how would change each of the channels with time. So you'll have 3 functions on the same time scale. Then I would brake the time axis in sections, where the functions are linear (or some other standard function), and define the function depending on in which section I am now. I gave just a general approach, since it's quite a big amount of job to do. Another approach is to set up the table will the sequence of all colors that are to be shown and just cycle through them in a loop and filling the display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM