Как написать марио на питоне

от admin

Make Super Mario Game using Python.

Hey! Fellow Python programmers. In this article, I’m going to provide you with a free source code of the Super Mario game using the python pygame module. You can use this source code to learn pygame module. Also, you can add this game to your portfolio or use it as a project in your school or college.

Installing Python On Your computer

First of all, you need to install python on your computer. For downloading python visit python.org. You should better download python version 3.7 or higher. While installing the python make sure you have tick marked “Add Python To PATH” otherwise you will get an error while installing any module or library in python.

install python, Mario using python

Add python to the path variable

If you want to check whether python is successfully installed on your computer or not. Just go to cmd or terminal whatever you have on your computer and type python. There you can see which version of python has been installed on your computer. If it does show the version of python like in the image below you could not have followed the steps properly. You should recheck the previous steps carefully.

Mario using python

The above image that is in my case, it’s showing the 3.9 version. Yours will be different as per the python version you have installed.

Before downloading the source code you have to know about pygame module. Which your computer requires to run the code.

Pygame

If you are not aware of what the pygame is? Basically, pygame is a free and open-source cross-platform library for the development of multimedia applications like video games using Python. It uses the Simple DirectMedia Layer library and several other popular libraries to abstract the most common functions, making writing these programs a more intuitive task.

How To install pygame?

First of all, you need to install python on your computer, and along with that install python-pip. Python uses pip to install packages.

In order to install pygame on your computer write the below code in your cmd or terminal.

Note: If you see an error like “pip isn’t recognized” . It means that you haven’t added the pip in your environment variable. Follow this youtube tutorial to resolve the error.

After successful installation of pygame module. All you need to do is just download the Super Mario python project source code.

Click here to download the zip file.

Note : If you are finding an issue with downloading. Make sure you have signed in with a google account in order to download from google drive or to open the link in google drive.

After downloading the zip file of the source code all you need to do is extract it and navigate to the next folder and after that, you have to run the main.py file.

Mario using python

Do have a look at How to make a 2d snake game, Flappy Bird game, and Fidget Spinner using python. Furthermore, Don’t forget to share this article with your programming buddy. Also, don’t hesitate to shower your love or give remarks in the comment section below.

Hacking Super Mario Bros. with Python

This weekend I was coming home from the meeting of the LSST Dark Energy Science Collaboration, and found myself with a few extra hours in the airport. I started passing the time by poking around on the imgur gallery, and saw a couple animated gifs based on one of my all-time favorite games, Super Mario Bros. It got me wondering: could I use matplotlib’s animation tools to create these sorts of gifs in Python? Over a few beers at an SFO bar, I started to try to figure it out. To spoil the punchline a bit, I managed to do it, and the result looks like this:

This animation was created entirely in Python and matplotlib, by scraping the image data directly from the Super Mario Bros. ROM. Below I’ll explain how I managed to do it.

Scraping the Pixel Data

Clearly, the first requirement for this pursuit is to get the pixel data used to construct the mario graphics. My first thought was to do something sophisticated like dictionary learning on a collection of screen-shots from the game to build up a library of thumbnails. That would be an interesting pursuit in itself, but it turns out it’s much more straightforward to directly scrape the graphics from the source.

It’s possible to find digital copies of most Nintendo Entertainment System (NES) games online. These are known as ROMs, and can be played using one of several NES emulators available for various operating systems. I’m not sure about the legality of these digital game copies, so I won’t provide a link to them here. But the internet being what it is, you can search Google for some variation of «Super Mario ROM» and pretty easily find a copy to download.

One interesting aspect of ROMs for the original NES is that they use raw byte-strings to store 2-bit (i.e. 4-color), 8×8 thumbnails from which all of the game’s graphics are built. The collection of these byte-strings are known as the «pattern table» for the game, and there is generally a separate pattern table for foreground and background images. In the case of NES games, there are 256 foreground and 256 background tiles, which can be extracted directly from the ROMs if you know where to look (incidentally, this is one of the things that made the NES an «8-bit» system. 2^8 = 256, so eight bits are required to specify any single tile from the table).

Extracting Raw Bits from a File

