PyGame
PyGame in a python library to develop games.
Before starting with pygame we have to install pygame and we must have some programming knowledge (Python Preferred).
pip install is simplest way
Getting Started with PyGame
As we know, to play any game we first need a window.So we will first create a window in the PyGame
#Importing pygame import pygame pygame.init() #Setting name of the window pygame.display.set_caption("pygame window") #Creating a window window = pygame.display.set_mode((400, 300)) done = False #Loop to control our window while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True pygame.display.flip() #Quit pygame pygame.quit() |
---|
Output

Let's see the functionality of our code
- First we imported Pygame library
- Initializing all modules of pygame (pygame.init())
- pygame.display.set_caption , used to display name of the window.
- After that we have created a window. (pygame.display.set_mode(), is a function in pygame to create window)
- The loop that is placed to stop our windows on the screen.(If we do not loop, our window will be created but will not stop)
- Inside the event, we handle all the actions that occur above the window.
- Like here we have given the message that if we quit the window (event.type == pygame.QUIT) means we close the window, then done become true means our window is closed.
- pygame.display.flip(),It allows only a portion of the screen to updated, instead of the entire area.
Interactivity or action on keys
Image Loading using Pygame
Let's see it with an example
import pygame pygame.init() window = pygame.display.set_mode((500, 500)) done = False clock = pygame.time.Clock() while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True screen.fill((255, 255, 255)) image = pygame.image.load(r'hhh.jpg') window.blit(image, (20, 20)) pygame.display.flip() clock.tick(60) pygame.quit() |
---|
Output
Let's see the functionality of our code
- What's new for us here,that is pygame.image.load() function, which is used here to load the image.
- And next is window.blit(),This function says take the image and draw it onto the window and position it at (x,y).
- As you can see from the output, our image is larger than our window size, this means that we do not need to take the image size smaller than the window size (window width and window height)
- (x,y) this is our coordinates where we draw the images on the window.
Font Style and size in Pygame
import pygame pygame.init() window = pygame.display.set_mode((300, 300)) clock = pygame.time.Clock() done = False font = pygame.font.SysFont(None,50) text = font.render("Goeduhub", True, (255, 0, 0)) while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True window.fill((255, 255, 255)) window.blit(text, (150 - text.get_width() // 2, 140 - text.get_height() // 2)) pygame.display.flip() clock.tick(60) pygame.quit() |
---|
Output

Let's see the functionality of our code
- The pygame.font.SysFont(text_style,text_size) ,function is used in pygame which describes the style of a text and the size of the text.Here we passed None in as style of text means, it will take default system text style.
- The font.render (text,True , color)- Create a Text surface object i.e.surface object in which Text is drawn on it.
- window.fill(color), it just fill our window with color which we defined in it , by default our window is black , here we filled it by white color
Sound and Music in pygame
Snake Game in Python with Pygame
Python Tutorial
Machine Learning Tutorial
AI Tutorial
Free Online Tutorials