我创建了一个使用像素阵列放置像素的表面,但是我想使该表面透明但使像素不透明,我尝试使该表面透明然后在其表面绘制像素,但这只是使像素也透明,有什么帮助或我错过的东西吗?
-编辑-希望这会有所帮助,这是创建表面(即银河系)的类对象
我也说了我尝试过的,没有什么可说的了
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])
使用MyGalaxy.set_colorkey(SomeUnusedRGB)
定义的零α(不可见)的背景颜色,填充MyGalaxy
与该颜色,然后绘制在其顶部的像素。您可以使用pixelArray
函数来绘制该表面,但是MyGalaxy.set_at(pixelLocationXY, pixelColourRGB)
出于可管理性和性能的考虑,您可能最好使用它。
确保该像素SomeUnusedRGB
永远不会与任何pixelColourRGB
像素相同,否则这些像素将不会出现(因为pygame会将其解释为不可见的)。当将MyGalaxy蒙版到任意位置时,它只应蒙版非SomeSomeUnusedRGB像素,其余像素保持不变。
(这是我可以在不进一步了解您的代码的情况下为您提供的最好的;将问题修改为包含您已经在尝试的内容,我将更新此答案。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句