Pygame中的加法混合不支持透明度

0 投票
1 回答
584 浏览
提问于 2025-04-18 10:59

我最近发现了在pygame中可以对绘制的表面应用不同的混合模式,我想看看这个系统有多灵活。除非我做错了什么,不然看起来这个功能挺有限的(就像pygame的其他部分一样,哎呀,真是直言不讳)。我写了一个简单的程序,用透明度画了一堆渐变圆圈,并把它们分散在屏幕上。这是代码:

import pygame
import pygame.gfxdraw
pygame.init()

import random

SCREEN = pygame.display.set_mode((800, 600))
SCREEN.fill((0, 0, 0))

def draw_square(surface, colour, x, y):
    """
    Yeah it's called draw square but it actually draws a circle thing I was just too lazy
    to change the name so you gotta deal with it.
    """
    square = pygame.Surface((100, 100))
    square.fill((0, 0, 0))
    colour += (int(15/255*100), )
    for i in range(25):
        pygame.gfxdraw.filled_circle(square, 50, 50, i*2, colour)
    # Comment the previous two lines out and uncomment the next line to see different results.
    # pygame.draw.circle(square, colour[:3], (50, 50), 50)
    surface.blit(square, (x - 50, y - 50), special_flags=pygame.BLEND_RGB_ADD)

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

    draw_square(SCREEN, (25, 255, 25), random.randint(0, 800), random.randint(0, 600))

    pygame.display.update()
pygame.quit()

在画普通圆圈的时候,这个方法似乎能正常工作,但当我用pygame.gfxdraw.filled_circle画圆圈时,加法混合就不管用了。有没有什么想法?

补充:我用的是Python 3,所以15/255能正确计算成一个小数。

1 个回答

1

问题出在这一行代码:

colour += (int(15/255*100), )

它本来应该一开始变成白色,但因为透明度设置得太低,所以需要很长时间才能看到效果(理论上应该是这样...)。

如果这样做:

colour += (int(125/255*100), )

效果就会更加明显。

结果是:

enter image description here

撰写回答