在pygam中创建要绘制像素的透明曲面

2024-09-26 18:09:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个表面,我用像素数组来放置像素,但是我想让表面透明,但是让像素不透明,我试着让表面透明,然后把像素画到表面上,但这只会使像素也透明,有什么帮助或我遗漏的吗?在

-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])

Tags: inselffordefcreate像素surface表面
1条回答
网友
1楼 · 发布于 2024-09-26 18:09:18

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

确保SomeUnusedRGB与任何pixelColourRGB不同,否则这些像素不会出现(因为pygame会将它们解释为不可见的)。当你把MyGalaxy点到你想要的任何地方时,它应该只对一些不常用的rgb颜色的像素点进行blit,其余的像素点保持不变。在

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

相关问题 更多 >

    热门问题