需要在Pygame中在曲面上闪烁透明度

2024-05-06 18:20:09 发布

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

我想用Pygame在我的游戏中产生一种盲目的影响。我在考虑制作一个表面,用黑色填充,然后在球员所在的表面上去掉一个颜色圈,这样你就可以看到球员了。我也想为火炬做同样的事。我想知道我是否能够在Pygame中擦除部分曲面


Tags: 游戏颜色表面pygame火炬球员曲面黑色
1条回答
网友
1楼 · 发布于 2024-05-06 18:20:09

可以使用alpha通道创建曲面(传递pygame.SRCALPHA标志),用不透明颜色填充曲面,然后在曲面上绘制透明颜色的形状(alpha值为0)

import pygame as pg


pg.init()
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
BLUE = pg.Color('dodgerblue4')
# I just create the background surface in the following lines.
background = pg.Surface(screen.get_size())
background.fill((90, 120, 140))
for y in range(0, 600, 20):
    for x in range(0, 800, 20):
        pg.draw.rect(background, BLUE, (x, y, 20, 20), 1)

# This dark gray surface will be blitted above the background surface.
surface = pg.Surface(screen.get_size(), pg.SRCALPHA)
surface.fill(pg.Color('gray11'))

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
        elif event.type == pg.MOUSEMOTION:
            surface.fill(pg.Color('gray11'))  # Clear the gray surface ...
            # ... and draw a transparent circle onto it to create a hole.
            pg.draw.circle(surface, (255, 255, 255, 0), event.pos, 90)

    screen.blit(background, (0, 0))
    screen.blit(surface, (0, 0))

    pg.display.flip()
    clock.tick(30)

pg.quit()

也可以使用另一个曲面而不是pygame.draw.circle来实现此效果。例如,您可以在图形编辑器中创建带有一些透明部分的白色图像,并将BLEND_RGBA_MIN作为特殊的_标志参数传递给^{},然后将其blit到灰色曲面上

brush = pg.image.load('brush.png').convert_alpha()

# Then in the while or event loop.
surface.fill(pg.Color('gray11'))
surface.blit(brush, event.pos, special_flags=pg.BLEND_RGBA_MIN)

相关问题 更多 >