【问题标题】:Prevent Opacity from multiplying with every new objects created防止不透明度与创建的每个新对象相乘
【发布时间】:2021-05-17 23:52:01
【问题描述】:

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

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

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

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

import pygame

def transparency(self,surface, inner_surface, size, x, y):
        self.circle = pygame.draw.circle(inner_surface, (255, 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,surface, inner_surface, x, 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, surface, inner_surface, size, x, 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))

【问题讨论】:

    标签: python random pygame opacity polyline


    【解决方案1】:

    导入游戏

    def 透明度(self,surface, inner_surface, size, x, y): self.circle = pygame.draw.circle(inner_surface, (255, 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 上。每次您将blitinner_surface 放到屏幕上时,在inner_surface 上绘制的任何圆圈都会变得更加不透明。你根本不需要inner_surface。创建一个临时Surface。在临时Surface上画一个圆圈,然后在屏幕上blit它:

    def transparency(self,surface, size, x, y):
        temp_surface = pygame.Surface(size, pygame.SRCALPHA)
        self.circle = pygame.draw.circle(temp_surface, (255, 255, 255, 10), (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

    【讨论】:

    • 我明白了,这基本上相当于在我的 for 循环中移动 inner_surface 来放置这些圆圈,但我想这是可行的,我将重新编写我的整个代码,因为它使用 25% 的 CPU屏幕上什么都没有,无论如何这解决了我的问题
    • @Gess1t 不要在 for 循环内移动 inner_surface。这将导致性能影响。 inner_surface 有屏幕大小,但你只需要一个 Surface 图片大小。
    • 我忘了问,如果我试图删除一个圆圈会怎样?那不会删除后面的像素吗?我对我的实施产生了一些疑问。此外,即使不移动我的内表面并删除与之相关的所有代码,我仍然有 25% 的 CPU 使用率,所以就像我说的,我只是要重新设计所有内容。
    • @Gess1t 您无法从屏幕上移除对象。当你绘制一个对象时,它只是改变屏幕上的像素。如果你想移除一个对象,你已经重绘了整个场景。在每一帧中重绘整个场景是常见的做法。
    猜你喜欢
    • 1970-01-01
    • 2013-01-06
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2016-11-02
    • 2015-03-31
    相关资源
    最近更新 更多