Search code examples
pythonpygame

How to make an object fades color in pygame


I want a rectangle to fade from green to red for a life-bar, but couldn't find easy way to do it on internet.

I tried like this (just for going from red to yellow):

colorR = 0
colorG = 255
colorB = 0
change = True
while change:
     pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
     colorR += 1
     if colorR == 255:
          print("done")

I also tried to do it with for i in range(0, 255):

It says pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0) TypeError: invalid color argument

But when I just draw the rectangle like this :

colorR = 0
colorG = 255
colorB = 0
pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)

the rectangle is displayed in green.

Is my method correct, or do I have. to do it in a complete other way ? Thanks for the answer

edit : here's the whole program, there are maybe mistakes in there because the rectangle doesn't appear before it gets its final color

import pygame, math
pygame.init()
length = 1440
heigth  = 700
win = pygame.display.set_mode((length, heigth))#, pygame.FULLSCREEN)
menuImg = pygame.image.load('menu_img.jpg')
laser = pygame.image.load('projectile.png')
clock = pygame.time.Clock()
game = False
def rgw():  
    win.fill((0, 0, 255))
    if game:
        print("game")
        colorR = 0
        colorG = 255
        colorB = 0
        change = True
        while change:
            pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
            colorR += 1
            if colorR == 255:
                change = False
        pygame.display.flip()
    pygame.display.update()
run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            game = True

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_BACKSPACE:
                run = False

    rgw()
pygame.quit()

Solution

  • To make changes to the display surface visible you have to call pygame.display.flip() or pygame.display.update(). Furthermore I recommend to handle the events by either pygame.event.get() or pygame.event.pump().
    Note you have to do a delay in the loop, otherwise the color will fade rapidly. If the color is not in range [0, 255] you'll get "invalid color argument" error. That happens because the loop in your example never terminates. You missed change = False.

    anyway, since you have a application loop, there is no need to do the fade in a separate loop. Use the main application loop:

    game = True
    colorR = 0
    colorG = 255
    colorB = 0
    def rgw(): 
        global colorR, colorG, colorB
        win.fill((0, 0, 255))
        if game:
            pygame.draw.rect(win, (colorR, colorG, colorB), (10, 10, 200, 50), 0)
            if colorR < 255:
                colorR += 1
        pygame.display.update()
    
    run = True
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                game = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_BACKSPACE:
                    run = False
    
        rgw()
    
    pygame.quit()