Cheatography
https://cheatography.com
Cheat Sheet on Pygame Module
Import & Initializationimport 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 screenpygame.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 Titledisplay.set_caption('Title of the window')
|
This function simply sets the argument as the title of the window.
Update DisplayUpdates 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.
ColorCreates a color object with RGBA as arguments.
fill FunctionThis function is used to fill a solid color onto your screen. Arguments should be RGBA(Red, Green, Blue, Alpha) values.
| | Set Fontpygame.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 FunctionSurface.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.
Timepygame.time.Clock() | Creates a clock object and you can control the clock using tick() function | pygame.time.Clock.get_fps() | Returns the clock framerate | pygame.time.Clock.get_time() | Returns the time used in previous tick | pygame.time.delay() | Pause for time specified |
Common Event Loopfor 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.
Eventpygame.event.post() | Places a new event that you specify on the queue | pygame.event.Event() | Creates a new event object | pygame.event.get() | Gets the event from the queue | pygame.event.clear() | removes all the events from the queue |
Events are always in a queue. Order of events does matter.
| | Imagespygame.image.load() | Loads a new image from a file that you specify | pygame.image.save() | You can save the image to your drive using this function |
Audiopygame.mixer.init() | Initializes the mixer module | pygame.mixer.music.load() | Loads the music file you specify as the argument | pygame.mixer.music.play() | Plays the sound | pygame.mixer.music.stop() | Stops the music from being played | pygame.mixer.quit() | UnInitializes the mixer module |
Exitpygame.quit() | Quits the game |
|
Help Us Go Positive!
We offset our carbon usage with Ecologi. Click the link below to help us!
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets