创建一个透明的表面来在pygame中绘制像素

时间:2015-01-08 07:33:22

标签: python pygame pygame-surface

我已经创建了一个表面,我已经使用了像素阵列来放置像素,但我想让表面透明但让像素不透明,我尝试使表面透明然后绘制表面上的像素,但这只会使像素透明,任何帮助或我错过的东西?

-Edit-希望这会以某种方式提供帮助,这是创建星系表面的类对象

此外,我已经陈述了我尝试过的内容,还有更多要说的

class Galaxy(object):



def __init__(self,posx=0,posy=0,radius=0,depth=0):
    radius = int(radius)
    self.size = [radius*2,radius*2,depth]
    self.posx = posx
    self.posy = posy
    self.radius = radius

    #create array for stars
    self.starArray = []

    #create surface for stars
    self.surface = pygame.Surface([radius*2,radius*2])
    self.starPixel = pygame.PixelArray(self.surface)

    #populate
    for x in range(radius*2):
        for y in range(radius*2):
            #generate stars
            num1 = noise.snoise2(x+posx,y+posy,repeatx=radius*10,repeaty=radius*10)
            distance = math.sqrt(math.pow((x-radius),2)+math.pow((y-radius),2))
            if distance < 0:
                distance = distance * -1

            #print(x,y,"is",distance,"from",radius,radius)

            val = 5

            #glaxy density algorithm
            num = (num1 / ( ((distance+0.0001)/radius)*(val*10) )) * 10

            #density
            if num > (1/val):
                #create star
                self.starArray.append(Stars(x,y,seed=num1*100000,distance=distance))
                #print(num*1000)
    self.addPixels()

#adds all star pixels to pixel array on surface
def addPixels(self):
    for i in self.starArray:
        self.starPixel[i.x,i.y] = i.colour
    del self.starPixel

#sends to screen to await rendering
def display(self):
    screen.displaySurface(self.surface,[self.posx+camPosX,self.posy+camPosY])

1 个答案:

答案 0 :(得分:1)

使用MyGalaxy.set_colorkey(SomeUnusedRGB)定义零-α(不可见)背景颜色,用该颜色填充MyGalaxy,然后在其上绘制像素。您可以使用pixelArray函数绘制到该表面,但出于管理和性能的原因,您最好使用MyGalaxy.set_at(pixelLocationXY, pixelColourRGB)

确保SomeUnusedRGB永远不会与任何pixelColourRGB相同,否则这些像素不会出现(因为pygame会将其解释为不可见)。当你将MyGalaxy blit到你想要的任何地方时,它应该只对非SomeUnusedRGB色像素进行blit,其余部分保持不变。

(这是我能在不了解您的代码的情况下为您提供的最佳内容;修改问题以包含您已尝试的内容,我将更新此答案。)