4.2.1 - What is Pygame Library

To make graphics easier to work with, we'll use “Pygame.” Pygame is a library of code that other people have written, and makes it simple to: 

  • Draw graphic shapes
  • Display bitmapped images
  • Animate
  • Interact with keyboard, mouse, and gamepad
  • Play sound
  • Detect when objects collide

The first code a Pygame program needs to do is load and initialize the Pygame library.  Every program that uses Pygame should start with these lines:

# Import a library of functions called 'pygame'

>>> import pygame

Pygame library should be installed to your Thonny python environment. If you get an error running the previous command then pygame is not installed. Read the installation instructions and install it.

Important: The import pygame command looks for a library file named pygame. If a programmer creates a new program named pygame.py, the computer will import that file instead! This will prevent any pygame programs from working until that pygame.py file is deleted. Never name a program pygame.

After import we have to initialize the pygame with the next command.

# Initialize the game engine

>>> pygame.init()