Kodeclik Blog

Everything you wanted to know about pygame

What is pygame?

Pygame is a Python library for designing games. Pygame is built on top of SDL, Simple DirectMedia Library, which is a library for interfacing with and managing input devices (like keyboards, mouse, joystick) and output devices (like screens, voice, and other graphics hardware). SDL (and thus pygame) is cross-platform, meaning once you write the code using functions from these libraries, it should work across a range of modalities.

Is it possible to build games without pygame?

It is possible to build games without pygame but it will be cumbersome. Pygame takes care of a lot of low-level details so you can concentrate on the game logic. For instance, you don’t have to worry about positioning objects, moving them across the screen, or determining how to receive inputs or commands from the users. These capabilities are provided using pygame functions so that you can focus on the game logic such as the rules of the game, how the user scores points, how the game is won or lost. If you write your game program in pygame, the game can work over a desktop, or an iPhone, or some other gaming console system.

Is pygame free?

Yes, just like many implementations of Python, pygame is completely free. You have to install it using one of the Python package installation commands.

Does pygame support textual games or graphical games?

Pygame supports both textual games and graphical games. Textual games can be, for instance, textual adventure games. Graphical games are those that are played on a board such as Connect 4, Tic Tac Toe, and Game of Life. pygame also supports the development of platformer games where the landscape continues indefinitely and the goal is to navigate the player through obstacles and other objects.pygame also supports games with significant animation and graphical elements for better realism.

What types of games can I build with pygame?

There are several classes of games that can be implemented in pygame. First, all board games can be implemented such as Chess, Checkers, Sudoku, Connect 4, Minesweeper, Solitaire, and more. Second, all types of graphical games that involve mouse or cursor movement can be implemented such as Snake, Flappy Bird, Car Racing, and so on. In fact, many of these games are themselves open sourced and available in addition to the core pygame library. Third, platformer games which involve animation of objects can be built using pygame. There are many packs of characters that can be used in conjunction with pygame to create a very realistic experience. Finally, more serious video game capabilities can be technically built with pygame but this is where you will start seeing the limits of pygame.

Can I implement games of chance with pygame?

Yes, because pygame is built on top of Python, you can import modules like random to create elements of uncertainty and chance into your game logic.

Who invented pygame?

Pygame originally began as an extension to SDL and was developed by Pete Shinners. Over the years many additions have been made to pygame by community members. It is a true community-led project today and you can follow all activities under their github account: https://github.com/pygame/pygame. The github account gives details of features under development, code improvements, bug fixes, and important versions released.

What are the main concepts behind pygame?

The key concept behind pygame is called the “game loop”. Just like repl (the read-eval-print-loop), the game loop is a never-ending loop that looks for events and responds to them. The game logic is encoded in the response code. For specific conditions and situations, the game logic can increase scores, decrease lives, or even exit the game (e.g., when the player exits or when there are no more lives left). All this information is typically encoded in the form of a “game state” which can be used to update the board or screen reflecting the current condition of the game. Thus the game loop simply cycles through: i) await events, ii) update game state, and iii) draw updated screen.

Is pygame slow?

Pygame is slow when compared to hardware accelerated games like those that run on GPUs (graphics processing units). For 2D games, pygame is sufficient. For 3D games with complex, real-time, rendering requirements, pygame might be slow. For the types of games discussed earlier and using modern PC/laptop hardware, pygame will be fast enough. Sometimes the slowness will come from game playing in large state spaces (e.g., chess) and not necessarily because of pygame itself.

What are famous games made with pygame?

As mentioned earlier, there are famous games like Snake, Battleships, Fets on Fire that have been implemented using pygame.

Is pygame easy to learn?

Pygame is easy to learn if you already know Python and are familiar with object oriented syntax. Explore Kodeclik’s Python programming courses that will set you on the track for game development with pygame.

How can I learn pygame?

Begin by gaining a solid foundation in Python using Kodeclik’s courses. Then learn about the pygame library and its functions using the several open source games already released. There are two classical books, viz. both by Al Sweigart that would be useful. While the former only covers pygame in some sections, the latter is entirely about pygame.
Want to learn Python with us? Sign up for 1:1 or small group classes.