防止不透明度与创建的每个新对象相乘

问题描述

我有这个类调用一个透明函数来在我的外圆后面创建一个透明的内圆,问题是这个圆的不透明度与导致这个问题的每个对象相乘:

enter image description here

这不是我想要的,我对如何在每个圆圈中只调用一次这个函数没有太多想法,我之前尝试将函数调用放在 init def 本身中,但它不记得了它做我想做的事。

最后,所有的圆圈都应该是这样的

enter image description here

这是我的类和不透明度函数,称为透明(我知道它应该被称为不透明度,而不是我只是 pepega),以及我通过另一个文件调用该类的函数

import pygame

def transparency(self,surface,inner_surface,size,x,y):
        self.circle = pygame.draw.circle(inner_surface,(255,255,10),(x,y),int(size[0]/2)-5)
        surface.blit(inner_surface,(0,0))
        surface.blit(self.image,(x-((size[0])/2),y-((size[1])/2)))

class circles(pygame.sprite.Sprite): # Initalise a new circle with set position and size (defined by cs)

    def __init__(self,y,cs,font):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("./assets/circle.png").convert_alpha()
        size = (int(round(2.25*(109-(9*cs)))),int(round(2.25*(109-(9*cs)))))
        self.image = pygame.transform.scale(self.image,size)
        transparency(self,y)
        pygame.display.update()

def Place_circles(screen,curve,circle_space,font):
    inner = inner_init([GetSystemMetrics(0),GetSystemMetrics(1)])
    Circle_list = []
    idx = [0,0]
    for c in reversed(range(0,len(curve))):
        for p in reversed(range(0,len(curve[c]))):
            dist = math.sqrt(math.pow(curve[c][p][0] - curve[idx[0]][idx[1]][0],2)+math.pow(curve [c][p][1] - curve[idx[0]][idx[1]][1],2))
            if dist > circle_space:
                print(dist)
                print(idx)
                idx = [c,p]
                Circle_list.append(circles.circles(screen,inner,curve[c][p][0],curve[c][p][1],4,font))

解决方法

导入pygame

def 透明度(self,surface,inner_surface,size,x,y): self.circle = pygame.draw.circle(inner_surface,(255,255,10),(x,y),int(size[0]/2)-5) surface.blit(inner_surface,(0,0)) surface.blit(self.image,(x-((size[0])/2),y-((size[1])/2)))

inner_surface 与屏幕大小相同。所有透明圆圈都绘制在 inner_surface 上。每次将 blit inner_surface 显示在屏幕上时,inner_surface 上绘制的任何圆圈都会变得更加不透明。您根本不需要 inner_surface。创建一个临时的表面。在临时Surface 上绘制圆圈并在屏幕上blit

def transparency(self,y):
    temp_surface = pygame.Surface(size,pygame.SRCALPHA)
    self.circle = pygame.draw.circle(temp_surface,(size[0]//2,size[1]//2),int(size[0]/2)-5)
    topleft = x - size[0] // 2,y - size[1] // 2
    surface.blit(inner_surface,topleft)
    surface.blit(self.image,topleft)

另见Draw a transparent rectangle in pygame
How to make transparent pygame.draw.circle

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...