Show Menu
Cheatography

Basic Pygame Cheat Sheet by

Cheat Sheet on Pygame Module

Import & Initia­liz­ation

import pygame
pygame.init()
This snippet of code imports pygame module and by using init function you can initialize pygame so that you can start rocking with game dev!

Create screen

pygame.display.set_mode((width, height))
Creates a window for your game, its similar to a canvas and it returns a surface. The arguments are width and height of the screen as a tuple

Set Title

display.set_caption('Title of the window')
This function simply sets the argument as the title of the window.

Update Display

pygame.display.update()
Updates the screen, basically redraws the main surface if arguments are not specified. And on the other hand, if you do happen to specify the arguments, it redraws the portions that you gave it.

Color

pygame.Color(R, G, B)
Creates a color object with RGBA as arguments.

fill Function

Surface.fill(color)
This function is used to fill a solid color onto your screen. Arguments should be RGBA(Red, Green, Blue, Alpha) values.
 

Set Font

pygame.font.SysFont('Font Name', FontSize)
This function lets you choose a font for your text that appears on the screen. It takes Font name and size as its arguments and it returns a font object

blit Function

Surface.blit(source, dest, area, speical_flags)
Draws one image onto another. Basically, it copies the pixels from one surface to another. It can be used to draw images to the screen.

Time

pygame.ti­me.C­lock()
Creates a clock object and you can control the clock using tick() function
pygame.ti­me.C­lo­ck.g­et­_fps()
Returns the clock framerate
pygame.ti­me.C­lo­ck.g­et­_time()
Returns the time used in previous tick
pygame.ti­me.d­elay()
Pause for time specified

Common Event Loop

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        pygame.quit()
One of the most common ways of event handling, Its a loop which constantly checks for events, quits if the QUIT event is triggered and prevents your game from freezing.

Event

pygame.ev­ent.post()
Places a new event that you specify on the queue
pygame.ev­ent.Ev­ent()
Creates a new event object
pygame.ev­ent.get()
Gets the event from the queue
pygame.ev­ent.cl­ear()
removes all the events from the queue
Events are always in a queue. Order of events does matter.
 

Images

pygame.im­age.load()
Loads a new image from a file that you specify
pygame.im­age.save()
You can save the image to your drive using this function

Audio

pygame.mi­xer.init()
Initia­lizes the mixer module
pygame.mi­xer.mu­sic.load()
Loads the music file you specify as the argument
pygame.mi­xer.mu­sic.play()
Plays the sound
pygame.mi­xer.mu­sic.stop()
Stops the music from being played
pygame.mi­xer.quit()
UnInit­ializes the mixer module

Exit

pygame.quit()
Quits the game
           
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Python Cheat Sheet