pygame 掩码中的透明位是黑色而不是透明的

transparent bits in pygame mask are black instead of being transparent

我有一个程序可以填充与另一个掩码重叠的掩码位,但是当我将重叠位的掩码 blit 到屏幕上时,透明位出于某种原因全黑了?该程序按预期工作,我尝试将重叠位的表面转换为每像素 alpha,但透明位为黑色

example gif

import pygame
import sprites

SCREEN_HEIGHT, SCREEN_WIDTH = 800, 800
running = True

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()

player = sprites.Block((100, 100))
block2 = sprites.Block((100, 100))
blocks = pygame.sprite.Group(block2)

block2.rect.topleft = 150, 150
block2.image.fill((0, 255, 0))

while running:
    events = pygame.event.get()

    screen.fill((100, 100, 100))
    for event in events:
        if event.type == pygame.QUIT:
            running = False

    player.move(screen.get_rect())
    screen.blit(player.image, player.rect)
    blocks.draw(screen)

    for block in blocks:
        offset = (player.rect.x - block.rect.x, player.rect.y - block.rect.y)
        colliding_bits = player.mask.overlap_mask(block.mask, offset)
        colliding_bits_image = colliding_bits.to_surface(setcolor=(0, 255, 0))

        screen.blit(colliding_bits_image, block.rect)

    clock.tick(144)
    pygame.display.flip()

包含精灵的代码 类:

import pygame


class Block(pygame.sprite.Sprite):
    def __init__(self, size):
        self.image = pygame.image.load("flappy_bird.png")
        self.rect = self.image.get_rect()
        self.mask = pygame.mask.from_surface(self.image)

        self.speed = 1

        super().__init__()

    def move(self, screen_rect):
        pressed_keys = pygame.key.get_pressed()

        if pressed_keys[pygame.K_w]:
            self.rect.move_ip(0, -self.speed)
        if pressed_keys[pygame.K_s]:
            self.rect.move_ip(0, self.speed)
        if pressed_keys[pygame.K_a]:
            self.rect.move_ip(-self.speed, 0)
        if pressed_keys[pygame.K_d]:
            self.rect.move_ip(self.speed, 0)

        self.rect.clamp_ip(screen_rect)

我将 unsetcolor 属性添加到 to_surface 方法并删除了 blocks.draw(screen) 行,它似乎产生了所需的结果:

from math import fabs
import pygame
import sprites

SCREEN_HEIGHT, SCREEN_WIDTH = 800, 800
running = True

pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()

player = sprites.Block((100, 100))
block2 = sprites.Block((100, 100))
blocks = pygame.sprite.Group(block2)

block2.rect.topleft = 150, 150
block2.image.fill((0, 255, 0))

while running:
    events = pygame.event.get()

    screen.fill((100, 100, 100))
    for event in events:
        if event.type == pygame.QUIT:
            running = False

    player.move(screen.get_rect())
    screen.blit(player.image, player.rect)

    for block in blocks:
        offset = (player.rect.x - block.rect.x, player.rect.y - block.rect.y)
        colliding_bits = player.mask.overlap_mask(block.mask, offset)
        colliding_bits_image = colliding_bits.to_surface(setcolor=(0, 255, 0, 255), unsetcolor=(0, 0, 0, 0))

        screen.blit(colliding_bits_image, block.rect)

    clock.tick(144)
    pygame.display.flip()