gpt4 book ai didi

python - 在不组合 alpha 的情况下将一个 Surface 传送到另一个 Surface

转载 作者:太空宇宙 更新时间:2023-11-04 02:14:44 26 4
gpt4 key购买 nike

在我正在处理的一个 pygame 项目中,角色和对象的 Sprite 会在地形上转换阴影。阴影和地形都是正常的 pygame 表面,因此,为了显示它们,阴影被 blit 到地形上。当没有其他阴影(只有一个阴影和地形)时一切正常,但是当角色走进阴影区域并转换自己的阴影时,两个阴影结合了它们的 alpha 值,使地形变得更加模糊。我想要的是避免这种行为,保持 alpha 值稳定。有什么办法吗?

编辑:这是我在 Photoshop 中制作的图像,用于显示问题 enter image description here

EDIT2:@sloth 的回答没问题,但我忽略了评论我的项目比这更复杂。阴影不是整个正方形,而是更类似于"template"。与真实阴影一样,它们是从中转换的对象的轮廓,因此它们需要与 colorkey 和整个 alpha 值不兼容的每像素 alpha。

这是一个YouTube video这更清楚地说明了问题。

最佳答案

解决这个问题的一个简单方法是首先在另一个具有 alpha 值但没有每个像素 alpha 的 Surface 上 blit 阴影。然后将 Surface block 传送到您的屏幕。

这是一个显示结果的简单示例:

from pygame import *
import pygame

pygame.init()
screen = pygame.display.set_mode((800, 600))

# we create two "shadow" surfaces, a.k.a. black with alpha channel set to something
# we use these to illustrate the problem
shadow = pygame.Surface((128, 128), pygame.SRCALPHA)
shadow.fill((0, 0, 0, 100))
shadow2 = shadow.copy()

# a helper surface we use later for the fixed shadows
shadow_surf = pygame.Surface((800, 600))
# we set a colorkey to easily make this surface transparent
colorkey_color = (2,3,4)
shadow_surf.set_colorkey(colorkey_color)
# the alpha value of our shadow
shadow_surf.set_alpha(100)

# just something to see the shadow effect
test_surface = pygame.Surface((800, 100))
test_surface.fill(pygame.Color('cyan'))

running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False

screen.fill(pygame.Color('white'))

screen.blit(test_surface, (0, 150))

# first we blit the alpha channel shadows directly to the screen
screen.blit(shadow, (100, 100))
screen.blit(shadow2, (164, 164))

# here we draw the shadows to the helper surface first
# since the helper surface has no per-pixel alpha, the shadows
# will be fully black, but the alpha value for the full Surface image
# is set to 100, so we still have transparent shadows
shadow_surf.fill(colorkey_color)
shadow_surf.blit(shadow, (100, 100))
shadow_surf.blit(shadow2, (164, 164))

screen.blit(shadow_surf, (400, 0))

pygame.display.update()

enter image description here

关于python - 在不组合 alpha 的情况下将一个 Surface 传送到另一个 Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52910848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com