If you’re able to obtain a copy of the ROM, the first step to getting at the graphics is to extract the raw bit information. This can be done easily in Python using numpy.unpackbits and numpy.frombuffer or numpy.fromfile . Additionally, the ROMs are generally stored using zip compression. The uncompressed data can be extracted using Python’s built-in zipfile module. Combining all of this, we extract the raw file bits using a function like the following:

This function checks whether the file is compressed using zip, and extracts the raw bit information in the appropriate way.

Assembling the Pattern Tables

The thumbnails which contain the game’s graphics patterns are not at any set location within the file. The location is specified within the assembly code that comprises the program, but for our purposes it’s much simpler to just visualize the data and find it by-eye. To accomplish this, I wrote a Python script (download it here) based on the above data extraction code which uses matplotlib to interactively display the contents of the file. Each thumbnail is composed from 128 bits: two 64-bit chunks each representing an 8×8 image with one bit per pixel. Stacking the two results in two bits per pixel, which are able to represent four colors within each thumbnail. The first few hundred chunks are difficult to interpret by-eye. They appear similar to a 2D bar code: in this case the «bar code» represents pieces of the assembly code which store the Super Mario Bros. program.

Scrolling down toward the end of the file, however, we can quickly recognize the thumbnails which make up the game’s graphics:

This first pattern table contains all the foreground graphics for the game. Looking closely, the first few thumbnails are clearly recognizable as pieces of Mario’s head and body. Going on we see pieces of various enemies in the game, as well as the iconic mushrooms and fire-flowers.

The second pattern table contains all the background graphics for the game. Along with numbers and text, this contains the pieces which make up mario’s world: bricks, blocks, clouds, bushes, and coins. Though all of the above tiles are shown in grayscale, we can add color by simply changing the matplotlib Colormap, as we’ll see below.

Combining Thumbnails and Adding Color

Examining the pattern tables above, we can see that big Mario is made up of eight pattern tiles stitched together, while small Mario is made up of four. With a bit of trial and error, we can create each of the full frames and add color to make them look more authentic. Below are all of the frames used to animate Mario’s motion throughout the game:

Similarly, we can use the thumbnails to construct some of the other familiar graphics from the game, including the goombas, koopa troopas, beetle baileys, mushrooms, fire flowers, and more.

The Python code to extract, assemble, and plot these images can be downloaded here.

Animating Mario

With all of this in place, creating an animation of Mario is relatively easy. Using matplotlib’s animation tools (described in a previous post), all it takes is to decide on the content of each frame, and stitch the frames together using matplotlib’s animation toolkit. Putting together big Mario with some scenery and a few of his friends, we can create a cleanly looping animated gif.

Читать:
Что такое входные данные

The code used to generate this animation is shown below. We use the same NESGraphics class used to draw the frames above, and stitch them together with a custom class that streamlines the building-up of the frames. By uncommenting the line near the bottom, the result will be saved as an animated GIF using the ImageMagick animation writer that I recently contributed to matplotlib. The ImageMatick plugin has not yet made it into a released matplotlib version, so using the save command below will require installing the development version of matplotlib, available for download on github.

«Mario Animation» animate_mario.py download

The result looks like this:

Pretty good! With a bit more work, it would be relatively straightforward to use the above code to do some more sophisticated animations: perhaps recreate a full level from the original Super Mario Bros, or even design your own custom level. You might think about taking the extra step and trying to make Mario’s movements interactive. This could be a lot of fun, but probably very difficult to do well within matplotlib. For tackling an interactive mario in Python, another framework such as Tkinter or pygame might be a better choice.

Name already in use

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Super Mario Implementation in Python

This is inspired by Meth-Meth-Method’s super mario game

  • $ pip install -r requirements.txt
  • $ python main.py

Standalone windows build

  • $ pip install py2exe
  • $ python compile.py py2exe
  • Left: Move left
  • Right: Move right
  • Space: Jump
  • Shift: Boost
  • Left/Right Mouseclick: secret

Alt text

  • pygame
  • scipy

If you have any Improvements/Ideas/Refactors feel free to contact me or make a Pull Request. The code needs still alot of refactoring as it is right now, so I appreciate any kind of Contribution.

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

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.

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.

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:

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 – 2

Level – 3

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:

Game Screen:

screen during play

Game Over Screen:

Complete code for the Super Mario game in Python using Pygame

Output:

Conclusion

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.

Похожие статьи