Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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.

00:39 Let’s take a look! To start off with, you need to create a file, and so inside here—in pygame/—creating a file, hello_game.py. Okay.

00:52 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.

01:08 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 500that’s going to be for the width, and 500—that’s going to be for the height.

01:29 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 True.

01:44 You’re going to use that in a while loop. So, while running:—in this case, while running is Truethen 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?

02:00 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.

02:14 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 .type—and from pygame, you’re going to use this type named QUIT.

02:28 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

02:42 and fill it with white. To do that, you use the screen that you created and use a method called .fill(). Now, .fill() takes a color—in this case, you’re going to use a tuple.

02:55 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?

03:10 You use 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 screen.

03:17 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—(250, 250).

03:33 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 .flip().

03:48 Once that’s called, everything will display. Now outside the loop, once that running gets flipped to False, the while loop will quit and it will be done. Time to quit, so pygame has a method called quit(). Okay.

04:02 Zoom out a little bit. Let’s see the whole program here, all 29 lines.

04:10 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,

04:35 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.