Basic PyGame Program
00:00 In this lesson, you’ll create your first PyGame program.
00:04 So, welcome to our first steps. It’s the PyGame equivalent to “Hello, World.” So, what are you going to do? This simple game is going to start by importing and initializing the PyGame library, setting up your display window, it’s going to create a game loop. Inside of that, it’ll scan and then handle events. You’re going to fill the window with a color,
00:27 and then you’re going to draw a circle. After doing all that, you need to have it update the display window to see all of it. And lastly, the final step will be a way to exit PyGame.
Let’s take a look! To start off with, you need to create a file, and so inside here—in
pygame/—creating a file,
Here is your simple
pygame program. Again, step one was to import and initialize the
pygame library—that starts with
import pygame. And then to initialize it, it’s
pygame.init()—you call that method.
The next step is to set up where everything is going to display. You’re going to set up the drawing window. For that, you’re going to create an object named
screen, and into it, you’re going to create a display and then set the display’s mode using the method, and then putting in a tuple of
500—that’s going to be for the width, and
500—that’s going to be for the height.
The next step is to create the game loop. And the game loop, you want to continue running until the user asks you to quit. So to implement that, you’re going to create a variable, which is going to be a Boolean variable, and you’re going to set it as a default to
You’re going to use that in a
while loop. So,
while running:—in this case,
while running is
True—then everything that’s inside of this loop is going to keep repeating and looping around until that gets flipped to
False. What are you going to check for?
Well, for this game, you’re going to confirm
# Did the user click the close button? Closing the window, therefore quitting
pygame. So, in this next step is to look inside what’s called the
pygame event queue.
And you’re going to grab from that event queue using
.get(), and for every one of those events that comes out, you can do a check. If
event is of
pygame, you’re going to use this type named
So, if the
event.type that’s pulled out of the queue while it’s looking through those is of
QUIT, then you’re going to flip running to
False. That will kick it out of that loop.
02:38 Meanwhile, if it’s still running, you’re going to create a background
and fill it with white. To do that, you use the
screen that you created and use a method called
.fill() takes a color—in this case, you’re going to use a tuple.
In RGB values,
(255, 255, 255) is equal to solid white. Then, into that solid white background, you’re going to put a solid blue circle—right in the center. How do you do that?
pygame and then from
draw, you’re going to use the method
.circle(). Where are you going to draw it? That’s the first argument, which is the
The next thing it’s looking for is a tuple for the color. Again, in RGB values, so
(0, 0, 255) would be solid blue. Next up, is “Where?”—or where its center will be. In this case, the center of the width and height that you set up before—
Those are again in pixels. And then
75 is going to be the radius of the circle. There’s your circle. Now, you need to make that stuff appear. To do that, you flip the display. From
display you use a method called
Once that’s called, everything will display. Now outside the loop, once that
running gets flipped to
while loop will quit and it will be done. Time to quit, so
pygame has a method called
04:02 Zoom out a little bit. Let’s see the whole program here, all 29 lines.
Okay. Save. From your prompt, go ahead and run—in my case, I’m running it from my virtual environment—
python hello_game.py. And here it is. Here’s the window, here’s the circle in the center of it—solid blue, white background. And again, it’s waiting for this and it will keep displaying this until I press this,
which kicked it out of
pygame and quit. Awesome! In the next lesson, I’ll take you through PyGame’s vocabulary and its high-level concepts that you’re going to use throughout the rest of this course.
Become a Member to join the conversation.