如何在 Pygame 中绘制一个半透明的圆圈? [英] How to draw a semi-transparent circle in Pygame?
问题描述
我试图在 Pygame 中绘制一个半透明的圆圈.这是我的代码:
I'm trying to draw a semi-transparent circle in Pygame. Here is my code:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen=pygame.display.set_mode((width,height))
while True:
msElapsed = clock.tick(100)
screen.fill((0,0,0,255))
pygame.draw.circle(screen,(30,224,33,100),(250,100),10)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
我想使用 RGBA 作为圆形颜色.但是圆圈是全彩色的.我的代码有什么问题?
I want to use RGBA as circle color. But the circle is fully colored. What's the problem with my code?
推荐答案
修复:
首先你需要创建一个接受透明度的表面
First you need to create a surface which accepts transparency
surface = pygame.Surface((width,height), pygame.SRCALPHA)
然后从这一行:
pygame.draw.circle(screen,(30,224,33,100),(250,100),10)
将 screen
更改为 surface
看起来应该是这样的:
CHANGE screen
TO surface
SO IT SHOULD LOOK LIKE:
pygame.draw.circle(surface,(30,224,33,100),(250,100),10)
然后添加:
screen.blit(surface, (0,0))
说明:
当您在屏幕上绘制圆圈时,它会忽略透明度值,因为 pygame 屏幕无法接受它们.我所做的是创建了一个可以接受透明度的新层/屏幕(由于这个参数 pygame.SRCALPHA
),然后将该层/屏幕粘贴到原始屏幕的顶部.
When you were drawing the circle onto the screen it ignored the transparency values as the pygame screen cannot accept them. What I have done is created a new layer/screen which can accept transparency (due to this argument pygame.SRCALPHA
) and then pasted that layer/screen on top of the original screen.
这篇关于如何在 Pygame 中绘制一个半透明的圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!