The Wayback Machine - https://web.archive.org/web/20240814173102/https://www.geeksforgeeks.org/pygame-import-and-initialize/
Open In App

PyGame – Import and Initialize

Last Updated : 21 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Previous Article
Next Article

Similar Reads

Adding Collisions Using pygame.Rect.colliderect in Pygame
Prerequisite: Drawing shapes in Pygame, Introduction to pygame In this article, we are going to use pygame.Rect.colliderect for adding collision in a shape using Pygame in Python. We can easily add collisions in Pygame shapes using the colliderect( ). For this, we are going to draw two rectangles then we will check if the rectangles are colliding o
3 min read
How to initialize all the imported modules in PyGame?
PyGame is Python library designed for game development. PyGame is built on the top of SDL library so it provides full functionality to develop game in Python. Pygame has many modules to perform it's operation, before these modules can be used, they must be initialized. All the modules can be initialized individually or one at a time. This post desc
2 min read
Getting width and height of an image in Pygame
Prerequisites: Pygame To use graphics in python programs we use a module called Pygame. Pygame provides high functionality for developing games and graphics in Python. Nowadays Pygame are very much popular to build simple 2D games. In order to run a program written in Python using Pygame module, a system must have Python installed with Pygame modul
3 min read
How to Rotate and Scale images using PyGame ?
In this article, we are going to see how to Rotate and Scale the image. Image Scaling refers to the resizing of the original image and Image Rotation refers to turning off an image with some angle. Rotations in the coordinate plane are counterclockwise. Let's proceed with the methods used and the complete code to perform these tasks. Scaling the Im
3 min read
How to use the mouse to scale and rotate an image in PyGame ?
In this article, we will discuss how to transform the image i.e (scaling and rotating images) using the mouse in Pygame. Approach Step 1: First, import the libraries Pygame and math. import pygame import math from pygame.locals import * Step 2: Now, take the colors as input that we want to use in the game. color_1 = #RGB value of color 1 color_2 =
5 min read
Pygame – Drawing Objects and Shapes
In this article, we are going to see how to draw an object using Pygame. There can be two versions for drawing any shape, it can be a solid one or just an outline of it. Drawing Objects and Shapes in PyGame You can easily draw basic shapes in pygame using the draw method of pygame. Drawing Rectangle shape: To draw a rectangle in your pygame project
10 min read
How to create MS Paint clone with Python and PyGame?
In this article, we will create a simple MS paint program with Python and PyGame. MS Paint is a simple program made by Microsoft, it allows users to create basic art and painting. Since its inception, MS Paint has been included with every version of Microsoft Windows. MS Paint provides features for drawing and painting in color and tools to create
9 min read
Differences Between Pyglet and Pygame in Python
In this article, we will see the difference between Pygame and Pyglet gaming libraries. What is Pyglet? Pyglet is a cross-platform window and media library for Python, intended for developing games and other visually rich applications. It supports windows, UI event handling, gamepads, OpenGL graphics, image and video loading, and sound and music pl
4 min read
Build a Music Player with Tkinter and Pygame in Python
We all use music players like Windows Media Player or VLC to play music on our system. Have you ever wanted to create your own music player if your answer is yes then with Python, you can build a customized Music Player. In this article, you will see how to build a simple yet functional music player using two powerful Python libraries: Tkinter and
7 min read
How to create a text input box with Pygame?
In this article, we will discuss how to create a text input box using PyGame. Installation Before initializing pygame library we need to install it. This library can be installed into the system by using pip tool that is provided by Python for its library installation. Pygame can be installed by writing these lines into the terminal. We can install
3 min read
How to add moving platforms in PyGame
Prerequisite: Drawing in Pygame In this article, we will learn how we can add moving platforms to our game using PyGame in Python. Creating a Platform We can easily create any type of platform in pygame using the draw( ) method. For this, we will create react with specific width and height using the draw.rect() function. Syntax: pygame.draw.rect(su
5 min read
How to get keyboard input in PyGame ?
While using pygame module of Python, we sometimes need to use the keyboard input for various operations such as moving a character in a certain direction. To achieve this, we have to see all the events happening. Pygame keeps track of events that occur, which we can see with the events.get() function. In this article, we are going to discuss how we
3 min read
Pygame - Surface
When using Pygame, surfaces are generally used to represent the appearance of the object and its position on the screen. All the objects, text, images that we create in Pygame are created using surfaces. Creating a surface Creating surfaces in pygame is quite easy. We just have to pass the height and the width with a tuple to pygame.Surface() metho
6 min read
Python | Display images with PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip in
2 min read
Python | Display text to PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of the developer, what type of game he/she wants to develop using this toolkit.. Command to install pygame on wind
6 min read
Python | Drawing different shapes on PyGame window
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. Command to install pygame : pip instal
3 min read
Introduction to pygame
Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI, and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the best modules for doing so. Installing pygame: Pygame requires Python;
5 min read
PyGame Set Mouse Cursor from Bitmap
In this article, we are going to see how to set the mouse cursor from bitmap using the PyGame module in Python. What is PyGame?It is a cross-platform set of Python modules designed for writing video games.It includes computer graphics and sound libraries designed to be used with the Python programming language.It can handle time, video, music, font
5 min read
How to create an empty PyGame window?
Pygame window is a simple window like any other window, in which we display our game screen. It is the first task we do so that we can display our output onto something. Our main goal here is to create a window and keep it running unless the user wants to quit. To perform these tasks first we need to install pygame package and import some pre-defin
2 min read
Python | Making an object jump in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. So, in this article, we will learn how
3 min read
Python | Playing audio file in Pygame
Game programming is very rewarding nowadays and it can also be used in advertising and as a teaching tool too. Game development includes mathematics, logic, physics, AI and much more and it can be amazingly fun. In python, game programming is done in pygame and it is one of the best modules for doing so. Note: For more information, refer to Introdu
2 min read
Python - Moving an object in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this tutorial, we are going to lear
3 min read
Slide Puzzle using PyGame - Python
Slide Puzzle game is a 2-dimensional game i.e the pieces can only be removed inside the grid and reconfigured by sliding them into an empty spot. The slide puzzle game developed here is a 3X3 grid game i.e 9 cells will be there from 1 to 8(9 is not here since a blank cell is needed for sliding the neighboring cells). In this article, we are going t
15 min read
Python - Drawing design using arrow keys in PyGame
Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries designed to be used with the Python programming language. Now, it’s up to the imagination or necessity of developer, what type of game he/she wants to develop using this toolkit. In this article we will see how we can
3 min read
Bubble sort visualizer using PyGame
In this article we will see how we can visualize the bubble sort algorithm using PyGame i.e when the pygame application get started we can see the unsorted bars with different heights and when we click space bar key it started getting arranging in bubble sort manner i.e after every iteration maximum value element should come at last. Bubble Sort is
3 min read
Save/load game Function in Pygame
Pygame is a free-to-use and open-source set of Python Modules. And as the name suggests, it can be used to build games, and to save game data or the last position of the player you need to store the position of the player in a file so when a user resumes the game its game resumes when he left. In this case, the text file with JSON format stores the
4 min read
Pygame - Random Movement of Object
Prerequisite: How to draw rectangle in Pygame? In this article, we will learn how we can add random movements in objects using PyGame in Python. Random movement in a single object To add random movement in our object we are going to use randint method of the random module. Syntax: random.randint(a, b) Parameters: a and b : Two integers Returns: Ret
6 min read
Install Pygame in Linux
In this article, we will discuss how to install PyGame in the Linux system. We are using Ubuntu 20 LTS you can use any other one. To successfully install PyGame in your Linux system, follow the below procedure: First Check you are using python's latest version or not. Because the PyGame is only supported python 3.7.7 or higher version so make sure
2 min read
Collision Detection in PyGame
Prerequisite: Introduction to pygame Collision detection is a very often concept and used in almost games such as ping pong games, space invaders, etc. The simple and straight forward concept is to match up the coordinates of the two objects and set a condition for the happening of collision. In this article, we will be detecting a collision betwee
7 min read
Adding Boundary to an Object in Pygame
Boundaries to any game are very important. In snake games, space invaders, ping pong game, etc. the boundary condition is very important. The ball bounces at the boundaries of the screen in ping pong games. So, the idea behind this boundaries is to change the position of the ball or object in reverse direction as soon as it hits the wall. Let us se
5 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg