0% found this document useful (0 votes)
2 views34 pages

PyGame

The document provides an overview of Pygame, a Python library for game development, covering installation, basic programming concepts, and essential features such as game loops, display windows, colors, images, shapes, text, animation, and sound. It includes code snippets to illustrate these concepts and emphasizes the importance of animation and sound in creating interactive games. The content is proprietary and intended for educational purposes.

Uploaded by

suhaspr2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views34 pages

PyGame

The document provides an overview of Pygame, a Python library for game development, covering installation, basic programming concepts, and essential features such as game loops, display windows, colors, images, shapes, text, animation, and sound. It includes code snippets to illustrate these concepts and emphasizes the importance of animation and sound in creating interactive games. The content is proprietary and intended for educational purposes.

Uploaded by

suhaspr2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

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

“Pygame is a wrapper package for SDL library which


contains vast library support that helps in creating 2D
games”

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.

Prerequisites for learning pygame

• A basic knowledge of Python programming

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

• Installing using pip

py -m pip install -U pygame --user or pip install pygame

• Installing using an IDE

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 color representation is done by a tuple of three values (R, G,


B)

• The values should be in range 0 to 255

• A fourth value can also be added to give transparency to the


color

• 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)

(R, G, B, Alpha) (R, G, B, Alpha)

Representation of color in pygame can be done using


pygame.color object

This color object can be created using a pygame.color()


constructor function and passing 3 or 4 values of color as
parameter

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.

Just creating an object will not render it on display surface, we need


to draw the content of one surface to another

Image formats that can be used are PNG, JPG, GIF and BMP

The basic method used is blit() which is overloaded in many forms.

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:-

1. As a tuple of four integers


X coordinate of the top left corner
Y coordinate of the top left corner
Width of rectangle
Height of rectangle

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

The function used for the same is draw( ).

pygame.draw.polygon(display_surface, color, pointlist, width)

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.

Sys.font( ) method can also be used in place of Font( ) method.


pygame.font.SysFont(‘Font style', font_size)

A render( ) method is used to create a surface object with text drawn on


it
font_variable.render(‘text to be displayed’, antialiasing, text color, bg
color)

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')

Pygame can load WAV, MP3, or OGG files.

To play the sound, call the Sound object’s play() method.


sound_object_variable.play()

To play the sound for few seconds, use sleep( ) method


time.sleep(no_of_seconds)

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.

From adding images to a game window to drawing geometrical shapes,


we learnt everything.

Without animation and sound any game is incomplete. So, we discussed


about that as well

With the help of Various code snippets, we explored programming using


pygame.

Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited

You might also like