In this lesson, you’ll start drawing on the screen using a Surface
. Recall that a Surface
is a rectangular object on which you can draw, like a blank sheet of paper. The screen
object is a Surface
, and you can create your own Surface
objects separate from the display screen.
You’ll fill the screen with white, and add a new Surface
object to display on the screen. Add the code below your existing code, starting at line 44. Note the indentation, keeping the lines you are adding within the game loop, but outside the event loop:
44# Fill the screen with white
45screen.fill((255, 255, 255))
46
47# Create a surface and pass in a tuple containing its length and width
48surf = pygame.Surface((50, 50))
49
50# Give the surface a color to separate it from the background
51surf.fill((0, 0, 0))
52rect = surf.get_rect()
For more information about the Surface
and Rect
classes, check out the following resources from the pygame
documentation: