I'm trying to create a chessgame using pygame. I got a screen with two layers blitted on it. One being the chessboard and the other one with the figures. In order to update the screen, I'm blitting the layers onto the Screen. First the chessboard, then the figure layer. However I couldn't find a way of doing this successfully without using a blend option as a special flag. This causes the figures to look different depending on which square they are (black, white background).
I tried doing it without the blending option but that results into me not being able to draw the chessboard layer.
The chessboard layer is drawn by using pygame.draw.rect().
The figures are drawn by using surface.blit().
Following code is the setup of the layers.
self.WIN = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
self.CHESSBOARD_LAYER = pygame.Surface((self.WIDTH,self.HEIGHT))
self.FIGURE_LAYER = pygame.Surface((self.WIDTH,self.HEIGHT))
Following code is being used in order to draw the chessboard and the figure layer.
self.WIN.fill("#000000")
self.WIN.blit(self.CHESSBOARD_LAYER,(0,0), special_flags=pygame.BLEND_RGB_ADD)
self.WIN.blit(self.FIGURE_LAYER,(0,0), special_flags=pygame.BLEND_RGB_ADD)
pygame.display.update()
Not using the .BLEND_RGB_ADD or other blend parameters results in a black screen with the image of the figure ontop of it (not drawing the chessboard).
I've also tried different blending options but didn't find one to work for me.
Image looks like this while the pawn on the black background looks as it should be but the other ones are affected by the background.
The problem is that your layer surface have no alpha channel. You create a Surface without alpha channel (RGB). You have to use the SRCALPHA flag to create a Surface with an alpha channel (RGBA). Also see pygame.Surface:
self.CHESSBOARD_LAYER = pygame.Surface((self.WIDTH, self.HEIGHT), pygame.SRCALPHA)
self.FIGURE_LAYER = pygame.Surface((self.WIDTH, self.HEIGHT), pygame.SRCALPHA)
Now you don't need any special blending flag at all:
self.WIN.fill("#000000")
self.WIN.blit(self.CHESSBOARD_LAYER, (0, 0))
self.WIN.blit(self.FIGURE_LAYER, (0, 0))
pygame.display.update()
Note, with the flag SRCALPHA each pixel has its own alpha value (RGBA format), without the flag the surfaces hast just 1 global alpha value and can not store different alpha values for the pixels (RGB format).
Is it possible to make a rect transparent in pygame?
I need it because I'm using rects as particles for my game. :P
pygame.draw functions will not draw with alpha. The documentation says:
Most of the arguments accept a color argument that is an RGB triplet. These can also accept an RGBA quadruplet. The alpha value will be written directly into the Surface if it contains pixel alphas, but the draw function will not draw transparently.
What you can do is create a second surface and then blit it to the screen. Blitting will do alpha blending and color keys. Also, you can specify alpha at the surface level (faster and less memory) or at the pixel level (slower but more precise). You can do either:
s = pygame.Surface((1000,750)) # the size of your rect
s.set_alpha(128) # alpha level
s.fill((255,255,255)) # this fills the entire surface
windowSurface.blit(s, (0,0)) # (0,0) are the top-left coordinates
or,
s = pygame.Surface((1000,750), pygame.SRCALPHA) # per-pixel alpha
s.fill((255,255,255,128)) # notice the alpha value in the color
windowSurface.blit(s, (0,0))
Keep in mind in the first case, that anything else you draw to s will get blitted with the alpha value you specify. So if you're using this to draw overlay controls for example, you might be better off using the second alternative.
Also, consider using pygame.HWSURFACE to create the surface hardware-accelerated.
Check the Surface docs at the pygame site, especially the intro.
Draw a transparent rectangle in pygame
I have had this question as a pygame user before, and this is a method of solving your problem.
I've just started learning some pygame (quite new to programming overall), and I have some very basic questions about how it works.
I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen. For example, when drawing a circle:
circle = pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)
I don't need to do screen.blit(circle), but when displaying text:
text = font.render("TEXT", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = screen.get_rect().centerx
screen.blit(text, textpos)
If I don't blit, the text won't appear.
To be honest, I really don't know what blitting is supposed to do, apart from "pasting" the desired surface onto the screen. I hope I have been clear enough.
The short answer
I haven't found a place yet that explains when I need to blit or not to include a certain surface on the screen.
Each operation will behave differently, and you'll need to read the documentation for the function you're working with.
The long answer
What Is Blitting?
First, you need to realize what blitting is doing. Your screen is just a collection of pixels, and blitting is doing a complete copy of one set of pixels onto another. For example, you can have a surface with an image that you loaded from the hard drive, and can display it multiple times on the screen in different positions by blitting that surface on top of the screen surface multiple times.
So, you often have code like this...
my_image = load_my_image()
screen.blit(my_image, position)
screen.blit(my_image, another_position)
In two lines of code, we copied a ton of pixels from the source surface (my_image) onto the screen by "blitting".
How do the pygame.draw.* functions blit?
Technically, the pygame.draw.* methods could have been written to do something similar. So, instead of your example...
pygame.draw.circle(screen, COLOR, POS, RADIUS, WIDTH)
...they COULD have had you do this...
circle_surface = pygame.draw.circle(COLOR, RADIUS, WIDTH)
screen.blit(circle_surface, POS)
If this were the case, you would get the same result. Internally, though, the pygame.draw.circle() method directly manipulates the surface you pass to it rather than create a new surface. This might have been chosen as the way to do things because they could have it run faster or with less memory than creating a new surface.
So which do I do?
So, to your question of "when to blit" and "when not to", basically, you need to read the documentation to see what the function actually does.
Here is the pygame.draw.circle() docs:
pygame.draw.circle():
draw a circle around a point
circle(Surface, color, pos, radius, width=0) -> Rect
Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.
Note that it says that "draws a shape on the surface", so it has already done the pixel changes for you. Also, it doesn't return a surface (it returns a Rect, but that just tells you where the pixel changes were done).
Now let's look at the pygame.font.Font.render() documentation:
draw text on a new Surface
render(text, antialias, color, background=None) -> Surface
This creates a new Surface with the specified text rendered on it. Pygame provides no way to directly draw text on an existing Surface: instead you must use Font.render() to create an image (Surface) of the text, then blit this image onto another Surface.
...
As you can see, it specifically says that the text is drawn on a NEW Surface, which is created and returned to you. This surface is NOT your screen's surface (it can't be, you didn't even tell the render() function what your screen's surface is). That's a pretty good indication that you will need to actually blit this surface to the screen.
Blit means 'BL'ock 'I'mage 'T'ranfser
When you are displaying things on the screen you will, in some way, use screen because that's where you are putting it.
When you do:
pygame.draw.circle(screen, (0, 0, 0), (100, 100), 15, 1)
you are still using screen but you are just not blitting because pygame is drawing it for you.
And when you use text, pygame renders it into an image then you have to blit it.
So basically you blit images, but you can also have pygame draw them for you. But remember when you blit an image, say over a background, you need to loop it back and fourth; so that it blits the background, then the image, then the background etc...
You dont need to know much more than that, but you can read all about it here Pygame Blit
I hope this helped. Good Luck!
Imagine that you are a painter:
You have a canvas, and a brush.
Let's say that your main screen surface will be your canvas, and all the other surfaces, are "in your head" - you know how to draw them already.
When you call blit, you paint on top of the surface, covering any pixels that were overlapped. That is why you need to repaint the whole screen black so that you won't have any smudges on the painting while moving an object.
As Mark already said, you can draw a circle with a function, or first blit it to a new surface, and blit that on the screen surface.
If you have a more complicated surface - curves, text etc. you wouldn't need to have a surface for that, so you don't have to do any expensive calculations, just drawing. The setback is that your program takes up more memory, so you have to choose between those 2.
I am using this sprite for some experiments. I cropped a section of the sheet with GIMP and made the blue background transparent, the result being this. However, when I run the codes using this sprite, I still end up with a sprite with a blue background. Same thing happens with other sprites (different sprite sheet, same source, same editing process).
Now, I know that I can set transparent colors using PyGame itself but I'm wondering if that's the only way I can do it? Can't I do it through the image itself? How come PyGame still reads a blue backgorund even after I have cleared it with GIMP?
P.S. SSCE would be pvz_shooter_test.py and/or image_test.py of the GitHub repo linked.
Try mySurface.convert_alpha() (check the pygame documentation about it).
Just some more additional details on how come PyGame still read a blue background even after I've cleared it with GIMP.
When the image file format supports it, pixels aren't just represented as RGB bits. There's additional information for every pixel, the alpha channel. What GIMP did is to set the alpha bits to 0% opacity. The RGB values remained (in this instance, blue). Since my PyGame code isn't set to read the additional alpha bits, it only read the RGB describing blue. The convert_alpha method told PyGame to read the alpha channel as well.
I have a selection of images that I will build a backdrop to a game from (it's a TD game but each level will be built from walls, walkways etc). I'm trying to work out how to dynamically build a single image to use as a background rather than having 100+ sprites for a background.
you will have to make pygame.surface with your images.
images = []
for image_name in image_names:
images.append(pygame.image.load(image_name))
background = pygame.display.get_surface()
for image in images:
back.blit(image, image_position) # how you compute the image position is your stuff ;)
pygame.display.update()
You can always use PIL
See this post for details about how to combine images. You can basically "paste" one image into another image. Make one big blank image for your background, and then paste all the sprites into the background image.