0
votes

I am just starting to learn pygame graphics. I drew a circle in pygame and was wondering how I program it to change colors.
For example: it changes colors from blue to red.

I need it to keep changing colors until I close pygame and it would be nice if the colors gradually changed from one to another instead of an instant change? Any ideas how I could do this?

import pygame
pygame.init()

RED =   (255,  0,  0)
BLUE =  (  0,  0,255)
BLACK = (  0,  0,  0)
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)

pygame.draw.circle(screen,RED,(500,500),200)

pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()
2

2 Answers

9
votes

I shall progress from simple to harder, and more complex..
Simplest: A for loop that changes the color 3 times, simplest:

import pygame
pygame.init()

RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)
colors = (RED, BLACK, BLUE) # tho allow you to iterate over the colors

for c in colors:
    pygame.draw.circle(screen,c,(500,500),200)
    pygame.display.flip()
    pygame.time.wait(1000)
pygame.quit()

Medium: Now an infinite loop, that ends when you close the window..

import pygame, itertools

RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)

colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors

SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)

# to cycle through the colors
cycle = itertools.cycle(colors) # create an infinite series..

clock = pygame.time.Clock() # regulate fps

while True:
    # handling events
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # close window event
            pygame.quit()

    c = cycle.next()
    pygame.draw.circle(screen,c,(500,500),200)
    pygame.display.flip()

    clock.tick(6) # run at maximum 6 frames per second

Hardest and most complex: This is the final one, the colors fade into the next one..

import pygame, itertools

def fade_into(c1, c2, n):
    """ Give the next color to draw \n"""
    "Args: c1,c2 => colors, n => int"
    dif = [(c1[i]-c2[i])/float(n) for i in range(3)] # calculate the per-frame difference
    return [c1[i]-dif[i] for i in range(3)] # subtract that difference

RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)

FADE_SPEED = 80 # no of frames for shifting

colors = (RED, BLACK, BLUE) # to allow you to iterate over the colors

SIZE = (1000,1000)
screen = pygame.display.set_mode(SIZE)

# to cycle through the colors
cycle = itertools.cycle(colors)

## needed for fading
c_color = cycle.next() # RED    current_color
n_color = cycle.next() # BLACK  next_color
frames = FADE_SPEED        
## --------------

clock = pygame.time.Clock() # regulate fps

while True:
    # handling events
    for event in pygame.event.get():
        if event.type == pygame.QUIT: # close window event
            pygame.quit()

    c_color = fade_into(c_color, n_color, frames) # get next color

    pygame.draw.circle(screen,map(int,c_color),(500,500),200)
    pygame.display.flip()

    frames -= 1
    if frames == 0: # translation complete
        frames = FADE_SPEED
        n_color = cycle.next() # get next color

    clock.tick(40) # run at maximum of 40 frames per second

If you have any doubts, please comment below..

1
votes

Pygame does not have scene graph, so you need to redraw your shape in a loop and call display.flip() to update.