Introduction To Pygame
Introduction To Pygame
Pygame Basics
• Just like how Python comes with several
modules that provide additional functions
for your programs (like math and
random), the Pygame framework includes
several modules with functions for:
o drawing graphics
o playing sounds
o handling mouse input
o And more!
Some Pygames
GUI vs. CLI
• The Python programs that you can
write with Python’s built-in functions
only deal with text through the
print() and input() functions.
• Your program can display text on the
screen and let the user type in text
from the keyboard. This type of
program has a command line
interface, or CLI (which is
pronounced like the first part of
“climb” and rhymes with “sky”).
GUI vs. CLI
• These programs are somewhat limited
because they can’t display graphics, have
colors, or use the mouse.
• These CLI programs only get input from
the keyboard with the input() function and
even then user must press Enter before
the program can respond to the input.
• This means real-time (that is, continuing
to run code without waiting for the user)
action games are impossible to make.
GUI vs. CLI
• Pygame provides functions for
creating programs with a
graphical user interface, or GUI
(pronounced, “gooey”).
• Programs with a graphics-based
GUIs can show a window with
images and colors.
Hello World
Our first program using
Pygame will be a small
program that makes a
window that says
“Hello World!”
appear on the screen.
Hello World
1. Create a script called blankpygame.py
2. Type in the following program (do not
include numbers or the periods at the
beginning of the line – they are just for
reference.)
Hello World
When you run this program, a black window like this
will appear:
• Pixels
• Surface objects
• Color objects
• Rect objects
• Pygame drawing functions.