KEMBAR78
Game Development With Python and Pygame | PDF
GAME DEVELOPMENT
WITH PYTHON AND
PYGAMESEPT 2017
CHARIZA BACLOR PLADIN
Data Analyst – Accenture Inc.
chariza.b.pladin@accenture.com
AGENDA
• Basics of Game Design
• Exploring Python
• Introducing PyGame
• Understanding Game Loop
• Setting up a Skeleton
• Working with Sprites
• Play with Events
• Q/A
Game Development with Python and PyGame Sept 2017
Game Development with Python and PyGame Sept 2017
BASIC OF TO GAME DESIGN
Game Development with Python and PyGame Sept 2017
THE “FUN”DAMENTALS
In making a game, you become an
entertainer, not a puppet
master bent on world
domination.
Game Development with Python and PyGame Sept 2017
KNOW YOUR AUDIENCE
Sturgeon’s Law: 99% of
everything is crap.
Pay attention to what they like
and what they don’t like.
Game Development with Python and PyGame Sept 2017
KNOW YOUR GENRE
What players expect from your
game is the deciding factor in
whether it will be a success or
a failure.
Game Development with Python and PyGame Sept 2017
KNOW YOUR SELF
Ask yourself.What
makes a game fun?
Game Development with Python and PyGame Sept 2017
EMPOWERING PLAYERS
“Take me to a place I’ve never
been, make me something I
could never be, and let me do
things
I could never do.”
Game Development with Python and PyGame Sept 2017
Nature of Power
Ability or official capacity to exercise
control. By understanding the nature of
power, and which types of power appeal to
which types of players, you can begin to
fine-tune your game design technique.
Game Development with Python and PyGame Sept 2017
A Small Lesson on the Nature of
Power(cont.)
• Creative Power
• Destructive Power
• Manipulative Power
Game Development with Python and PyGame Sept 2017
CREATIVE POWER
Pros
• brings a sense of accomplishment that
is extremely rewarding.
• Games which is more of a ‘toy’ than a
competition.
Cons
• complex and time consuming, and can
turn off players who want instant
gratification.
Game Development with Python and PyGame Sept 2017
DESTRUCTIVE POWER
Pros
• most satisfying in an immediate
sense, and thus are the quickest to
empower.
Cons
• Game type of shallowest learning
curve.
Game Development with Python and PyGame Sept 2017
MANIPULATIVE POWER
• most subtle power, and its correct
use rewards the player by making
him feel clever and proud of that
cleverness.
Game Development with Python and PyGame Sept 2017
EXPLORING PYTHON
Game Development with Python and PyGame Sept 2017
Install Python
For Windows
http://www.python.org > Download link > Get the latest ver.
Game Development with Python and PyGame Sept 2017
Install Python
For Mac OS X
The installation for Mac OS X is similar. Instead of downloading
the .msi file from the Python website, download the .dmg Mac
Installer Disk Image file instead.The link to this file will look
something like "Mac Installer disk image (python_version)" on the
"Download Python Software" web page.
Ubuntu and Linux
you can install Python by opening a terminal window (click on
Applications > Accessories >Terminal) and entering
sudo apt-get install <python_ver> then pressing Enter.
Game Development with Python and PyGame Sept 2017
Install Python
Open IDLE (Interactive DeveLopment Environment. )
Game Development with Python and PyGame Sept 2017
INTRODUCING PYGAME
Game Development with Python and PyGame Sept 2017
Pygame
• A set of Python modules to help write games; “game
library”.
• Deals with media nicely (pictures, sound)
• Interacts with user nicely (keyboard, joystick, mouse
input)
• Lots of more advanced features for working with
graphics, etc.
Game Development with Python and PyGame Sept 2017
Pygame
Game Development with Python and PyGame Sept 2017
History of Pygame
Pygame is built on another game creation library called
Simple DirectMedia Layer (SDL). SDL was written
by Sam Lantinga while he was working for Loki Software (a
now-defunct game company) to simplify the task
of porting games from one platform to another. It provided
a common way to create a display on multiple
platforms as well as work with graphics and input devices.
Because it was so simple to work with, it became
very popular with game developers when it was released in
1998, and has since been used for many hobby
and commercial games.
Game Development with Python and PyGame Sept 2017
Installing Pygame
Pygame does not come with Python. Like
Python, Pygame is available for free.You will
have to download and install Pygame, which is
as easy as downloading and installing the
Python interpreter.
Game Development with Python and PyGame Sept 2017
Installing Pygame
pip install pygame
Windows
'pip' is not recognized as an internal or external command,
operable program or batch file
Error may encounter
C:/Python34/Scripts/pip install pygame
Use Full path
Game Development with Python and PyGame Sept 2017
Installing Pygame
For Mac OS X
Mac OS has historically had a lot of trouble with
Python GUI-related modules in all forms.This is an ongoing
campaign, so your best bet is to see the latest installation
instructions from
http://pygame.org/wiki/macintosh.
pip install C:/Users/H/Downloads/pygame-1.9.2a0-cp34-none-
win32.whl
Game Development with Python and PyGame Sept 2017
GUI vs. CLI
Command-Line Interface (CLI)
Your Python program can display text on the screen and let the user type
in text from the keyboard. These programs are somewhat limited
because they can’t display graphics, have colors, or use the mouse.
Graphical User Interface (GUI)
Pygame provides functions for creating programs with a GUI. Instead of
a text-based CLI, programs with a graphics-basedGUI can show a
window with images and colors.
Game Development with Python and PyGame Sept 2017
Pygame Packages
Just like how Python comes with several modules like
random, math, or time that provide additional
functions for your programs, the Pygame framework
includes several modules with functions for drawing
graphics, playing sounds, handling mouse
input, and other things.
Game Development with Python and PyGame Sept 2017
Pygame Packages
Game Development with Python and PyGame Sept 2017
Pygame Packages
*For complete documentation on the Pygame modules, see
http://pygame.org/docs/.
Game Development with Python and PyGame Sept 2017
Understanding Game Loop
PROCESS
INPUT
UPDATE
GAME
RENDER
FPS
Game Development with Python and PyGame Sept 2017
Setting up a Pygame Program
(Skeleton)
Game Development with Python and PyGame Sept 2017
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 features that Pygame provides are in the
PyGame module.
<Module name> import *, you can skip the <module name>. portion and simply
use function name().
Game Development with Python and PyGame Sept 2017
Line 4 is the command which always needs to be called after importing
the PyGame module and before calling any other Pygame function.
Line 5 is a call to the pygame.display.set_mode() function, which returns
the pygame. Surface object for the window.
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.
Game Development with Python and PyGame Sept 2017
Line 6 sets the caption text that will appear at the top of the window by
calling the pygame.display.set_caption() function.The string value 'Hello
World!' is passed in this function call to make that text appear as the
caption:
Line 7 is a while loop that has a condition of simply the valueTrue.The
only way the program execution will ever exit the loop is if a break
statement is executed.
Game Development with Python and PyGame Sept 2017
Line 12 calls the pygame.display.update() function, which draws the
Surface object returned by pygame.display.set_mode() to the screen.
Since the Surface object hasn’t changed, the same black image is
redrawn to the screen each time pygame.display.update() is called.
Game Development with Python and PyGame Sept 2017
SURFACE
Most of the game elements you see are represented as
Surfaces.
display.set_mode((x, y))
creates your canvas – it returns a Surface object.
Useful surface methods:
fill("color") fills the surface object it's called on.
blit(surface, area) paints surface onto the object blit is called on in
the rectangle bounded by the area tuple.
screen.blit(ball, (50,50))
Game Development with Python and PyGame Sept 2017
RECT
Objects that store rectangular coordinates.
.get_rect()
on a surface to get its bounding box.
Rectangle methods/variables:
.center
holds the object's center as a tuple
.colliderect(target)
returnsTrue if the parameter overlaps with the object
.collidepoint(target)
returnsTrue if the target point overlaps with the object.
Game Development with Python and PyGame Sept 2017
MEDIA
• Loading an image:
img = image.load("file.gif").convert()
• Getting a bounding rectangle:
img_rect = img.get_rect()
• Loading and playing a sound file:
mixer.Sound("file.wav").play()
Game Development with Python and PyGame Sept 2017
SPRITE
Class visible game objects inherit from.
Game Development with Python and PyGame Sept 2017
SPRITE
They're just objects: initialize them
ball = Ball()
Create a group of sprites in main
sprites = RenderPlain(sprite1, sprite2)
Groups know how to draw and update
sprites.update()
sprites.draw(surface)
Game Development with Python and PyGame Sept 2017
EVENTS
User input such as clicking, moving mouse or key presses.
Add more branches to test the result of event.poll().
SAMPLE EVENTTRIGGER:
– QUIT
– MOUSEBUTTONDOWN
– JOYBUTTONDOWN
•Testing for the letter ‘d’ being pressed using KEYDOWN
if e.type == KEYDOWN:
if e.key == K_d:
Game Development with Python and PyGame Sept 2017
TEXT
• f = font.Font(font, size) goes before your game loop
f = font.Font(None, 25)
• text = Font.render(text, antialias, color)
text = f.render("Hello!", True,Color("green"))
Returns a surface
• Must be blit, just like any other surface
screen.blit(t, (320, 0))
Game Development with Python and PyGame Sept 2017
Ask me about
Pygame
Game Development with Python and PyGame Sept 2017
Good Reads
Beginning Python Games Development With
PyGame
- Harrison Kinsley andWill McGugan
InventYour Own Computer Games with Python, 2nd
and 3rd Edition
- Al Sweigart
Making Gameswith Python& Pygame
- Al Sweigart
Pygame 1.5.5 Reference Manual
- Pygame Documentation
Game Development with Python and PyGame Sept 2017
PYGAME CHALLENGE
You can make your own game using pygame library.
Your game good to have:
• At least two sprites.
• Interaction between sprites.
• User input from keyboard or mouse.
• Some kind of score displayed.
THANK YOU
:)

