Nintendo first launched this BMX racing game for the NES. From there, the company modernized Excitebike and released it on the N64 and, eventually, the Wii.

Not many NES consoles exist anymore, which makes playing Excitebike again hard to come by. Thanks to some retro fans at Wireframe Magazine, a snippet of Python code can recreate this NES classic for hours of fun on your Raspberry Pi or home PC.

Getting the Code and Setting Up

Although you can use nano or vim to edit your python code, you’ll want to take advantage of a full-featured text editor or IDE.

You’ll need to install Pygame Zero to support the features built within this homage to Excitebike. Pygame Zero contains Python modules for game creators to focus without having to repeat simple foundations.

If you don’t have python3 installed on your PC, you can download the latest version from Python directly. You’ll also want to make sure you have PIP installed too.

With the latest version of Python and PIP installed, open up your PowerShell or Linux terminal and type:

Lastly, you will need to pick up a copy of the Python code from Wireframe magazine’s GitHub repository. Type the following command into a terminal:

Alternatively, you can download the zipped-up code directly from the Wireframe GitHub page.

Breaking Down the Code

To understand what the code does, first run the Excitebike game. Do so by navigating to the source code and then running it using Pygame Zero:

You can then open the exitebike.py file with a text editor to view the code.

The draw function draws the background as the bike image moves. A “blit” refers to a block that is drawn on the screen. The word, “background,” refers to the background image (linking the images to the background block is thanks to the imported Pygame Zero modules).

You’ll also notice calculations for lap time and the last lap (this is represented by the time counter at the bottom of the screen). Lap time is constantly calculating the difference between the initial right arrow key press (start time) and the current time.

The last lap is a similar calculation. Instead of the start time, the time is calculated from when the motorbike passes a predefined track position (mentioned as trackPos < -4800 in the code). The below items in quotes represent an image.

This code draws image blocks on the screen at certain intervals and particular positions on the screen. The supplied crowd image is only 100px wide. Yet, the code, below, tells the computer when to draw the image so that it appears as if there is a simulated crowd behind the motorcycle as it moves.

The rock images are displayed on the screen in two parts. The second and third lines of code, below, work together to simulate the scenery in a tidy fashion.

Once you create a backup copy of the python code, try changing the below numbers to see what happens to the rocks on screen!

Do you want to replace your motorbike with a Monster truck, four-wheeler, or another vehicle? Maybe you want to race a unicorn to the finish line?

All you need to do is draw a 50x50px image (with a transparent background). Then, place this file (picture.png) into the ‘images’ folder (within the source-code-excitebike folder). You’ll want to create two images (one with a slightly increased height) to add some realism. All you need to do is change the reference to ‘bike0’ here:

Also make sure to perform a find and replace action with the name of the picture (without the image extension).

checkBikeRamp() is the function that does the work to calculate the Y axis so that your motorbike (or truck) goes up the ramp (and gets some serious air if you’re lucky). The keyboard.right and keyboard.left parameters are defined in the update() function.

These code bits tell the computer to slow the motorbike down or speed up (depending on what key the user presses on the keyboard). Translation: The faster your motorbike is moving, the higher the number of the Y axis (or very big jump).

Transversely, the on_key_down(key) function will send your motorbike into a nosedive.

The muckLane parameter will slow your motorbike down using the following calculation:

Line 66 of the code determines when to display the ‘muck’ image on screen. Now that the computer knows when your motorbike will pass over the muck, it will slow down your motorbike speed in half as you move over the muck on the track. Change the muckLane value from /50 to /25 or /75 to see what happens next.

Racers (ahem, Python Programmers)! Start Your Engines!

Even if you aren’t comfortable programming this homage to Excitebike from scratch, this code is still a great primer. By tweaking it, and realizing the visual impact of the changes, you can learn a lot. Will you try to add more features or AI racers in your game? Go for it!

With a little Python practice under your belt, another idea for you to try is adding different levels once you pass the finish line after 4 or 5 times. As well, you may want to add in rewards for first, second, and third place. If you are in first place, add a crowd-cheering audio file too! There are so many possibilities.

When you’re happy with your Excitebike enhancements, why not tackle creating a text adventure game? There are a lot of different pieces that will ensure you’re learning something exciting for hours, days, or even weeks!