Super Mario game in Python using Pygame

Super Mario game in Python

Have you ever wanted to have the power to create the Mario game from scratch? Now, you can create your own Super Mario game in Python and Pygame! This step-by-step tutorial with source code will walk you through the entire process, from setting up the game environment to creating your own Mario game in Python.

In addition, we’ll be able to customize the game by adding characters, levels, and power-ups to make the game unique. So get ready to explore the world of game development and learn how to create a Super Mario game in Python using Pygame.

Setting Up the Game Environment

We want to set up our game environment so that we can start designing our game. At the highest level, we’ll have three modules in your code: the game module, the level module, and the sprite module. The game module will contain game logic (i.e., what happens when a player dies), and the level module will include game design (i.e., what the level looks like). 

The sprite module will contain our sprites (i.e., your characters, coins, blocks, etc.). It’s important to separate our game design and game logic because we want to minimize the dependencies between our modules so that they are easier to understand and modify. 

We’ll also need to install the Pygame library( pip install pygame==2.1.3.dev8 ), allowing us to create the game from scratch. You can find installation instructions here. Once you have Pygame installed, you should be able to open up your Python IDE and start creating your game.

Folder structure of game

Folder structure of game

Designing the Level and Adding Sprites

Now that we have our environment set up for the Mario game in Python, it’s time to create the level for the game. A level represents the game space that includes the general environment and obstacles that the player needs to overcome (e.g., ground, grass, rocks, trees, mountains, vines, clouds, and rivers). We can create the level using a graphical editor like Tiled. There are also many visual-level design tools available online.

You can create a level by placing various sprites, such as coins, blocks, and vines. Once we have your level designed, we will need to save it as an image file (.png) and then load it into your Python IDE. You can also find thousands of sprites online, which you can use to create various characters and obstacles in your game.

Designing the Level and Adding Sprites

Programming the Character Movement

Once the level is designed, you can start programming the character movement. First, we’ll need to import the pygame library in our IDE and then write a few functions for a player to do the following things:

  • Move left and right – Move up or down a level. 
  • Collect coins. 
  • Avoid rocks and vines. 
  • And die when it hits an enemy. 

Once We’ve written these functions, you can call them from your main game loop. You’ll also need to create a function that tells the player what to do when a power-up is collected. You can also create a function to create the enemy characters by spawning them at random places.

Creating the Enemy AI

Enemy characters are controlled by the computer and move toward the player’s character. They are programmed to avoid moving toward the edge of the level unless they are going down a level. Create a function that will tell the computer how to decide where to go. For example, the enemy can go toward the player or a random location.

We can also choose whether the enemy should go down a level or stay on the same level. To make our enemies more challenging, We can add conditions to their movement, such as going toward the player only if the player is close to them. Also, we add an idle state to make your enemies stop moving when the player is far away from them.

When the player gets close to the enemy, the enemy should start running toward the player again. Then create a function to generate a new enemy at a random location whenever the player goes down a level.

enemy images

Adding the Power-Ups and Coins

To create the power-ups, We will need to create a function that will pick a random power-up from a list of power-ups. We can also create a function to tell the computer when to give the player a power-up after making a list of coins for the level. Pick a random coin from a list of coins, and then place the coin at a random location in the level. Finally, we can create a function that tells the computer when to give the player a coin. Once we have started these functions, you can add them to the game loop.

Power Up:

power up

Tiles and Coins:

tiles and coins

Making the Level Interactive

We can make the level more interactive by adding sounds whenever a power-up is collected, or the player picks up a coin. Also, make vines come down from the top of the level when a player goes down a level. We can make ground disappear when a player goes down a level and make rocks fall down from the top of the level.

We can also make vines come down from the bottom of the level when the player goes up a level. We can make the ground appear when a player goes up a level, and rocks appear at the bottom when a player goes down a level. We can also make water flow from the top to the bottom when a player goes down a level.

Level – 1

level 1

Level – 2

level 2

Level – 3

level 3

Level – 4

level 4

Adding the Title Screen and Game Over Screen

Now that We have completed most of our game Create a title screen and a game over screen to finish the game. Which tells the player how many coins they’ve collected and their score. You can create your title screen using a logo or a picture, and you can create your game over the screen by using a message to inform the player that the game has ended.

Title Screen:

title screen

Game Screen:

screen during play

Game Over Screen:

game over screen

Complete code for the Super Mario game in Python using Pygame



Now that you have completed your game, you can test it to ensure that it works as expected. You can also use feedback from friends and family to improve the gameplay of your game. Once you have made the necessary changes to your game, you can publish it online for people to play.

Thank you for visiting our website.

Also Read:


Author: Ayush Purawr