Cheatography
https://cheatography.com
Cheat Sheet on Pygame Module
Import & Initialization
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
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
Creates a color object with RGBA as arguments.
fill Function
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.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 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.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.
|
|
Images
pygame.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 |
Audio
pygame.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 |
Exit
pygame.quit() |
Quits the game |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets