PyGame Tutorial Last Updated : 08 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Pygame is a free and open-source library for making games and multimedia applications in Python. It helps us create 2D games by giving us tools to handle graphics, sounds and user input (like keyboard and mouse events) without needing to dig deep into complex stuff like graphics engines.Release date: 28 October 2000Programming languages: Python, C, Cython, Assembly languageDeveloper: Pete ShinnersLicense: GNU Lesser General Public LicenseStable release: 2.5.0 / 24 June 2023; 6 months agoWhat we can do with Pygame:Draw shapes, images and text on the screenPlay music and sound effectsDetect keyboard, mouse or joystick inputControl the frame rate of your gameBuild simple 2D games like platformers, puzzles or shootersInteresting Facts about PyGamePygame is Over Two Decades Old: Pygame was first released in the year 2000! It’s been helping people make games with Python for over 20 years.Built on Top of SDL: Pygame is a wrapper for SDL (Simple DirectMedia Layer), which is a powerful low-level multimedia library used in many commercial games.No Game Engine Needed: Unlike Unity or Unreal, Pygame is more low-level — it gives you the tools to build everything from scratch, helping you learn core game development logic.Tiny File Size: Despite its power, Pygame is lightweight and doesn’t require huge installations or tools.Pygame Zero is a Simplified Version: There’s a simpler version called Pygame Zero, which is made for kids and beginners to start coding games with almost zero setup.Code Example of Pygame:This code creates a basic Pygame window with the title "Hello Pygame".It runs a loop that keeps the window open and listens for events like closing the window.When the close button is clicked, the loop stops, and the game exits cleanly.It's the typical starting point for any Pygame project to get a working window on screen. Python import pygame # Initialize Pygame pygame.init() # Set up the game window screen = pygame.display.set_mode((400, 300)) pygame.display.set_caption("Hello Pygame") # Game loop running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Quit Pygame pygame.quit() OutputPyGamePyGame TutorialTo get the latest stable version of pygame, you can download it from this link.IntroductionIn this section, we’ll start with the basics of Pygame. You’ll get an idea of what Pygame is, how it works and why it’s popular for creating 2D games with Python. We’ll walk through how to set it up on both Windows and MacOS and even include a few interesting facts that’ll give you a better feel for the Pygame world.Introduction to pygameGetting Started with PygameHow to install Pygame in Windows?Install Pygame on MacOSInteresting Facts about PYGAMEGetting StartedIn this part, we’ll learn how to import and initialize Pygame, create a game window, customize things like the window name, background color and icon and understand how the game loop works. We’ll also touch on key concepts like surfaces and handling time in Pygame and all the building blocks needed before you start adding game elements.PyGame – Import and InitializeHow to initialize all the imported modules in PyGame?How to create an empty PyGame window?How to get the size of the PyGame Window?Allowing resizing window in PyGameHow to change screen background color in Pygame?How to Change the Name of a Pygame window?How to set up the Game Loop in PygGame?How to change the PyGame icon?Pygame - SurfacesPygame - TimeDrawing ShapesIn this section, we’ll explore how to draw basic shapes using Pygame. You’ll learn how to create rectangles (including ones with rounded corners) and how to use Pygame’s drawing tools to place different shapes and objects onto the game window. These are the visual elements that help bring your game to life.Pygame - Drawing Objects and shapesPython | Drawing different shapes on PyGame windowHow to draw a rectangle in Pygame?How to draw a rectangle with rounded corners in PyGame?Event HandlingIn this part, we’ll look at how your game can react to player actions. You’ll learn how event handling works in Pygame and from detecting key presses to creating custom events. We’ll also cover how to handle user input and even play audio files, making your game feel more interactive and dynamic.Pygame - Event HandlingHow to add Custom Events in Pygame?Pygame - Input HandlingHow to get keyboard input in pygame?Python | Playing audio file in PygameWorking with TextIn this section, you’ll learn how to work with text in Pygame. We’ll cover how to display text on the screen, customize its appearance and even create a text input box so users can type into your game. This is useful for things like scores, messages or entering player names.Pygame – Working with TextPython | Display text to PyGame windowHow to create a text input box with Pygame?Working with imagesIn this section, you’ll learn how to load and display images in Pygame. We’ll cover how to get image dimensions, rotate, scale and flip images and even let users interact with images using the mouse. These skills will help you add characters, backgrounds and interactive visuals to your game.Python | Display images with PyGameGetting the width and height of an image in PygameHow to Rotate and Scale images using PyGame?Pygame - Flip the imageHow to move an image with the mouse in PyGame?How to use the mouse to scale and rotate an image in PyGame?PyGame AdvanceYou’ll learn how to create buttons, move objects with keyboard input, make them jump, add boundaries and handle collisions. We’ll also cover working with sprites, how to create them, control them and add cool visual effects like color breezing. These topics will help take your game from basic to polished and interactive.How to Create Buttons in a game using PyGame?Python – Drawing design using arrow keys in PyGamePython – Moving an object in PyGamePython | Making an object jump in PyGameAdding Boundary to an Object in PygameCollision Detection in PyGamePygame - Creating SpritesPygame - Control SpritesHow to add color breezing effect using pygame?Playing audio files in PygameExercise, Applications and ProjectsIn this section, you’ll put everything you’ve learned into practice through fun exercises, creative visualizations and full projects. From classic games like Snake and Tic Tac Toe to cool effects like snowfall and color breezing, you’ll build real applications using Pygame. You’ll also explore how to visualize algorithms like sorting and searching and even create a Sudoku game. This is where your coding skills turn into playable, visual experiences!How to add color breezing effect using pygame?Snowfall display using Pygame in PythonRhodonea Curves and Maurer Rose in PythonCreating start Menu in PygameTic Tac Toe GUI In Python using PyGameSnake Game8-bit game using pygameBubble sort visualizer using PyGameTernary Search Visualization using Pygame in PythonSorting algorithm visualization: Heap SortSorting algorithm visualization: Insertion SortBinary Search Visualization using Pygame in PythonBuilding and visualizing Sudoku Game Using Pygame Comment More infoAdvertise with us Next Article Introduction to pygame N nikhilaggarwal3 Follow Improve Article Tags : Python Python-PyGame Tutorials Practice Tags : python Similar Reads PyGame Tutorial Pygame is a free and open-source library for making games and multimedia applications in Python. It helps us create 2D games by giving us tools to handle graphics, sounds and user input (like keyboard and mouse events) without needing to dig deep into complex stuff like graphics engines.Release date 7 min read IntroductionIntroduction to pygamePygame is a set of Python modules designed for writing video games. It adds functionality on top of the excellent SDL library, enabling you to create fully-featured games and multimedia programs in the Python language. It's key benefits include:Beginner-Friendly: Simple Python syntax makes it ideal 4 min read Getting Started with PygamePygame is a free-to-use and open-source set of Python Modules.  And as the name suggests, it can be used to build games. You can code the games and then use specific commands to change it into an executable file that you can share with your friends to show them the work you have been doing.  It incl 3 min read How to Install Pygame on Windows ?In this article, we will learn how to Install PyGame module of Python on Windows. PyGame is a library of python language. It is used to develop 2-D games and is a platform where you can set python modules to develop a game. It is a user-friendly platform that helps to build games quickly and easily. 2 min read Install Pygame in MacOSPyGame is a collection of modules that break through the language of Python applications. These modules are designed to edit video games. PyGame, therefore, includes computer graphics and audio libraries created for the use and language of Python programs. At first, open the Terminal which is locate 1 min read Interesting Facts about PYGAMEPygame is a set of python module which is used in designing video games. In Pygame, there are computer graphics and sound libraries in order to develop high quality and user interactive games. Pygame was developed by Pete Shinners. Till 2000, it was a community project, later on it was released unde 2 min read Getting StartedPyGame - Import and InitializeIn 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 pygameImporting the Pygame library To import the pygame library, make sure you have installed pygame alrea 2 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 initial 2 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 t 2 min read How to get the size of PyGame Window?In this article, we will learn How to get the size of a PyGame Window. 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 1 min read Allowing resizing window in PyGameIn this article, we will learn How to allow resizing a PyGame Window. 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 2 min read How to change screen background color in Pygame?Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Functions Used: pygame.init(): This function is used to initialize all the pygame 1 min read How to Change the Name of a Pygame window?PyGame window is a simple window that displays our game on the window screen. By default, pygame uses "Pygame window" as its title and pygame icon as its logo for pygame window. We can use set_caption() function to change the name and set_icon() to set icon of our window. To change the name of pygam 2 min read How to set up the Game Loop in PygGame ?In this article, we will see how to set up a game loop in PyGame. Game Loop is the loop that keeps the game running. It keeps running till the user wants to exit. While the game loop is running it mainly does the following tasks: Update our game window to show visual changesUpdate our game states ba 3 min read How to change the PyGame icon?While building a video game, do you wish to set your image or company's logo as the icon for a game? If yes, then you can do it easily by using set_icon() function after declaring the image you wish to set as an icon. Read the article given below to know more in detail. Syntax: pygame_icon = pygame 2 min read Pygame - SurfaceWhen 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 an 6 min read Pygame - TimeWhile using pygame we sometimes need to perform certain operations that include the usage of time. Like finding how much time our program has been running, pausing the program for an amount of time, etc. For operations of this kind, we need to use the time methods of pygame. In this article, we will 4 min read Drawing ShapesPygame â Drawing Objects and ShapesIn 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 Rectangl 10 min read Python | Drawing different shapes on PyGame windowPygame 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 th 3 min read How to draw rectangle in Pygame?Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. Functions  Used: pygame.display.set_mode(): This function is used to initialize a 1 min read How to draw a rectangle with rounded corner in PyGame?Pygame is a Python library designed to develop video games. Pygame adds functionality on top of the excellent SDL library. This allows you to create fully featured games and multimedia programs in the python language. In this article, we will see how can we draw a rectangle with rounded corners in P 2 min read Event HandlingHow to add Custom Events in Pygame?In this article, we will see how to add custom events in PyGame. Installation PyGame library can be installed using the below command: pip install pygame Although PyGame comes with a set of events (Eg: KEYDOWN and KEYUP), it allows us to create our own additional custom events according to the requ 4 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() functio 3 min read Working with TextPygame - Working with TextIn this article, we will see how to play with texts using the Pygame module. We will be dealing here with initializing the font, rendering the text, editing the text using the keyboard, and adding a blinking cursor note. Installation To install this module type the below command in the terminal. pi 5 min read Python | Display text to PyGame windowPygame 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 usin 6 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 wri 3 min read Working with imagesPython | Display images with PyGamePygame 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 usin 2 min read Getting width and height of an image in PygamePrerequisites: 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 s 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 co 3 min read Pygame - Flip the imageIn this article, we are going to see how images can be flipped using Pygame. To flip the image we need to use pygame.transform.flip(Surface, xbool, ybool) method which is called to flip the image in vertical direction or horizontal direction according to our needs. Syntax: pygame.transform.flip(Surf 2 min read How to move an image with the mouse in PyGame?Pygame is a Python library that is used to create cross-platform video games. The games created by Pygame can be easily run through any of the input devices such as a mouse, keyboard, and joystick. Do you want to make a game that runs through mouse controls? Don't you know how to move the image with 4 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 th 5 min read PyGame AdvanceHow to create Buttons in a game using PyGame?Pygame is a Python library that can be used specifically to design and build games. Pygame only supports 2D games that are build using different shapes/images called sprites. Pygame is not particularly best for designing games as it is very complex to use and lacks a proper GUI like unity gaming eng 3 min read Python - Drawing design using arrow keys in PyGamePygame 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 th 3 min read Moving an object in PyGame - PythonTo make a game or animation in Python using PyGame, moving an object on the screen is one of the first things to learn. We will see how to move an object such that it moves horizontally when pressing the right arrow key or left arrow key on the keyboard and it moves vertically when pressing up arrow 2 min read Python | Making an object jump in PyGamePygame 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 th 3 min read Adding Boundary to an Object in PygameBoundaries 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 revers 5 min read Collision Detection in PyGamePrerequisite: 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 thi 7 min read Pygame - Creating SpritesSprites are objects, with different properties like height, width, color, etc., and methods like moving right, left, up and down, jump, etc. In this article, we are looking to create an object in which users can control that object and move it forward, backward, up, and down using arrow keys. Let fi 2 min read Pygame - Control SpritesIn this article, we will discuss how to control the sprite, like moving forward, backward, slow, or accelerate, and some of the properties that sprite should have. We will be adding event handlers to our program to respond to keystroke events, when the player uses the arrow keys on the keyboard we w 4 min read How to add color breezing effect using pygame?Pygame is a python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different sprites. Pygame is not particularly best for designing games as it is very complex to use and doesnât have a proper GUI like unity but it definitely builds 2 min read Python | Playing audio file in PygameGame 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 doin 2 min read Exercise, Applications, and ProjectsSnowfall display using Pygame in PythonNot everybody must have witnessed Snowfall personally but wait a minute, What if you can see the snowfall right on your screen by just a few lines of creativity and Programming.  Before starting the topic, it is highly recommended revising the basics of Pygame. Steps for snowfall creation 1. Import 3 min read Rhodonea Curves and Maurer Rose in PythonIn this article, we will create a Rhodonea Curve and Maurer Rose pattern in Python! Before we proceed to look at what exactly is a rhodonea curve or maurer rose we need to get the basic structure of our program ready! Basic Structure of the Program - Before we move on to learn anything about Rhodon 7 min read Creating start Menu in PygamePygame is a Python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different shapes or sprites. Pygame doesn't have an in-built layout design or any in-built UI system this means there is no easy way to make UI or levels for a game. 3 min read Tic Tac Toe GUI In Python using PyGameThis article will guide you and give you a basic idea of designing a game Tic Tac Toe using pygame library of Python. 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 l 15+ min read Snake Game in Python - Using Pygame moduleSnake game is one of the most popular arcade games of all time. In this game, the main objective of the player is to catch the maximum number of fruits without hitting the wall or itself. Creating a snake game can be taken as a challenge while learning Python or Pygame. It is one of the best beginne 15+ min read 8-bit game using pygamePygame is a python library that can be used specifically to design and build games. Pygame supports only 2d games that are built using different sprites. Pygame is not particularly best for designing games as it is very complex to use doesnât have a proper GUI like unity but it definitely builds log 9 min read Bubble sort visualizer using PyGameIn 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 3 min read Ternary Search Visualization using Pygame in PythonAn algorithm like Ternary Search can be understood easily by visualizing. In this article, a program that visualizes the Ternary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any s 5 min read Sorting algorithm visualization : Heap SortAn algorithm like Heap sort can be understood easily by visualizing. In this article, a program that visualizes the Heap Sort Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach: Generate random array and fill the pygame window wi 4 min read Sorting algorithm visualization : Insertion SortAn algorithm like Insertion Sort can be understood easily by visualizing. In this article, a program that visualizes the Insertion Sort Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in python using pygame library. Approach: Generate random array and fill the pygame 3 min read Binary Search Visualization using Pygame in PythonAn algorithm like Binary Search can be understood easily by visualizing. In this article, a program that visualizes the Binary Search Algorithm has been implemented. The Graphical User Interface(GUI) is implemented in Python using pygame library. Approach Generate random array, sort it using any sor 4 min read Building and visualizing Sudoku Game Using PygameSudoku is a logic-based, combinatorial number-placement puzzle. The objective is to fill a 9Ã9 grid with digits so that each column, each row, and each of the nine 3Ã3 subgrids that compose the grid contain all of the digits from 1 to 9. We will be building the Sudoku Game in python using pygame li 7 min read Create Bingo Game Using PythonA card with a grid of numbers on it is used to play the popular dice game of bingo. Players check off numbers on their cards when they are selected at random by a caller, competing to be the first to mark off all of their numbers in a particular order. We'll examine how to utilise Python to create a 9 min read Create Settings Menu in Python - PygamePython is a flexible programming language with a large selection of libraries and modules for a variety of applications. Pygame menu is one such toolkit that enables programmers to design graphical user interfaces for games and apps. In this tutorial, we'll look at how to use the pygame menu package 9 min read Car Race Game In PyGameIn this article, we will see how to create a racing car game in Python using Pygame. In this game, we will have functionality like driving, obstacle crashing, speed increment when levels are passed, pause, countdown, scoreboard, and Instruction manual screen. Required Modules: Before going any furt 15+ min read Spiral Sprint Game in Python Using PygameIn this article, we will see how to create a spiral sprint game in Python using Pygame. In this game, we will have functionality like difficulty modes, obstacle crashing, speed increment when points are increased, scoreboard, Particle animation, color-changing orbits, coins, and game sounds. Spiral 15+ min read Selection sort visualizer using PyGameIn this article, we will see how to visualize Selection sort using a Python library PyGame. It is easy for the human brain to understand algorithms with the help of visualization. Selection sort is a simple and easy-to-understand algorithm that is used to sort elements of an array by dividing the ar 3 min read Mouse Clicks on Sprites in PyGameThe interactiveness of your game can be significantly increased by using Pygame to respond to mouse clicks on sprites. You may develop unique sprite classes that manage mouse events and react to mouse clicks with the aid of Pygame's sprite module. This article will teach you how to use Pygame to mak 3 min read Slide Puzzle using PyGame - PythonSlide 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 n 14 min read Brick Breaker Game In Python using PygameBrick Breaker is a 2D arcade video game developed in the 1990s. The game consists of a paddle/striker located at the bottom end of the screen, a ball, and many blocks above the striker. The basic theme of this game is to break the blocks with the ball using the striker. The score is calculated by th 14 min read Hover Button in PygameHere, we will talk about while hovering over the button different actions will perform like background color, text size, font color, etc. will change. In this article we are going to create a button using the sprites Pygame module of Python then, when hovering over that button we will perform an eve 6 min read Create a Pong Game in Python - PygamePong is a table tennis-themed 2-player 2D arcade video game developed in the early 1970s. The game consists of two paddles/strikers, located at the left and right edges of the screen, and a ball. Create a Pong Game in PythonThe basic theme of this game is to make sure that the ball doesn't hit the w 10 min read Save/load game Function in PygamePygame 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 t 4 min read Like