-1

I've created a surface that I've used pixel array to put pixels on, but i want to make the surface transparent but leaving the pixels opaque, I've tried making the surface transparent then drawing the pixels tot he surface but that just makes the pixels also transparent, any help or something I've missed?

-Edit- Hopefully this'll help in some way, this is the class object that creates the surface that is the galaxy

Also I have stated what I've tried, there's not much more to tell

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])
Ctrl
  • 123
  • 5
  • 1
    Is there code you've written that you can show us? – Taryn East Jan 08 '15 at 07:58
  • not particularly, I've created the surface then used an algorithm to populate the surface with pixels to resemble a galaxy, then passes the surface off to a function to put the surface on the screen – Ctrl Jan 08 '15 at 08:29
  • is that function the thing that you need to change to make stuff work? if so - we need to see it to help you. if not - then your question is not specified well enough... one way or another, you need to tell us what it is that you are using to add those pixels to the screen... if you have *written software* to do that, then you need to include it... if you have just *used software* to do that, then you are on the wrong site to get help... and probably need to try SuperUser instead of Stackoverflow (try looking under the StackExchange button at top left) – Taryn East Jan 08 '15 at 23:39
  • It's super-important that you post your code here, or just a relevant part of it, so that we can understand the problem and fix it. I've posted an answer from my own experiences with this _sort of_ issue, but, really, we aren't here to tell you what _we_ did, we're here to help you with what _you're doing._ Without knowing what that is, all we can offer are guesses and generalizations, which aren't really the motivation of the StackOverflow project, as I understand it. Post your code that generates the galaxy, toss in some comments that explain what you want, and we can fix it! :D – Augusta Jan 09 '15 at 00:53
  • Telling us what you've already tried (and how you tried it) will also give us a starting point for solutions, as well as satisfying the grumpier-types who don't want to feel like they're doing your homework for you. You'll get a better result, and your questions will last longer. – Augusta Jan 09 '15 at 00:57
  • @Ctrl I think you need to indent the contents of `class Galaxy`... Also, what do we need to import to get the module `noise`? Could this be it? https://pypi.python.org/pypi/noise/1.2.1 – Augusta Jan 09 '15 at 02:33
  • the class is properly indented in my code, it just comes out weird here, also if it was incorrectly indented i would have gotten an error, there is no error here, i was looking for advice, also the noise is a module called perlin noise, which is properly imported – Ctrl Jan 09 '15 at 09:06

1 Answers1

1

Use MyGalaxy.set_colorkey(SomeUnusedRGB) to define a zero-alpha (invisible) background colour, fill MyGalaxy with that colour, then draw the pixels on top of that. You can use pixelArray functions to draw to that surface, but you're probably better to use MyGalaxy.set_at(pixelLocationXY, pixelColourRGB) instead, for reasons of managability and performance.

Make sure that SomeUnusedRGB is never the same as any pixelColourRGB, or those pixels won't appear (since pygame will interpret them as invisible). When you blit MyGalaxy to wherever you want it, it ought to only blit the non-SomeUnusedRGB-coloured pixels, leaving the rest unaltered.

(This is the best I can offer you without knowing more about your code; revise the question to include what you're already trying, and I'll update this answer.)

Augusta
  • 7,171
  • 5
  • 24
  • 39