PyGame
PyGame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Agenda
• Introduction to Pygame
• Installing pygame
• A "hello world" program in Pygame
• Game loop and Game state
• Knowledge about display window: Pixel coordinates
• Using Colors in Pygame
• Rect Object
• Adding an image to Game window
• Pygame Blit
• Drawing geometrical shapes
• Adding text to Game window
• Animation
• Playing sounds Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Pygame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to pygame
Python is a best choice to start with Game development
• Easy to learn
• Simple syntax to code with
Example
• Pirates of the Caribbean
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Introduction to Pygame
History
• Officially written by Pete Shinners, Pygame is a wrapper package
for SDL
• The seeds of Pygame and SDL were shown together in 2000
• The SDL- Simple DirectMedia Layer was created by Sam Lantinga.
• Pygame, Pyglet, and PyOgre are some other library/framework
to help create games in Python.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Installing Pygame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Installing Pygame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
A “hello world” program in pygame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
A “hello World” program in pygame
import pygame, sys
from pygame.locals import *
pygame.init()
display_window = pygame.display.set_mode((800, 500))
pygame.display.set_caption('Hello World!')
while True:
for each_event in pygame.event.get():
if each_event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Game loop and Game State
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Game loop and Game state
A game loop refers to main loop of pygame program code
It does 3 things:-
1. Handles all events raised
2. Update the state of game
3. Update the screen as per game state
while True:
for each_event in pygame.event.get():
if each_event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Knowledge about display window:
Pixel coordinates
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Knowledge about display window:
Pixel coordinates
Pixels
Represented as a tuple of two values
Uses Cartesian coordinate system
Starts at left top corner of screen/display window
X
0 1 2 3
y
0
1
(1, 1) represents a pixel
2
3
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Using colors in pygame
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Using colors in pygame
• Pygame uses RGB color values:- The three primary colors of light
• The fourth integer value added is called alpha value and it’s
range is 0 to 255
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Using colors in pygame
{ completely transparent} {completely opaque}
(0, 255, 0, 0) (0,255, 0, 255)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pygame Blit
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Pygame Blit
A process to display the image surface object on the display surface
object is called Blit.
Image formats that can be used are PNG, JPG, GIF and BMP
blit(source_image, location_onscreen)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Adding an image to game window
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Adding an image to Game Window
import pygame
pygame.init()
white = (255, 255, 255)
display_surface = pygame.display.set_mode((800, 500))
pygame.display.set_caption('Display image')
image_name = pygame.image.load(r'C:\image1.png')
while True:
display_surface.fill(white)
display_surface.blit(image_name, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Rect Object
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Rect Object
Two ways are there to represent rectangular areas:-
2. As a pygame.Rect object
Example:-
var1 = pygame.Rect(30,40,200,300)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Rect Object
import pygame
pygame.init()
display_surface = pygame.display.set_mode((800, 500))
done = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.draw.rect(display_surface, (255, 0, 0),
pygame.Rect(40, 30, 100, 100))
pygame.display.flip() Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Drawing geometrical shapes
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Drawing geometrical shapes
Different drawing functions are used to draw the primitive geometrical
shapes on a surface object.
The shapes that can be drawn are eclipse, circle, polygon, rectangle, lines etc
Here,
display_surface = represents the surface object to draw polygon on
Color = represents the color for the polygon drawn
Pointlist = it is a tuple or list of points
Width = represents the thickness of polygon being drawn
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Drawing geometrical shapes
To draw a line:-
pygame.draw.line(display_surface, color, start_point, end_point, width)
To draw a circle:-
pygame.draw.circle(display_surface, color, center_point, radius, width)
To draw a ellipse:-
pygame.draw.ellipse(display_surface, color, bounding_rectangle, width)
Bounding rectangle
To draw a rectangle:-
pygame.draw.rect(display_surface, color, rectangle_tuple, width)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Adding text to Game Window
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Adding text to Game Window
Text can also be drawn on the display window using font( ) method.
Using which new font objects are created. There are lot of methods to
support display of text on window/screen.
A rectangle object is needed to position and copy the font surface object
which is created by using get_rect( ) method
textSurfaceobject_variable.get_rect()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Animation
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Animation
Animated images are nothing but drawing a particular image on the
screen and then seconds later drawing a different angle of the same
image again on the screen
Frame rate:-
No of frames/images displayed per second is called frame rate. T is
measured in FPS(Frame per second)
FPS = 40
Actual_fps = pygame.time.Clock()
Actual_fps.tick(FPS)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Playing sounds
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Playing sounds
For Playing sounds that are stored in sound files, create a sound object by
calling the pygame.mixer.Sound() constructor function.
sound_object_variable = pygame.mixer.Sound('file_name.wav')
To immediately stop the Sound object from playing, use stop() method.
sound_object_variable.stop()
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
Summary
We discussed what is pygame all about and also various basic concepts
related to pygame that could help in developing a interactive game.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited