带有alpha的Pygame曲面,而不是bliting透明度

2024-05-14 15:24:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让一个用户界面在我的游戏中变得透明,当鼠标不停在上面的时候。但由于某些原因,当我设置图像的alpha值使其变得透明时,什么都不会发生。下面是一些可运行的代码,用于复制问题:

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()

非常感谢任何帮助! 编辑:有人说他们用自己的形象,效果很好。。。当我执行程序时会收到以下警告:

^{pr2}$

是因为我的文件而不能正常闪烁的原因吗?在


Tags: runrectselfalphaeventimggetbar
1条回答
网友
1楼 · 发布于 2024-05-14 15:24:56

set_alpha方法似乎不适用于未转换的png文件。调用^{}方法也将显著提高blit性能:

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

它也不适用于每像素alpha曲面(使用convert_alpha转换或使用pygame.SRCALPHA标志创建的曲面)。每像素表面的alpha可以通过填充透明的白色并传递pygame.BLEND_RGBA_MULT特殊标志来改变,例如:

^{pr2}$

相关问题 更多 >

    热门问题