Show Menu
Cheatography

Pygame Cheat Sheet (DRAFT) by

A cheat sheet for the Pygame python library

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Pygame Basics

Importing
from pygame import *
Starting up
init()
Make the Screen
screen = displa­y.s­et_­mod­e((­width, height))
Quit pygame
quit()

Events

Get newest events
new_event = event.p­oll()
Check event type
if new_ev­ent.type == EVENT_­TYPE:
Event Type: Key Press
KEYDOWN
Event Type: Key Release
KEYUP
Event Type: Quitting
QUIT
Event Type: Mouse Movement
MOUSEM­OTION
Event Type: Mouse Press
MOUSEB­UTT­ONDOWN
Event Type: Mouse Release
MOUSEB­UTTONUP
Replace EVENT_TYPE in the if statement with one of the event types listed below

Keys

Checking which key
if new_ev­ent.key == KEY:
Key: Escape
K_ESCAPE
Key: Space
K_SPACE
Key: Up
K_UP
Key: Down
K_DOWN
Key: Left
K_LEFT
Key: Right
K_RIGHT
Replace KEY in the if statement with one of the Keys listed below.
The name of any of the letter keys is K_letter (e.g. the q key is K_Q, the w key is K_W etc.)

Text

Make font colour
colour = (R, G, B)
Set font size
font = font.F­ont­(None, size)
Set text co-ord­inates
location = (x, y)
Put it all together
screen.bl­it(­fon­t.r­end­er(­"­TEX­T", True, colour), location)
 

Images

Get image
image_name = image.l­oa­d("i­mag­e_f­ile.jp­g")
Put image on screen
screen.bl­it(­ima­ge_­name, (x,y))
Display screen
displa­y.u­pdate()
Rotate Image
image_name = transf­orm.ro­tat­e(i­mag­e_name, angle)
Flip Image
image_name = transf­orm.fl­ip(­ima­ge_­name, True, False)
Change Image Size
image_name = transf­orm.sc­ale­(im­age­_name, (width, height))
Check if two Images have collided
if image_­1.c­oll­ide­rec­t(i­mage_2)

Sound

Load sound
mixer.m­us­ic.l­oa­d('­fil­ena­me.m­p3')
Play sound once
mixer.p­lay(1)
Play sound x times
mixer.p­lay(x)
Play sound on loop
mixer.p­la­y(-1)
Stop sound
mixer.s­top()
Pause sound
mixer.p­ause()
UnPause sound
mixer.u­np­ause()
Fadeout sound before stopping
mixer.f­ad­eout()
Set volume of sound
mixer.m­us­ic.s­et­_vo­lum­e(0.1)

Mouse

Get Mouse Co-ord­inates
mouse.g­et­_pos()
Move Mouse
mouse.s­et­_po­s([x, y])
Hide Mouse
mouse.s­et­_vi­sib­le(­False)
Show Mouse
mouse.s­et­_vi­sib­le(­True)

Time

Time in millis­econds
time.g­et_­ticks()
Pause program for x time
time.w­ait(x)