Game Development With Python and Pygame

  • 1.
    GAME DEVELOPMENT WITH PYTHONAND PYGAMESEPT 2017 CHARIZA BACLOR PLADIN Data Analyst – Accenture Inc. chariza.b.pladin@accenture.com
  • 2.
    AGENDA • Basics ofGame Design • Exploring Python • Introducing PyGame • Understanding Game Loop • Setting up a Skeleton • Working with Sprites • Play with Events • Q/A Game Development with Python and PyGame Sept 2017
  • 3.
    Game Development withPython and PyGame Sept 2017 BASIC OF TO GAME DESIGN
  • 4.
    Game Development withPython and PyGame Sept 2017 THE “FUN”DAMENTALS In making a game, you become an entertainer, not a puppet master bent on world domination.
  • 5.
    Game Development withPython and PyGame Sept 2017 KNOW YOUR AUDIENCE Sturgeon’s Law: 99% of everything is crap. Pay attention to what they like and what they don’t like.
  • 6.
    Game Development withPython and PyGame Sept 2017 KNOW YOUR GENRE What players expect from your game is the deciding factor in whether it will be a success or a failure.
  • 7.
    Game Development withPython and PyGame Sept 2017 KNOW YOUR SELF Ask yourself.What makes a game fun?
  • 8.
    Game Development withPython and PyGame Sept 2017 EMPOWERING PLAYERS “Take me to a place I’ve never been, make me something I could never be, and let me do things I could never do.”
  • 9.
    Game Development withPython and PyGame Sept 2017 Nature of Power Ability or official capacity to exercise control. By understanding the nature of power, and which types of power appeal to which types of players, you can begin to fine-tune your game design technique.
  • 10.
    Game Development withPython and PyGame Sept 2017 A Small Lesson on the Nature of Power(cont.) • Creative Power • Destructive Power • Manipulative Power
  • 11.
    Game Development withPython and PyGame Sept 2017 CREATIVE POWER Pros • brings a sense of accomplishment that is extremely rewarding. • Games which is more of a ‘toy’ than a competition. Cons • complex and time consuming, and can turn off players who want instant gratification.
  • 12.
    Game Development withPython and PyGame Sept 2017 DESTRUCTIVE POWER Pros • most satisfying in an immediate sense, and thus are the quickest to empower. Cons • Game type of shallowest learning curve.
  • 13.
    Game Development withPython and PyGame Sept 2017 MANIPULATIVE POWER • most subtle power, and its correct use rewards the player by making him feel clever and proud of that cleverness.
  • 14.
    Game Development withPython and PyGame Sept 2017 EXPLORING PYTHON
  • 15.
    Game Development withPython and PyGame Sept 2017 Install Python For Windows http://www.python.org > Download link > Get the latest ver.
  • 16.
    Game Development withPython and PyGame Sept 2017 Install Python For Mac OS X The installation for Mac OS X is similar. Instead of downloading the .msi file from the Python website, download the .dmg Mac Installer Disk Image file instead.The link to this file will look something like "Mac Installer disk image (python_version)" on the "Download Python Software" web page. Ubuntu and Linux you can install Python by opening a terminal window (click on Applications > Accessories >Terminal) and entering sudo apt-get install <python_ver> then pressing Enter.
  • 17.
    Game Development withPython and PyGame Sept 2017 Install Python Open IDLE (Interactive DeveLopment Environment. )
  • 18.
    Game Development withPython and PyGame Sept 2017 INTRODUCING PYGAME
  • 19.
    Game Development withPython and PyGame Sept 2017 Pygame • A set of Python modules to help write games; “game library”. • Deals with media nicely (pictures, sound) • Interacts with user nicely (keyboard, joystick, mouse input) • Lots of more advanced features for working with graphics, etc.
  • 20.
    Game Development withPython and PyGame Sept 2017 Pygame
  • 21.
    Game Development withPython and PyGame Sept 2017 History of Pygame Pygame is built on another game creation library called Simple DirectMedia Layer (SDL). SDL was written by Sam Lantinga while he was working for Loki Software (a now-defunct game company) to simplify the task of porting games from one platform to another. It provided a common way to create a display on multiple platforms as well as work with graphics and input devices. Because it was so simple to work with, it became very popular with game developers when it was released in 1998, and has since been used for many hobby and commercial games.
  • 22.
    Game Development withPython and PyGame Sept 2017 Installing Pygame Pygame does not come with Python. Like Python, Pygame is available for free.You will have to download and install Pygame, which is as easy as downloading and installing the Python interpreter.
  • 23.
    Game Development withPython and PyGame Sept 2017 Installing Pygame pip install pygame Windows 'pip' is not recognized as an internal or external command, operable program or batch file Error may encounter C:/Python34/Scripts/pip install pygame Use Full path
  • 24.
    Game Development withPython and PyGame Sept 2017 Installing Pygame For Mac OS X Mac OS has historically had a lot of trouble with Python GUI-related modules in all forms.This is an ongoing campaign, so your best bet is to see the latest installation instructions from http://pygame.org/wiki/macintosh. pip install C:/Users/H/Downloads/pygame-1.9.2a0-cp34-none- win32.whl
  • 25.
    Game Development withPython and PyGame Sept 2017 GUI vs. CLI Command-Line Interface (CLI) Your Python program can display text on the screen and let the user type in text from the keyboard. These programs are somewhat limited because they can’t display graphics, have colors, or use the mouse. Graphical User Interface (GUI) Pygame provides functions for creating programs with a GUI. Instead of a text-based CLI, programs with a graphics-basedGUI can show a window with images and colors.
  • 26.
    Game Development withPython and PyGame Sept 2017 Pygame Packages Just like how Python comes with several modules like random, math, or time that provide additional functions for your programs, the Pygame framework includes several modules with functions for drawing graphics, playing sounds, handling mouse input, and other things.
  • 27.
    Game Development withPython and PyGame Sept 2017 Pygame Packages
  • 28.
    Game Development withPython and PyGame Sept 2017 Pygame Packages *For complete documentation on the Pygame modules, see http://pygame.org/docs/.
  • 29.
    Game Development withPython and PyGame Sept 2017 Understanding Game Loop PROCESS INPUT UPDATE GAME RENDER FPS
  • 30.
    Game Development withPython and PyGame Sept 2017 Setting up a Pygame Program (Skeleton)
  • 31.
    Game Development withPython and PyGame Sept 2017 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 features that Pygame provides are in the PyGame module. <Module name> import *, you can skip the <module name>. portion and simply use function name().
  • 32.
    Game Development withPython and PyGame Sept 2017 Line 4 is the command which always needs to be called after importing the PyGame module and before calling any other Pygame function. Line 5 is a call to the pygame.display.set_mode() function, which returns the pygame. Surface object for the window. 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.
  • 33.
    Game Development withPython and PyGame Sept 2017 Line 6 sets the caption text that will appear at the top of the window by calling the pygame.display.set_caption() function.The string value 'Hello World!' is passed in this function call to make that text appear as the caption: Line 7 is a while loop that has a condition of simply the valueTrue.The only way the program execution will ever exit the loop is if a break statement is executed.
  • 34.
    Game Development withPython and PyGame Sept 2017 Line 12 calls the pygame.display.update() function, which draws the Surface object returned by pygame.display.set_mode() to the screen. Since the Surface object hasn’t changed, the same black image is redrawn to the screen each time pygame.display.update() is called.
  • 35.
    Game Development withPython and PyGame Sept 2017 SURFACE Most of the game elements you see are represented as Surfaces. display.set_mode((x, y)) creates your canvas – it returns a Surface object. Useful surface methods: fill("color") fills the surface object it's called on. blit(surface, area) paints surface onto the object blit is called on in the rectangle bounded by the area tuple. screen.blit(ball, (50,50))
  • 36.
    Game Development withPython and PyGame Sept 2017 RECT Objects that store rectangular coordinates. .get_rect() on a surface to get its bounding box. Rectangle methods/variables: .center holds the object's center as a tuple .colliderect(target) returnsTrue if the parameter overlaps with the object .collidepoint(target) returnsTrue if the target point overlaps with the object.
  • 37.
    Game Development withPython and PyGame Sept 2017 MEDIA • Loading an image: img = image.load("file.gif").convert() • Getting a bounding rectangle: img_rect = img.get_rect() • Loading and playing a sound file: mixer.Sound("file.wav").play()
  • 38.
    Game Development withPython and PyGame Sept 2017 SPRITE Class visible game objects inherit from.
  • 39.
    Game Development withPython and PyGame Sept 2017 SPRITE They're just objects: initialize them ball = Ball() Create a group of sprites in main sprites = RenderPlain(sprite1, sprite2) Groups know how to draw and update sprites.update() sprites.draw(surface)
  • 40.
    Game Development withPython and PyGame Sept 2017 EVENTS User input such as clicking, moving mouse or key presses. Add more branches to test the result of event.poll(). SAMPLE EVENTTRIGGER: – QUIT – MOUSEBUTTONDOWN – JOYBUTTONDOWN •Testing for the letter ‘d’ being pressed using KEYDOWN if e.type == KEYDOWN: if e.key == K_d:
  • 41.
    Game Development withPython and PyGame Sept 2017 TEXT • f = font.Font(font, size) goes before your game loop f = font.Font(None, 25) • text = Font.render(text, antialias, color) text = f.render("Hello!", True,Color("green")) Returns a surface • Must be blit, just like any other surface screen.blit(t, (320, 0))
  • 42.
    Game Development withPython and PyGame Sept 2017 Ask me about Pygame
  • 43.
    Game Development withPython and PyGame Sept 2017 Good Reads Beginning Python Games Development With PyGame - Harrison Kinsley andWill McGugan InventYour Own Computer Games with Python, 2nd and 3rd Edition - Al Sweigart Making Gameswith Python& Pygame - Al Sweigart Pygame 1.5.5 Reference Manual - Pygame Documentation
  • 44.
    Game Development withPython and PyGame Sept 2017 PYGAME CHALLENGE You can make your own game using pygame library. Your game good to have: • At least two sprites. • Interaction between sprites. • User input from keyboard or mouse. • Some kind of score displayed.
  • 45.