2

I'm trying to make a user interface thing transparent in my game when the mouse isn't hovering over it. But for some reason, when I set the alpha value of the image for it to become transparent, nothing happens. Here is some runnable code for it that replicates the problem:

import pygame
WHITE = (255, 255, 255)

class UI:
    def __init__(self):
        self.img = pygame.image.load("ink_bar_solid.png")
        self.img.set_alpha(0)
        self.ink_bar_rect = self.img.get_bounding_rect()
        self.x, self.y = 0, 10

resolution = (500, 500)
screen = pygame.display.set_mode(resolution)
mouse = pygame.mouse.get_pos
ink_bar = UI()
run = True

def mouse_over():
    if ink_bar.ink_bar_rect.collidepoint(mouse()):
        ink_bar.img.set_alpha(255)
    else:
        ink_bar.img.set_alpha(0)

while run:
    mouse_over()
    screen.fill(WHITE)
    screen.blit(ink_bar.img, (ink_bar.x, ink_bar.y))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            break
    pygame.display.flip()
pygame.quit()

Any help is greatly appreciated! Edit: I got a comment from someone who said they used their own image and it worked fine... I'm getting this warning when I execute the program:

libpng warning: iCCP: known incorrect sRGB profile

Is the reason why it doesn't blit properly because of my file?

lakam99
  • 575
  • 4
  • 9
  • 24
  • 1
    I'm pretty sure the problem is simply that you're incorrectly blitting your `ink_bar`. Just remove the `self.` in front of each variable name (`self.` is what you use from within the object to refer to itself) like so `(ink_bar.x, ink_bar.y)` in your blit call. After that small modification (and using a 256 by 256 image I loaded myself), the object is invisible when not hovered over, and vice-versa. – underscoreC Nov 04 '18 at 07:58
  • Sorry the selfs in the positional argument were typos. Originally I had it as (0, 0) but changed it to the image's positions here for clarification. The problem still oddly persists for me regardless what goes in the positional argument. – lakam99 Nov 04 '18 at 08:11

1 Answers1

3

The set_alpha method doesn't seem to work for unconverted png files. Calling the convert method will also improve the blit performance drastically:

self.img = pygame.image.load("ink_bar_solid.png").convert()

It also doesn't work for per-pixel alpha surfaces (surfaces converted with convert_alpha or created with the pygame.SRCALPHA flag). The alpha of per-pixel surfaces can be changed by filling them with a transparent white color and passing the pygame.BLEND_RGBA_MULT special flag, e.g.:

image = pygame.image.load('an_image.png').convert_alpha()
# Make a copy so that the original doesn't get modified.
transparent_image = image.copy()
transparent_image.fill((255, 255, 255, 100), special_flags=pygame.BLEND_RGBA_MULT)
skrx
  • 19,980
  • 5
  • 34
  • 48
  • 1
    Actually, it looks like it only doesn't work with unconverted png files. It works with jpgs. I need to investigate why that's the case and change the answer. – skrx Nov 04 '18 at 09:22
  • Analyzing the source code is a bit too much work for me atm, but maybe the devs know why pngs have to be converted. Here's the mailing list: https://www.pygame.org/wiki/info – skrx Nov 04 '18 at 11:09