Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A bit confused with blitting (Pygame)

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.

like image 971
Pabce Avatar asked Jul 03 '13 17:07

Pabce


People also ask

How do you stop Blitting in Pygame?

You can't "clear" blits, you can only blit something else in place. You can every loop clear the whole screen, using screen. fill(), with no rect argument. Then draw blit your new scene.

What is Blitting in Pygame?

blit() is how you copy the contents of one Surface to another. You can only . blit() from one Surface to another, but since the screen is just another Surface , that's not a problem. Here's how you draw surf on the screen: 54# This line says "Draw surf onto the screen at the center" 55screen.

How do Pygame coordinates work?

In pygame we always pass positions as an (X,Y) coordinate. This represents the number of pixels to the right, and the number of pixels down to place the image. The top-left corner of a Surface is coordinate (0, 0). Moving to the right a little would be (10, 0), and then moving down just as much would be (10, 10).


2 Answers

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.

like image 139
Mark Hildreth Avatar answered Sep 23 '22 09:09

Mark Hildreth


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!

like image 20
Serial Avatar answered Sep 23 '22 09:09

Serial