pygame:覆盖透明图像显示新旧图像

问题描述 投票:0回答:0

我无法弄清楚如何在不显示覆盖图像的情况下将图像绘制到另一个图像上。 示例:我有一张显示“thumpsup”图标的图像。在多种情况下,它应该显示相同的图像,但显示为“红色”而不是“绿色”。所以我创建了两个不同的图像。 代码可以正常工作、缩放、旋转和翻转图像,除了旧图像仍然可见,因为 alpha 通道不知何故没有被覆盖..因此我看到了它们-

import sys
import pygame, pygame_widgets
import Globals
from Globals import pictures_path
from Button import Button

pygame.init()
win = pygame.display.get_surface()
pygame.display.set_mode((400,400))

# images
image = Globals.images[pictures_path]["icons"]["thumps_up.png"]
image = pygame.transform.scale(image,(20,20))

image_red = Globals.images[pictures_path]["icons"]["thumps_upred.png"]
image_red = pygame.transform.flip(pygame.transform.scale(image_red,(20,20)),True,True).convert_alpha()


b  = Button(   win,             x=200,
                                y=200,
                                width=12,
                                height=12,
                                image=image,
                                onClick= lambda: Button.setImage(b,image_red),
                                transparent=True,
                                image_hover_surface_alpha=255,

                                tooltip="no tooltip set yet !!!",)


def quit_game(events):
    # quit the game with quit icon or esc
    for event in events:
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                sys.exit()

run = True
while run:
    events = pygame.event.get()
    quit_game(events)
    pygame_widgets.update(events)
    pygame.display.update()

然后按钮正在调用:

    def setImage(self, image):

        del(self.image)
        copy_ = image
        copy_rect = copy_.get_rect()
        self.image = copy_
        self.imageRect =copy_rect

        self.alignImageRect()

这对透明和不透明的图像都很好,除了上面的问题。

如何删除、覆盖或重置图像?

del(image),创建一个表面并填充它,然后在这个表面上blit。简单地覆盖

python image pygame reset transparent
© www.soinside.com 2019 - 2024. All rights reserved.