0% found this document useful (0 votes)
20 views

Introduction To Pygame

PyGame animacion

Uploaded by

arbux.drones
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Introduction To Pygame

PyGame animacion

Uploaded by

arbux.drones
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

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:

Yay! We’ve just made


The world’s most
boring video game!
Hello World
• It’s just a blank window with “Hello
World!” at the top of the window (in
what is called the window’s title
bar, which holds the caption text).
• But creating a window is the first
step to making graphical games.
When you click on the X button in the
corner of the window, the program
will end and the window will
disappear.
Hello World – A
Closer Look

• The first few lines of code are lines


that will begin almost every program
you write that uses Pygame.
• Line 1 is a simple import statement
that imports the pygame and sys
modules so that our program can use
the functions in them.
• All of the Pygame functions dealing
with graphics, sound, and other
Hello World
• We can’t use the input() or
print() functions in a Pygame
GUI. We will learn more about
input and output in Pygame
later.
• Let’s take a closer look to the
“Hello World” program!
Hello World – A
Closer Look

• Line 2 is also an import statement. It uses


the form modulename import * format.
• Normally if you want to call a function that
is in a module, you must use the
modulename.functionname() format after
importing the module.
• However, with from modulename import *,
you can skip the modulename. portion and
simply use functionname() (just like
Python’s built-in functions).
Hello World – A
Closer Look

• Line 4 is the pygame.init() function call, which


always needs to be called after importing the
pygame module and before calling any other
Pygame function.
• You don’t need to know what this function does,
you just need to know that it needs to be called
first in order for many Pygame functions to work.
• If you ever see an error message like
pygame.error: font not initialized,
check to see if you forgot to call pygame.init() at
the start of your program.
Hello World – A
Closer Look
• Line 5 is a call to the
pygame.display.set_mode() function, which
returns the pygame.Surface object for the
window. More on surface objects later…
• Notice that we pass a tuple value of two
integers to the function: (400, 300). This
tuple tells the set_mode() function how
wide and how high to make the window in
pixels. (400, 300) will make a window with a
width of 400 pixels and height of 300 pixels.
• NOTE: An error will occur if it is not a tuple.
Hello World – A
Closer Look
Hello World – A
Closer Look - Game
States

Most of the games we will be looking at


have these while True loops in them
along with a comment calling it the
“main game loop”. A game loop (also
called a main loop) is a loop where the
code does three things:
1. Handles events.
2. Updates the game state.
The Game State
• The game state is simply a way of referring
to a set of values for all the variables in a
game program. In many games, the game
state includes the values in the variables
that tracks the player’s health and position,
the health and position of any enemies,
which marks have been made on a board,
the score, or whose turn it is.
• Whenever something happens like the
player taking damage (which lowers their
health value), or an enemy moves
somewhere, or something happens in the
game world we say that the game state has
changed.
The Game State
• If you’ve ever played a
game that lets you save,
the “save state” is the
game state at the point
that you’ve saved it. In
most games, pausing the
game will prevent the
game state from changing.
The Game State
• Since the game state is usually updated in
response to events (such as mouse clicks or
keyboard presses) or the passage of time,
the game loop is constantly checking and
re-checking many times a second for any
new events that have happened. This is
usually called event handling.
Hello World – The
QUIT Event

• Event objects have a member variable (also


called attributes or properties – remember
ALICE?) named type which tells us what kind
of event the object represents.
• Line 9 checks if the Event object’s type is
equal to the constant QUIT. Remember that
since we used the from pygame.locals import
* form of the import statement, we only have
to type QUIT instead of pygame.locals.QUIT.
Hello World – A
Closer Look

• Line 12 calls the pygame.display.update()


function, which draws the Surface object
returned by pygame.display.set_mode() to
the screen (remember we stored this object
in the DISPLAYSURF variable).
• Since the Surface object hasn’t changed,
the same black image is redrawn to the
screen each time pygame.display.update()
is called.
Hello World – That is
IT!
• That is the entire program!
• After line 12 is done, the infinite
while loop starts again from the
beginning.
• This program does nothing besides
make a black window appear on
the screen, constantly check for a
QUIT event, and then redraws the
unchanged black window to the
screen over and over again.
What is Next?
Let’s learn how to make
interesting things appear on
this window instead of just
blackness by learning about :

• Pixels
• Surface objects
• Color objects
• Rect objects
• Pygame drawing functions.

You might also like