Commando Jump Game For The Micro:bit In C
Written by Mike James   
Thursday, 04 August 2016
Article Index
Commando Jump Game For The Micro:bit In C
Top Down Programming
Complete Listing

The Micro:bit is quite a capable computer but it does have one limitation - it only has a 5x5 LED matrix display. What sort of game could you possibly program on that!

In this project we resurrect a classic BBC Micro game on the BBC micro:bit. Games have always been a great way to get into programming so why should the micro:bit be any different.

In this article we implement the game in C, but if you want to see how this compares to the other standard languages on the micro:bit see the list of articles at the end.

C is perhaps the most difficult of the languages to get started with but it rewards the effort. There is no language on the micro:bit that works as fast and if you can't do the job in C you probably can't do it. It is a simple language and arguably a good language for teaching programming. It is close to the way the machine works and can be used to show alll sorts of programming ideas and techniques.

 

gamesbook

 

Back in the days of the BBC Micro I was involved the development of a game called Commando Jump that was included in the book 21 Games for the BBC Micro. It was, by design, a very simple game intended to teach BBC Basic programming. It presented the player with a wall that the commando had to climb. The initial jump height was determined by the player's reaction time. After that they could make the commando scramble up the wall by pressing a key as fast as they could. The faster, the higher the commando climbed. If you reached the top of the wall before that time out then at the next level there was a higher wall. If you didn't make it the commando would slide down with a deflated noise.  

commando

A 5x5 LED display is just enough to implement this game. Not in all its many-colored glory but we can reproduce some of the original game play - and the temptation of re-implementing a game from the BBC Micro days on the BBC Micro:bit days was too good to miss!

In the rest of this article it is assumed that you have a Micro:bit and know a little C but not that much. 

Getting Started With C

The online compiler for the micro:bit is the best way to get started. 

To use the online compiler you have to sign up with mbed.

Go to:

https://www.mbed.com/

and sign up for an account. At the time of writing, to find the online compiler a you have to go to the mbed Classic Developer site, but this could change. 

Once you have an account and have signed in, your first task is to get the compiler set up with the micro:bit as its target. You can either go to Platforms and select the vendor as BBC to see the one and only product in that category, i.e. the micro:bit

compiler1

Alternatively you can go to the compiler and select the target device icon in the top right, which initially shows as "No device Selected". Click the Add Platform button and you are back to the Platform selection page. Select the BBC micro:bit and you are back in the same place.

The micro:bit page contains a lot of information but if you just want to work with it in the compiler simply select the Add to your mbed Compiler at the right of the screen:

add

 

Following this when you go back to the compiler you will see the BBC micro:bit as the target platform in the top right hand corner:

 

bbccompiler

 

To get a starter project you have to select the samples template because  it is the one that uses the full micro:bit library - the others used a reduced mbed version.

 

new

 

It takes some time to build the project but eventually you will see the finished project ready for you to edit. 

The C/C++ programs are all in the source directory as usual but the difference is that you are using the microbit library rather than the usual mbed library. 

samplestructure

If you want to you can compile (ignore the compiler warnings) and run the sample without modification just to check everything works. If you select the compile button the program is compiled and then downloaded to your machine. You then have to download the file - microbit-samples_NRF51_MICROBIT.hex - the the micro:bit via the usb drive in the usual way. When you run the program you will discover that by default you will see a Hello World message scroll across the screen. 

At the moment the samples provide the best starting template for you own projects. Create a new project based on the samples template and call it commando

Next delete all of the source files in the Source directory. The quickest way to do this is to delete the Source directory and then add it back as a new directory. Create a new file called main.cpp - you can call it anything you like but the name main gives you the clue that it is the start of the program. 

 

The Serial Console

You don't have to do this step but if you are going to be developing using C then I strongly advise it. Without it you are going to have to do a lot more work to debug your programs. 

Having a console where you can see error messages and see debug output is essential for an efficient and stress free programming experience.

First you need to set up a USB serial connection to the Micro:bit. This is the default on Linux and the Mac but you have to install it under Windows.

To do this you need a driver. I suspect that any USB serial driver for the OS of you choice will do, but the official drivers for the Micro:bit can be downloaded from:

https://developer.mbed.org/handbook/Windows-serial-configuration

It should just install as it is a very standard USB driver. 

When you have the driver installed you need to discover what new serial port has been added to your system. Use the device manager to examine the serial ports. The one you want is called mbed Serial Port.

deviceman

In this case the port is COM3, but this isn't always the case. 

Next you need a serial console program to run on your PC. There are a lot of different possible choices, but I would advise using PuTTY because its versatile and works with serial and SSH connections.

Learning to use PuTTY is a skill worth the effort in acquiring.

Download and unzip PuTTY. Open it and enter the details of the serial connection you want to make.  

You need to enter the COM port number that corresponds to the mbed serial port and the speed of 115200 and the rest of the parameters can remain at their defaults. 

putty

When you run the console you will be able see error messages and any information you care to printf. For example:

printf("hello world\n\r")

will cause hello world, new line, to be displayed on the serial console.

printf("count=%d,count);

will display the current value of count. 

This really does make debugging easier. 



Last Updated ( Thursday, 04 August 2016 )