拼命想要了解pygame半透明

时间:2014-04-18 13:36:51

标签: python pygame

我试图弄清楚如何制作一个简单的函数,在pygame中将一个半透明的圆圈绘制到给定的表面,过去的一小部分我一直在无方向地工作。我做了我的研究,我发现很多人建议我简单地将圆圈绘制到一个启用了SRCALPHA的临时表面,然后将那个表面放在我正在绘制的真实表面上。但我认为这是我在下面实施的,不是吗?

import pygame

SCREEN_DIMENSIONS = w, h = 800, 600
screen = pygame.display.set_mode(SCREEN_DIMENSIONS)

FPS = 60
clock = pygame.time.Clock()

def draw_alpha_circle(screen, colour, position, radius, thickness=0):
    *colour, alpha = colour # Separate the colour from the alpha
    # Note: (assuming colour is a 4-tuple (r, g, b, a))
    #   *colour, alpha = colour
    #
    # is equivalent to:
    #   r, g, b, alpha = colour; colour = r, g, b
    x, y = position
    d = 2*radius

    temp_surface = pygame.Surface((d, d))
    temp_surface.set_alpha(alpha)

    pygame.draw.circle(temp_surface, colour, position, radius, thickness)
    screen.blit(temp_surface, (x - radius, y - radius))

running = True
while running:
    clock.tick(FPS)
    screen.fill((0, 0, 0))
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            running=False

    draw_alpha_circle(screen, (255, 0, 0, 128), (w//2, h//2), 20)

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

这实际上根本没有吸引屏幕。我完全不知道是什么导致它绝对没有任何吸引力。有人可以帮我一把吗?如果有任何帮助,我正在运行Python 3.2.3。

作为一个附带问题;为什么pygame半透明难以理解?应该是引擎中的其他所有东西都应该是直截了当的,但是在我看来,这种情况非常缺乏记录并且难以使用。

编辑:现在我很困惑,因为即使以下代码也无法正常运行:

def draw_alpha_circle(screen, colour, position, radius, thickness=0):
    *colour, alpha = colour # Separate the colour from the alpha
    # Note: (assuming colour is a 4-tuple (r, g, b, a))
    #   *colour, alpha = colour
    #
    # is equivalent to:
    #   r, g, b, alpha = colour; colour = r, g, b

    x, y = position
    d = 2*radius

    temp_surface = pygame.Surface((d, d))
    # Doesn't even draw real alpha, I just wanted to test out if it draws properly without alpha, which it doesn't.

    pygame.draw.circle(temp_surface, colour, position, radius, thickness)
    screen.blit(temp_surface, (x - radius, y - radius))

这里发生了什么?我完全感到困惑!我是疯了吗?你们实际上是在圈子里吗?我只是觉得没有圈子?我发誓这个pygame半透明是为了得到我。

1 个答案:

答案 0 :(得分:0)

......我是个白痴。

修复是在临时表面上绘制圆而不是在正常表面上的位置,而是在位置(radiusradius)。以下功能完美无缺。

def draw_alpha_circle(screen, colour, position, radius, thickness=0):
    *colour, alpha = colour # Separate the colour from the alpha
    # Note: (assuming colour is a 4-tuple (r, g, b, a))
    #   *colour, alpha = colour
    #
    # is equivalent to:
    #   r, g, b, alpha = colour; colour = r, g, b

    x, y = position
    d = 2*radius

    temp_surface = pygame.Surface((d, d))
    temp_surface.set_alpha(alpha)

    pygame.draw.circle(temp_surface, colour, (radius, radius), radius, thickness)
    screen.blit(temp_surface, (x - radius, y - radius))

但是我不打算删除这个问题,我会把它作为我的dumbassery的纪念碑。

相关问题