0

I am trying to put in a background image for my game and I need that at certain opacity. I have used the method set_alpha() of surface to control its translucency. However, this is creating a fade-in effect with the initial background at the set opacity turning to its full brightness eventually. Is there any way I can set the translucency of the background to a constant value?

My code is

self.bg = pygame.image.load("../game_images/background.png")
self.bg_size = self.bg.get_size()
self.bg_rect = self.bg.get_rect()
self.bg.set_alpha(5)
self.screen = pygame.display.set_mode(self.bg_size,pygame.FULLSCREEN)
self.screen.blit(self.bg, self.bg_rect)
pygame.display.update()

Any hints on how to do this?

fuesika
  • 3,280
  • 6
  • 26
  • 34
BrownBeast
  • 21
  • 1
  • 9
  • This shouldn't create a fade-in effect. Can you provide a standalone example? – sinan Aug 30 '14 at 11:00
  • @sinan You were correct. This does not create such an effect. But the piece of code I have written was in a function that is being called in a loop which was the cause of the problem. I didn't know that earlier. Thank you so much :) – BrownBeast Aug 31 '14 at 15:07

1 Answers1

0

It would help to see game loop.

Your image probably is getting bright because you not clearing the screen before it redrawn.

screen.fill((0,0,0))
screen.blit(self.bg, self.bg_rect)
pygame.display.update()

if you decide to do a self.bg.convert_alpha(). Then you need to use pygame.surfarray.pixels_alpha() and this require numpy to be install.

# value is a float .01 to .99
def set_alpha(image, value):
    array = pygame.surfarray.pixels_alpha(image)
    for x in xrange(array.shape[0]):
        for y in xrange(array.shape[1]):
            array[x][y] = int(array[x][y] * value)
Windspar
  • 36
  • 4
  • Your solution of clearing the screen before redrawn is working! But I now have an associated problem. If I'm clearing the screen everytime, the foreground also becomes blank and it has looks as if it is blinking little. How can I rectify this? – BrownBeast Aug 31 '14 at 14:58
  • Never mind, got it. Just removed this piece of code from the loop – BrownBeast Aug 31 '14 at 15:05