Skip to content
Related Articles

Related Articles

Improve Article
PyGame – Import and Initialize
  • Difficulty Level : Medium
  • Last Updated : 21 Apr, 2021

In this article, we will see how to import and initialize PyGame.

Installation

The best way to install pygame is with the pip tool, we can install pygame by using the below command:

pip install pygame

Importing the Pygame library

To import the pygame library, make sure you have installed pygame already. Open the terminal or the IDE which you want to work upon and import this library to check whether pygame is installed or not by using the command given below:

import pygame 

This library imports all the available pygame modules into the pygame package.The below image how to perform it with the help of a command prompt. If you don’t find any error and get the message as shown in the image means the pygame library is imported successfully, and now you are good to proceed. 



Initializing the Pygame library

Before we can do much with pygame, we first need to initialize it. Just call the method given below to initialize it. 

pygame.init()

This method initializes all the necessary pygame modules. This method has nothing like an argument and returns a tuple that returns the no of modules initialized successfully and the number of modules failed respectively. 

As you can see in the image pygame.init() method returns a tuple (7,0) which means 7 modules are initialized successfully and 0 is failed to initialize. 

Check whether all the modules are initialized successfully or not

There is a method named pygame.get_init() which shows whether all the pygame modules are initialized successfully or not. If all the modules are initialized successfully, this method returns true.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :