Cheatography
https://cheatography.com
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 = display.set_mode((width, height)) |
Quit pygame |
quit() |
Events
Get newest events |
new_event = event.poll() |
Check event type |
if new_event.type == EVENT_TYPE: |
Event Type: Key Press |
KEYDOWN |
Event Type: Key Release |
KEYUP |
Event Type: Quitting |
QUIT |
Event Type: Mouse Movement |
MOUSEMOTION |
Event Type: Mouse Press |
MOUSEBUTTONDOWN |
Event Type: Mouse Release |
MOUSEBUTTONUP |
Replace EVENT_TYPE in the if statement with one of the event types listed below
Keys
Checking which key |
if new_event.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.Font(None, size) |
Set text co-ordinates |
location = (x, y) |
Put it all together |
screen.blit(font.render("TEXT", True, colour), location) |
|
|
Images
Get image |
image_name = image.load("image_file.jpg") |
Put image on screen |
screen.blit(image_name, (x,y)) |
Display screen |
display.update() |
Rotate Image |
image_name = transform.rotate(image_name, angle) |
Flip Image |
image_name = transform.flip(image_name, True, False) |
Change Image Size |
image_name = transform.scale(image_name, (width, height)) |
Check if two Images have collided |
if image_1.colliderect(image_2) |
Sound
Load sound |
mixer.music.load('filename.mp3') |
Play sound once |
mixer.play(1) |
Play sound x times |
mixer.play(x) |
Play sound on loop |
mixer.play(-1) |
Stop sound |
mixer.stop() |
Pause sound |
mixer.pause() |
UnPause sound |
mixer.unpause() |
Fadeout sound before stopping |
mixer.fadeout() |
Set volume of sound |
mixer.music.set_volume(0.1) |
Mouse
Get Mouse Co-ordinates |
mouse.get_pos() |
Move Mouse |
mouse.set_pos([x, y]) |
Hide Mouse |
mouse.set_visible(False) |
Show Mouse |
mouse.set_visible(True) |
Time
Time in milliseconds |
time.get_ticks() |
Pause program for x time |
time.wait(x) |
|