Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 拼命想了解pygame的半透明性_Python_Pygame - Fatal编程技术网

Python 拼命想了解pygame的半透明性

Python 拼命想了解pygame的半透明性,python,pygame,Python,Pygame,在过去的一段时间里,我一直在无方向地工作,同时试图找出如何在pygame中制作一个简单的函数,在给定的曲面上绘制一个半透明的圆。我做了研究,发现很多人建议我只需在启用了SRCALPHA的情况下将圆绘制到一个临时曲面上,然后将该曲面拼接到我要绘制的真实曲面的顶部。但我想这就是我在下面实现的,不是吗 import pygame SCREEN_DIMENSIONS = w, h = 800, 600 screen = pygame.display.set_mode(SCREEN_DIMENSIONS

在过去的一段时间里,我一直在无方向地工作,同时试图找出如何在
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))
这是怎么回事?我完全困惑了!我是不是疯了,你们是不是真的有圆圈,而我只是觉得没有圆圈?我发誓这个游戏的半透明性会让我失望。

…我是个白痴

解决方法是在临时曲面上绘制圆,不是在法线曲面上的位置,而是在(
半径
半径
)的位置。下面的函数工作得很好

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))
我不会删除这个问题,但我会把它作为我哑巴的纪念碑