For more information on concepts covered in this course, please check out:
Getting to Know Pygame
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.
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.
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
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: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.
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.
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.
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.
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
y, and radius value for the circle. Note the choice of the range of values for
The radius of the circle is always between 2 and 10, so the ranges of
y are chosen so that the circle always appears on the screen. With appropriate random values selected, line 18 draws the actual circle.
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.
Become a Member to join the conversation.