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.

Getting to Know Pygame

For more information on concepts covered in this course, please check out:

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’m going to show you how to get started with Pygame, drawing simple shapes to the screen and animating them.

00:11 Pygame is a third-party library and as such, needs to be installed. It is recommended that whenever you install libraries, you use a virtual environment.

00:19 There are links in the notes below that can show you how to do that if you haven’t done it before. With your virtual environment activated, run the pip install command to download and install Pygame.

00:34 Video games are structured around a central game loop. Inside of each iteration of the game loop, you handle any input from the user or outside events such as the network, determine where your moveable pieces are in the game, and draw all the things visible to the user onto the screen, rendering a single frame of the game.

00:55 Like movies, games are made up of a series of frames shown to the screen in rapid succession. To get the appearance of fluid motion, you want at least 25 frames per second, also known as FPS.

01:10 Modern gaming hardware often goes way past this minimum, with high fidelity games routinely using 60 frames per second.

01:18 In order to achieve the goal of 25 FPS, you’ll first have to draw a frame. A frame is just an image shown to the screen, albeit for a very short time. Graphic systems use a Cartesian coordinate system to describe where things are in the frame.

01:34 This means the frame is described with an x- and y-axis, with each pixel being described by an (x, y) tuple called a point.

01:45 Different graphics libraries treat the coordinate system differently. Instead of using an origin in the center of the screen like your algebra class, Pygame uses the top left-hand corner as (0, 0).

01:56 In order to draw this circle halfway across and a quarter of the way down, you describe it as 200 on the x-axis and 75 on the y-axis. The r parameter to the circle() function specifies the radius.

02:12 An r of 15 will give you a circle with a diameter of 30 pixels. For this sample screen size of 400 wide and 300 tall, (200, 75) gives you the circle you see here.

02:26 Not all graphics systems use the top-left corner as the origin. This means when loading images in, you may have to translate coordinate systems. Another peculiar side effect of this origin is that moving up on the screen is a negative direction. You’ll see more on this later.

02:45 Now that you’re oriented and you’ve seen a circle, let’s go write some code.

02:51 Here’s the code for your first “game.” Did you hear the air quotes around that word? To get started, you’ll need to import the pygame library and initialize it.

03:00 The init() call on line 5 gets Pygame set up. On line 6, I’m setting the screen’s title, and on line 7, I tell Pygame how big the screen will be. In this case, I’ve chosen 800 pixels wide and 600 pixels tall. Note that the set_mode() method takes a tuple specifying the dimensions, rather than two parameters.

03:25 The set_mode() method returns a surface. Surfaces are where you can draw things. In this case, the surface is the main screen of the program.

03:34 I’m storing this away in a variable called screen so that I can draw to this surface later.

03:41 I mentioned that games have a game loop, and here is yours. This while loop loops forever, or until someone trips the quit condition. Inside this loop, you normally handle any user interaction, the calculation of the positions of objects, and the rendering of a frame.

03:59 Once all that is done, you start the next loop.

04:03 In this first program, the only user interaction is handling the QUIT event. This inner loop on line 10 goes through all of the events that have happened since the last iteration of the game loop. For this simple program, the only event you are interested in is if the close button on the app has been pressed.

04:23 Line 14 fills in the background of the screen by calling the .fill() method on the screen surface, .fill() takes an RGB tuple that specifies the color to fill with.

04:34 In this case, it is a shade of blue. Were you expecting something different in a program named blue?

04:42 Lines 15 through 17 are about positioning the objects to be drawn. In this case, the only thing being positioned is a single circle. I’m randomly choosing an x, y, and radius value for the circle. Note the choice of the range of values for x and y.

04:59 The radius of the circle is always between 2 and 10, so the ranges of x and y are chosen so that the circle always appears on the screen. With appropriate random values selected, line 18 draws the actual circle.

05:15 The pygame.draw module contains many functions for drawing lines and shapes. The circle() function takes a few parameters. The first is a surface to draw on. This is the background screen.

05:28 The second, a color to draw with, once again an RGB value for a different shade of—well, what else?—blue. The third is an (x, y) tuple representing the point where to draw the circle.

05:40 And finally, the radius of the circle that needs to be drawn.

05:45 The last line here calls display.flip(). The drawing that has been happening so far hasn’t actually been drawn to the screen, but to a buffer.

05:54 You want to do things this way so that everything is shown on the screen at the same time. If you’re drawing lots of objects, you don’t want them to blink in and out as each is created.

06:04 This flip() method takes everything in the surface buffer and updates the screen with a copy of the buffer all at once. Let’s see all of this in action.

06:20 This seizure-inducing display is your first Pygame “game.” A nice, pretty shade of sky blue is filled into the screen, than a navy blue circle is drawn randomly.

06:32 The game loop continues, doing this over and over. The quick speckling you see here gives you an idea of how many frames your program is spitting out per second.

06:42 Later on, I’ll show you how to control that frame rate, but for now, you can play around. Change the circles, dig into Pygame docs, and get fancy. See if you can add a rectangle. Call of Duty it is not, but everyone has to start somewhere. Next up, you’ll write Call of Duty. No, wait. No, you won’t.

07:03 You’ll see how to get ready for larger programs and how to organize your code.

Become a Member to join the conversation.