1

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()
ForceBru
  • 43,482
  • 10
  • 63
  • 98
bappi
  • 137
  • 9
  • I have pygame.display.update() already but it is indented like the "color = ..." after the while loop, do I have to put it in the while ? – bappi Apr 05 '20 at 13:07

1 Answers1

1

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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • thanks @Rabbid76 but the rectangle doesn't appear before its color is yellow. Maybe if I post the whole program it will be easier to help me – bappi Apr 05 '20 at 13:21
  • so I add "pygame.time.delay()" after "colorR += 1", and I don't really understand what handle the events is (maybe the problem comes from here...) – bappi Apr 05 '20 at 13:29
  • I mean I don't understand what it does when I use it, except for when I use keyboard letters or mouse – bappi Apr 05 '20 at 13:35
  • @bappi The event handling handles internal events, too. On some systems it is necessary to update the display and to handle the clock correctly (e.g. windows message loop). – Rabbid76 Apr 05 '20 at 13:36