Loading video player…

PyGame Concepts

In this lesson, you’ll learn about some of the core pygame concepts. As pygame and the SDL library are portable across different platforms and devices, they both need to define and work with abstractions for various hardware realities. Understanding those concepts and abstractions will help you design and develop your own games.

Below are links to the documentation for the parts of the pygame library mentioned in the lesson, starting with several of the pygame modules:

These are links to the other concepts, classes, and methods mentioned:

00:01 In this lesson, I’ll take you through some of the core PyGame concepts. PyGame and the SDL library are portable across different platforms and devices. They both need to define and work with abstractions for various hardware realities.

00:15 Understanding these concepts and abstractions will help you to design and develop your own games. The PyGame library itself is composed of a number of Python constructs, which includes several different modules.

00:27 These modules are going to provide abstract access to specific hardware on your system, as well as a uniform set of methods to work with that hardware. For example, display allows uniform access to your display, while joystick would allow abstract controls of a joystick that’s connected to the computer. There’s also ones for music, which you’ll get the chance to play with, responding to key input from your keyboard.

00:53 You’ve already seen the event module. You’ll also get to work with the image module, which allows you to load and display images or sprites.

01:02 You’ve already walked through importing the PyGame library with import pygame. Once you’ve done that, the next step is always to initialize PyGame with pygame.init().

01:13 It initializes all the included PyGame modules, so it’s ready to work with the specific hardware in your machine. This initialization step is required so that you can work with the same code on Linux, Windows, and Mac. In addition to the modules, PyGame also includes several Python classes, which encapsulate non-hardware dependent concepts.

01:34 One of these is the Surface, which at the most basic level defines a rectangular area that you can draw on. Surface objects are used in many contexts in PyGame.

01:45 As you work through this tutorial, eventually you’ll be loading images, such as PNGs or JPGs, into a Surface, and then displaying them on the screen. Inside of PyGame, everything is viewed on a single user-created display, which can be either a window or the full screen.

02:03 The display is created by using .set_mode(), which returns a Surface representing the visible part of the window. All the contents of a Surface are pushed to the display when you call that pygame.display.flip().

02:20 Your basic PyGame program drew a shape directly under the display’s Surface, but you can also work with images from the disk. The image module allows you to do that loading and saving of images.

02:31 Images will be loaded into a Surface object so that they can then be manipulated.

02:37 The other tool you’ll use a lot is the Rect class. It’s a special class for storing rectangular coordinates.

02:44 Rectangles are so heavily used that there’s actually this special class just to handle them. These objects are not only used for managing and moving onscreen objects, but they’re also used to manage collisions.

02:56 I think that’s enough terminology and theory for now. It’s time to design and write a game—what you’ll dive into in the next section.

Become a Member to join the conversation.