Pygame Cheat Sheet for Beginners
Pygame is a python framework for making 2D games. Creating games is a great fun and it's a great way to expand and implement your programming skills. If you know basics of python and know want to start with pygame, here is the Pygame Cheat Sheet for Beginners. I hope this will help you in your pygame learning journey. Share this article if you like our work.
Installing Pygame - Write and hit the following command in your command prompt or terminal.
pip install pygame
Pygame Basic Set Up
Import the pygame module
import pygame
Initialise Pygame
pygame.init()
Create game window
screen = pygame.display.set_mode((screen_width, screen_height))
Note that here we are passing a tuple as a parameter, that's why double parenthesis are used.
Fill Solid color in window
Fills the surface with solid color. Here the argument color is a tuple of RGB values. For example - (0,0,0) for Black.
screen.fill(color)
Update the game window
pygame.display.update()
Quit Pygame
pygame.quit()
Also Read - Digital Clock with Pygame
Using Images in Pygame
Loading Image
img = pygame.image.load("PATH_TO_IMAGE")
Draw image to game window
screen.blit(img, (img_x, img_y))
Here, the second parameter is a tuple in which we assign the position of image as coordinates of top - left corner of the image.
convert
Changes pixel format of the image to the format that is used by the main display. This increase the loading speed.
img.convert()
convert_alpha
Same as above, but when the surface has alpha transparency values to deal with.
img.convert_alpha()
Flip image
Here the first parameter is the image surface that has to be flipped. Second parameter is for horizontal flipping and third parameter is for vertical flipping. If you want to flip in both direction, set both the values True.
1) Flip image horizontally
img = pygame.transform.flip(img,True,False)
2) Flip image vertically
img = pygame.transform.flip(img,False,True)
3) Flip image both horizontally and vertically
img = pygame.transform.flip(img,True,True)
Resize Image
img = pygame.transform.scale(img,(new_width, new_height))
Rotate an image
Rotate the image counter clockwise by degrees.
img = pygame.transform.rotate(img,angle)
Get height and width of image
img_width = img.get_width()
img_height = img.get_height()
Get bounding rectangle that will tell you about the dimension and location of image
rect = img.get_rect()
Events in Pygame
Get list of events
events = pygame.event.get()
Check event type
for event in events:
if event.type == EVENT_TYPE:
Event types
1) Key pressed on Keyboard
pygame.KEYDOWN
2) Key released
pygame.KEYUP
3) QUIT - When someone presses the quit button of game window.
pygame.QUIT
4) Detect Mouse motion
pygame.MOUSEMOTION
5) Mouse button press
pygame.MOUSEBUTTONDOWN
6) Mouse button release
pygame.MOUSEBUTTONUP
Mouse
Get mouse current position
x,y = pygame.mouse.get_pos()
Set the mouse cursor position
pygame.mouse.set_pos([x,y])
Hide mouse
pygame.mouse.set_visible(False)
Show mouse
pygame.mouse.set_visible(True)
Also Read - Digital Clock with Pygame
Time
Clock in pygame
Create a new clock object that can be used to track the amount of time. The clock also provide some function to control the frame rate of the game.
clock = pygame.time.Clock()
Set frame rate (FPS)
This method counts how many milliseconds have passed since the last call. For example if FPS = 60, the program will never run at more than 60 frames per second.
clock.tick(FPS)
Pause the game
Pauses the game for the specified time.
pygame.time.delay(milliseconds)
Get time in milliseconds
Returns the number of milliseconds passed since pygame.init() was called.
pygame.time.get_ticks()
Text in pygame
Define Font color
color = (R,G,B)
Define Font
font = pygame.font.SysFont(name, size, bold = False, italic = False)
Set text co-ordinates
text_location = (x,y)
Draw text on screen
font.render creates a image object of text. It's second parameter is whether the text is anti-aliased or not. After rendering the font, we blit it on screen like an image.
screen.blit(font.render("Text",True,color), text_location)
Also Read - RGB Flashing Text with Pygame
Music
Load music
pygame.mixer.music.load("mymusic.mp3")
Play sound n times
pygame.mixer.music.play(loops = n)
Play music indefinitely
pygame.mixer.music.play(-1)
Stop music
pygame.mixer.music.stop()
Pause music
pygame.mixer.music.pause()
Unpause music
pygame.mixer.music.unpause()
Fade out before stopping
pygame.mixer.music.fadeout()
Restart music
pygame.mixer.music.rewind()
Set volume
The volume argument is a float between 0.0 and 1.0.
pygame.mixer.music.set_volume(volume)
Check if music playing
Returns True or False depending on whether music is playing or not.
pygame.mixer.music.get_busy()
Keys
Checking which key is pressed.
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == *KEY*
Here, *KEY* can be replaced by the following to check which key is pressed.
*KEY* | Common Name |
---|---|
pygame.K_BACKSPACE | Backspace |
pygame.K_ESCAPE | Escape |
pygame.K_SPACE | Space |
pygame.K_UP | Up |
pygame.K_DOWN | Down |
pygame.K_LEFT | Left |
pygame.K_RIGHT | Right |
pygame.K_HOME | Home |
pygame.K_END | End |
pygame.K_*letter* | *letter* = a,b,c...etc |
Post a Comment
For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.