如何在pygame中使曲面的背景透明

2024-06-02 05:51:59 发布

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

在pygame中,我在曲面上绘制一个图形。我希望表面本身是透明的,但图像是可见的。我尝试在表面上使用set_alpha,但图形本身保持透明

    self.original_image = Surface((self.rect.w, self.rect.h))
    self.original_image.set_alpha(0)

    pg.draw.polygon(self.original_image,
                    self.color,
                    [Vector2(gui.PATCH_SIZE, gui.PATCH_SIZE),
                     Vector2(gui.HALF_PATCH_SIZE( ), 0),
                     Vector2(0, gui.PATCH_SIZE),
                     Vector2(gui.HALF_PATCH_SIZE( ), gui.PATCH_SIZE*3/4),
                     ])

    self.image = self.original_image

我画的时候什么也看不见。当我省略self.original_image.set_alpha(0)时,我得到了我想要的图形,但是在一个黑色的背景矩形上,它掩盖了所有的内容。如果我尝试self.original_image.fill((0, 0, 0, 0))而不是self.original_image.set_alpha(0),我会得到相同的结果。如有任何建议,将不胜感激。谢谢

另外,我正试图画一个网络标志标准的“海龟”


Tags: rectimageselfalpha图形size绘制gui
1条回答
网友
1楼 · 发布于 2024-06-02 05:51:59

示例用绿色填充背景,然后用红色矩形/多边形绘制曲面


首先使用convert_alpha(),然后使用fill([0,0,0,0])填充,您可以绘制

在此版本中,您可以使用颜色[R,G,B,Alpha]为每个像素设置不同的透明度。但不能同时使用set_alpha(128)更改所有像素的透明度

import pygame

#  - constants  - (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED   = (255, 0, 0)

#  - main  -

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

surface = pygame.surface.Surface(screen.get_size()).convert_alpha()
surface.fill([0,0,0,0])
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])

clock = pygame.time.Clock()

running = True
while running:

    #  - events  -
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    #  - draws  -

    screen.fill(GREEN)

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

    pygame.display.flip()

    #  - FPS  -

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

#  - end  -

pygame.quit()

使用set_colorkey([0,0,0])blit()将不会复制颜色为[0,0,0]的像素

在此版本中,可以使用set_alpha(128)为所有像素设置相同的透明度,但不能使用颜色[R,G,B,Alpha]为每个像素分别设置不同的透明度

import pygame

#  - constants  - (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25 # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED   = (255, 0, 0)

#  - main  -

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0]) # don't copy color [0,0,0] on screen
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])

clock = pygame.time.Clock()

running = True
while running:

    #  - events  -
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    #  - draws  -

    screen.fill(GREEN)

    #surface.set_alpha(128)
    screen.blit(surface, (0,0))

    pygame.display.flip()

    #  - FPS  -

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

#  - end  -

pygame.quit()

编辑:

enter image description here

import pygame

#  - constants  - (UPPER_CASE_NAMES)

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

FPS = 25 # for more than 220 it has no time to update screen

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED   = (255, 0, 0)
BLUE  = (0, 0, 255)
YELLOW = (255,255,0)

#  - main  -

pygame.init()

screen = pygame.display.set_mode( (SCREEN_WIDTH, SCREEN_HEIGHT) )

#  -

background = pygame.surface.Surface(screen.get_size())

color = (128,128,128)
for x in range(0, SCREEN_WIDTH, 40):
    for y in range(0, SCREEN_HEIGHT, 40):
        pygame.draw.rect(background, color, [x,y,40,40])
        if color == (128,128,128):
            color = (192,192,192)
        else:
            color = (128,128,128)

#  -

surface = pygame.surface.Surface(screen.get_size())
surface.set_colorkey([0,0,0]) # don't copy color [0,0,0] on screen
pygame.draw.polygon(surface, RED, [(100,100), (200,200), (300,100)])
pygame.draw.polygon(surface, GREEN, [(100,300), (200,200), (300,300)])
pygame.draw.polygon(surface, BLUE, [(100,100), (200,200), (100,300)])
pygame.draw.polygon(surface, YELLOW, [(300,100), (200,200), (300,300)])
surface.set_alpha(128) # semi transparent all pixels

#  - mainloop  -

clock = pygame.time.Clock()

running = True
while running:

    #  - events  -

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

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_ESCAPE:
                running = False

    #  - draws  -

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

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

    pygame.display.flip()

    #  - FPS  -

    ms = clock.tick(FPS)
    #pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS, 16ms for 60FPS
    fps = clock.get_fps()
    pygame.display.set_caption('FPS: {}'.format(fps))

#  - end  -

pygame.quit()

相关问题 更多 >