I want to decreased the alpha value of a rectangle until it gets completely transparent, It works fine in 1 while loop, but I want to do it in a while loop inside a while loop.
The code below works and is just for demonstration, therefore i didn't write things like pygame.display.update()
:
screen = pygame.display.set_mode((800,800))
red = 255
green = 0
blue = 0
alpha = 255 # 255 is opaque, and a value of 0 is completely transparent.
while True:
rectangle = pygame.Surface((40,40),pygame.SRCALPHA)
rectangle.fill((red,green,blue,alpha))
if alpha != 0:
alpha -= 1
screen.blit(rectangle,(0,0)
....
The problem starts to happen if I to do the same but inside another while loop:
while True:
while True:
rectangle = pygame.Surface((40,40),pygame.SRCALPHA)
rectangle.fill((red,green,blue,alpha))
if alpha != 0:
alpha -= 1
screen.blit(rectangle,(0,0)
....
I have figured it out.
Essentially, when you create a new while
loop you are creating a new game, therefore you must call pygame.Surface.fill((color))
.
If you don't call this function, the changes on alpha won't be visible on the screen.