Stam Kaly
Stam Kaly

Reputation: 668

pygame-How to darken screen

I want to darken current screen a bit before poping up a new surface. I know there is no function in pygame to do this and that I will have to process the image to darken it. But as long as I know the only way to get current displaying surface in pygame is by saving it to disk as a file which slows down the game. Is there any other way to do this with pygame? Like saving the image to a value in memory so that I can process it without saving it somewhere.

Thanks in advance,

Stam

Upvotes: 1

Views: 3906

Answers (1)

jsbueno
jsbueno

Reputation: 110301

You don't need to save anything to a file. When you read an image to a file, it is a Surface object. You them blit this object to the screen. But these Surface objects have the same methods and properties than the object working as the screen - (which is also a Surface): you can draw primitives, and blit other images to them - all in memory.

So, once you read your image, just make a copy of it, draw a filled rectangle with a solid transparent color on it to darken it, and then blit it to the screen. Repeat the process increasing the transparency level and pasting it on the screen again if you want a fade in effect.

import pygame
screen = pygame.display.set_mode((640,480))
img = pygame.image.load("MYIMAGE.PNG")

for opacity in range(255, 0, -15):
     work_img = img.copy()
     pygame.draw.rect(work_img, (255,0, 0, opacity),  (0,0, 640,480))
     screen.blit(work_img, (0,0))
     pygame.display.flip()
     pygame.time.delay(100)

Upvotes: 3

Related Questions