Drawing on the Screen
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 45 screen.fill((255, 255, 255)) 46 47 # Create a surface and pass in a tuple containing its length and width 48 surf = pygame.Surface((50, 50)) 49 50 # Give the surface a color to separate it from the background 51 surf.fill((0, 0, 0)) 52 rect = surf.get_rect()
For more information about the
Rect classes, check out the following resources from the
In this lesson, you’re going to start drawing on the screen. There’s two commands you’ve used so far to accomplish drawing on the screen, and they were
screen.fill(), which filled in the background, and then you used
pygame.draw.circle() to draw a circle.
Let me take you into the code. So, your next step is to fill in the screen with white. And the
screen—here on line 25, you created the
screen with the
pygame.display.set_mode(), and then you used those two variables. So, down here, on line 44, within the game loop but you’re going to back out of the event loop—so you’re still in the main loop, just not in the event queue—you’re going to fill in the screen with white. To do that,
screen is that object that you’ve created—it’ll have a method called
It’s going to take a tuple of the color. You’ve already done this before, but
(255, 255, 255) is white, all white. Great. On line 47, you’re going to create a
Surface and pass in a tuple that contains the length and width. Again, it’s a rectangle.
To do that, you use the object
surf and then use
.fill() again—again, it takes a tuple. Let’s use black, which will be
(0, 0, 0) in RGB. And then you’re going to use this other method of the
Surface, which is to get a rectangle of it, and you’re going to simply call it
Become a Member to join the conversation.