【问题标题】:How can I make the background of a surface transparent in pygame如何在pygame中使表面的背景透明
【发布时间】:2020-01-11 17:15:06
【问题描述】:

在 pygame 中,我在 Surface 上绘制图形。我希望 Surface 本身是透明的,但图像可见。我尝试在 Surface 上使用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),我会得到相同的结果。任何建议,将不胜感激。谢谢。

附:我正在尝试绘制一个 NetLogo 标准“乌龟”。

【问题讨论】:

  • 0 表示完全透明,因此像素是不可见的 - 试试 ie。 128
  • set_alpha 为所有像素设置相同的透明度。可能使用colorkey() 仅为某些像素设置透明度。

标签: python pygame


【解决方案1】:

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


先用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()

编辑:

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()

【讨论】:

  • 谢谢。我尝试了convert_alpha() 方法,解决了这个问题。还没有尝试过其他方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2023-01-07
  • 2019-08-14
  • 2013-06-09
  • 2011-10-02
相关资源
最近更新 更多