I am a beginner in Python and I am not clear about the function surface.blit()
. What does it do? How does it works?
I have come across the following points as to how to create it.
Syntax: canvas.blit(surface, surfacerect)
Why is only rect used? Can it be any other shape?
blit() — blit stands for Block Transfer—and it's going to copy the contents of one Surface onto another Surface . 00:17 The two surfaces in question are the screen that you created and the new Surface . So, . blit() will take that rectangular Surface and put it on top of the screen.
A pygame Surface is used to represent any image. The Surface has a fixed resolution and pixel format. Surfaces with 8-bit pixels use a color palette to map to 24-bit color. Call pygame. Surface() pygame object for representing images to create a new image object.
Pygame does not provide a direct way to write text onto a Surface object. The method render() must be used to create a Surface object from the text, which then can be blit to the screen. The method render() can only render single lines. A newline character is not rendered.
Create a Image surface object i.e. surface object in which image is drawn on it, using image. load() method of pygame. Copy the image surface object to the display surface object using blit() method of pygame display surface object. Show the display surface object on the pygame window using display.
Putting this in real terms may help, although as simply put as possible -> blitting is drawing
Going through each of the steps you have mentioned:
This is our window, created by screen = pygame.display.set_mode((width,height))
. Where screen
is the canvas name. Eventually everything will need to be drawn onto this canvas so that we can see it.
This is a surface that we will populate with objects such as images. It does not need to be smaller than the window size and it can be moved around freely.
When you create a surface using something like background = pygame.Surface((width,height))
you specify it's size. The images or drawn items on the surface can be any shape or size but they must all be contained within the bounds set by this width and height.
Now the all important bit. We need to get this surface (background) and draw it onto the window. To do this we will call screen.blit(background,(x,y))
where (x,y) is the position inside the window where we want the top left of the surface to be. This function says take the background surface and draw it onto the screen and position it at (x,y).
A simple example:
import pygame
pygame.init()
#### Create a canvas on which to display everything ####
window = (400,400)
screen = pygame.display.set_mode(window)
#### Create a canvas on which to display everything ####
#### Create a surface with the same size as the window ####
background = pygame.Surface(window)
#### Create a surface with the same size as the window ####
#### Populate the surface with objects to be displayed ####
pygame.draw.rect(background,(0,255,255),(20,20,40,40))
pygame.draw.rect(background,(255,0,255),(120,120,50,50))
#### Populate the surface with objects to be displayed ####
#### Blit the surface onto the canvas ####
screen.blit(background,(0,0))
#### Blit the surface onto the canvas ####
#### Update the the display and wait ####
pygame.display.flip()
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#### Update the the display and wait ####
pygame.quit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With