0

我正在尝试制作一个每像素的 2D 照明系统,但我不断收到一个奇怪的错误。这是我的代码以及沿途的解释:

import pygame, sys, math

white = (255, 255, 255)
black = (0, 0, 0)

screen_width = 640
screen_height = 480

pygame.init()
pygame.display.set_caption("Light sources")
screen = pygame.display.set_mode([screen_width, screen_height])
clock = pygame.time.Clock()

这些是任何 pygame 游戏的常规设置行

darkness = pygame.image.load("grafiikka/pimeys.png")
darkness.set_alpha(254)

lights_list = [] ### For controlling all light sources at a time

加载单色黑暗图像。这将在其他所有内容之上进行 blit 以使其变暗。然后,我将通过更改光照区域中黑暗的 alpha 值来模拟光照。

class lightsource: ### The parameters for a light
    def ___init___(self, loc, ang, direction, radius):
        self.loc = loc
        self.ang = ang
        self.dir = direction
        self.radius = radius

我做光源课

def LightDraw(): ### Drawing the lights, pixel per pixel
    for v in lights_list:
        for i in range (int((v.dir - v.ang/2)), int((v.dir + v.ang/2))):
            for l in range (0, v.radius):
                if int(i) < 90 and int(i) >= 0:
                    x = (v.loc[0]) + int(-l * math.cos(math.radians(i)))
                    y = (v.loc[1]) + int(-l * math.sin(math.radians(i)))
                elif i < 180 and i >= 90:
                    x = (v.loc[0]) + int(l * math.cos(math.pi - math.radians(i)))
                    y = (v.loc[1]) + int(-l * math.sin(math.pi - math.radians(i)))
                elif i < 270 and i >= 180:
                    x = (v.loc[0]) + int(l * math.sin(1.5 * math.pi - math.radians(i)))
                    y = (v.loc[1]) + int(l * math.cos(1.5 * math.pi - math.radians(i)))
                elif i < 360 and i >= 270:
                    x = (v.loc[0]) + int(-l * math.cos(2 * math.pi - math.radians(i)))
                    y = (v.loc[1]) + int(l * math.sin(2 * math.pi - math.radians(i)))
                alpha = int(254 * (l / v.radius))
                print(int(254 * (l / v.radius)))
                pygame.TempPimeys[x, y] = pygame.Color.a(alpha) ### THIS CAUSES ERROR

    return TempPimeys.make_surface()

### Adding a test light source at (300, 200) on screen    
testValo = lightsource()
testValo.___init___((300,200), 360, 90, 100)
lights_list.append(testValo)

错误在函数中,即在我实际更改 alpha 值的行中。我不明白到底是什么导致了错误。

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    myOtherImage_rect.topleft = pygame.mouse.get_pos()

    screen.fill(white)
    TempPimeys = pygame.PixelArray(pimeys)
    hämärä = LightDraw()
    screen.unlock()
    screen.blit(hämärä, (0, 0))
    pygame.display.update()

和一般循环。

我得到的错误是: TypeError: 'getset_descriptor' object is not callable

4

1 回答 1

0
pygame.TempPimeys[x, y] = pyame.Color.a(alpha) ### THIS CAUSES ERROR

# same as
>>> pygame.Color.a(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'getset_descriptor' object is not callable

当它期望成为一个实例时,您将a其作为类方法调用。pygame.Color.a()前任:

>>> pygame.Color('red')
(255, 0, 0, 255)
>>> c = pygame.Color('red')
>>> c.a
255
>>> c.a = 20

如果原因是您只想修改alpha而不是RGB,那么您可以使用pygame.surfarray.pixels_alpha

于 2013-07-10T21:21:39.413 回答