Python : #1 Introduction to pygame module

Hi all,

Today we are going to start the series of pygame module. It will be very interesting series so please be with this series.

What is pygame module?

pygame is a module of a Python which helps us to create games or other applications. We can control our apps or games with the logic and graphics.

Installing pygame

We can simply install the pygame using pip in the terminal. To know more about pip you can surely check our article – https://knowledge-junction.com/2021/07/25/most-popular-tool-for-installing-python-packages-pip/

pip install pygame

Fig-1 Python - Installing pygame
Fig-1 Python – Installing pygame

Once you installed the pygame you are ready to work with it.

Now we will import pygame

import pygame

Just to check the pygame is installed successfully or not we can import pygame and run it. If it shows this message then it is installed.

Fig - 2 Python - After running the code import pygame
Fig – 2 Python – After running the code import pygame

Now we will initialize the pygame module

import pygame

# Initializing the modules
pygame.init()

This initialize the modules in the pygame which are necessary. We can check that how many modules have initialized and how many errors have occurred.

import pygame

# We will store pygame.init() in variable so that we can print that variable and see what does it return

initilize_pygame_module = pygame.init()
print(initilize_pygame_module)

Output

Fig - 3 Python - After running the above program
Fig – 3 Python – After running the above program

The output shows that it has initialized the 5 modules with 0 errors. This was only for the testing we can remove the print statement.

Now the most common and basic thing in all the games is display or a screen so we will create the display in the pygame.

import pygame
initilize_pygame_module = pygame.init()

#Creating the display or window
 '''pygame.display.set_mode takes the input width and height and creates the display'''
pygame.display.set_mode((900,600))

Output

The display will be creating but it will disappear very quickly.

We will solve this problem in next article so be with the series.

Thanks for reading this article 🙂

Have a great day ahead!

You may also like...

1 Response

  1. July 23, 2022

    […] who has not read mine last article they can read – https://knowledge-junction.com/2022/07/22/python-1-introduction-to-pygame-module/ so that you will understand better in this […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: