【问题标题】:How to make transparent pygame.draw.circle [duplicate]如何制作透明的pygame.draw.circle [重复]
【发布时间】:2020-04-05 03:56:39
【问题描述】:

如何使pygame.draw.circle透明(添加alpha级别),至于“surface”“set_alpha”? 我找到了一个解决方案,只是将pygame.draw.circle 的颜色更改为不那么亮

【问题讨论】:

  • 所以你想在一个表面上画一个透明的圆圈——也就是在位图中做一个圆形的“洞”?
  • 如果你与黑色背景混合,那么它必须不那么亮

标签: python python-3.x pygame


【解决方案1】:

您可以使用 Alpha 通道创建表面

 surface1 = screen.convert_alpha()

用透明颜色填充它 -

 surface1.fill([0,0,0,0])

用颜色画圆[R,G,B,Alpha]

 pygame.draw.circle(surface1, (255, 0, 0, 128), (300, 300), 200)

并在屏幕上粘贴它

 screen.blit(surface1, (0,0))

但 alpha 总是将对象颜色与背景颜色混合,因此它会降低亮度。


import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))#, depth=32)

surface1 = screen.convert_alpha()
surface1.fill([0,0,0,0])
pygame.draw.circle(surface1, (255, 0, 0, 128), (325, 250), 100)

surface2 = screen.convert_alpha()
surface2.fill([0,0,0,0])
pygame.draw.circle(surface2, (0, 255, 0, 128), (475, 250), 100)

surface3 = screen.convert_alpha()
surface3.fill([0,0,0,0])
pygame.draw.circle(surface3, (0, 0, 255, 128), (400, 350), 100)

screen.fill([255,255,255]) # white background
screen.blit(surface1, (0,0))
screen.blit(surface2, (0,0))
screen.blit(surface3, (0,0))

pygame.display.flip()

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

pygame.quit()    


GitHub 上的示例:furas/python-examples/pygame/transparency

【讨论】:

    【解决方案2】:

    您必须使用pygame.Surface。创建具有每像素 Alpha 图像格式的 Surface。例如:

    radius = 100
    circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)
    

    并在上面画一个透明的圆圈。圆的颜色必须有一个 alpha 通道

    pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)
    

    blit()Surface 到窗口。例如:

    window.blit(circle, (100, 100))
    

    例子:

    import pygame
    
    pygame.init()
    wndsize = (400, 400)
    window = pygame.display.set_mode(wndsize)
    clock = pygame.time.Clock()
    
    run = True
    while run:
        clock.tick(60)
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        window.fill(0)
        pygame.draw.rect(window, (0, 0, 255), (0, 0, 200, 400))
    
        radius = 100
        circle = pygame.Surface((radius*2, radius*2), pygame.SRCALPHA)   
        pygame.draw.circle(circle, (255, 0, 0, 128), (radius, radius), radius)
        window.blit(circle, (100, 100))
    
        pygame.display.flip()
    

    【讨论】:

      猜你喜欢
      • 2013-09-06
      • 1970-01-01
      • 2016-04-09
      • 2014-12-31
      • 2016-02-02
      • 2021-01-09
      • 2014-12-16
      • 2011-04-08
      相关资源
      最近更新 更多