python – 在pygame中绘制表面透明度?

我正在编写一个捕食者 – 猎物模拟使用
python和pygame进行图形表示.我正在制作它,所以你可以实际上与一个生物“互动”(杀死它,选择它并在世界各地跟随它等).现在,当你点击一个生物时,一个粗圆圈(由gfxdraw类中的各种消除锯齿的圆圈组成)附着它,这意味着你已经成功地选择了它.

我的目标是使该圆圈透明,但根据文档,您无法为绘制曲面设置alpha值.我已经看到了矩形的解决方案(通过创建一个单独的半透明表面,blitting它,然后在其上绘制矩形),但不是半填充圆.

你有什么建议?谢谢 :)

最佳答案 看看下面的示例代码:

import pygame

pygame.init()
screen = pygame.display.set_mode((300, 300))
ck = (127, 33, 33)
size = 25
while True:
  if pygame.event.get(pygame.MOUSEBUTTONDOWN):
    s = pygame.Surface((50, 50))

    # first, "erase" the surface by filling it with a color and
    # setting this color as colorkey, so the surface is empty
    s.fill(ck)
    s.set_colorkey(ck)

    pygame.draw.circle(s, (255, 0, 0), (size, size), size, 2)

    # after drawing the circle, we can set the 
    # alpha value (transparency) of the surface
    s.set_alpha(75)

    x, y = pygame.mouse.get_pos()
    screen.blit(s, (x-size, y-size))

  pygame.event.poll()
  pygame.display.flip()
点赞