Cheat Sheet on Pygame Module
Import & Initialization
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!
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
display.set_caption('Title of the window')
This function simply sets the argument as the title of the window.
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.
Creates a color object with RGBA as arguments.
This function is used to fill a solid color onto your screen. Arguments should be RGBA(Red, Green, Blue, Alpha) values.
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
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.
Creates a clock object and you can control the clock using tick() function
Returns the clock framerate
Returns the time used in previous tick
Pause for time specified
Common Event Loop
for event in pygame.event.get():
if event.type == 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.
Places a new event that you specify on the queue
Creates a new event object
Gets the event from the queue
removes all the events from the queue
Events are always in a queue. Order of events does matter.
Loads a new image from a file that you specify
You can save the image to your drive using this function
Initializes the mixer module
Loads the music file you specify as the argument
Plays the sound
Stops the music from being played
UnInitializes the mixer module
Quits the game