In this lesson, you’ll use .blit()
and .flip()
to get your newly created Surface
to display on the screen. The term blit
stands for Block Transfer, and .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.blit(surf, (SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
56pygame.display.flip()
The reason why the image looks off-center is that .blit()
puts the top-left corner of surf
at the location given. If you want surf
to be centered, you would update the code like below:
54# Put the center of surf at the center of the display
55surf_center = (
56 (SCREEN_WIDTH-surf.get_width())/2,
57 (SCREEN_HEIGHT-surf.get_height())/2
58)
59
60# Draw surf at the new coordinates
61screen.blit(surf, surf_center)
62pygame.display.flip()
For more information about the .blit()
and .flip()
, check out the following resources from the pygame
documentation:
John Berliner on March 23, 2020
Just curious why you introduced get_rect() here. I assume you’ll be using it later, e.g. to determine the coordinates of sprite on the screen, but unless I’m missing something, it’s not being used yet? Great tutorial BTW.