Basic 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,
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
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
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.