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 End Game

While the playGame function is complicated the end game handling can be as complicated as you like. You could add a scoreboard, a best score, a play again, etc.

For simplicity all we are going to do in the endGame function is to animate a final sequence depending on whether or not the commando got over the wall.

If the commando did get to the top of the wall within the time limit then an animation that moves the block horizontally and then down the "screen" is performed.

If it didn't make it, then the commando block simply falls back down.

`void endGame(int position) { if (position == 0) {   moveHorizontal();   moveVertical(0, 0); } else {   moveVertical(4, position); }}`

If position==0 then the commando is over the wall and we call an animation function to move the LED to the far left and then another animation function to move the commando down the edge of the array from 0,0.  If the commando hasn't made it then the same moveVertical function is used to drop the commando down from its final position.

The horizontal move is easy:

`void moveHorizontal() { int x, fraction; for (x = 4; x >= 0; x--) {  for (fraction = 0; fraction <= 10; fraction++) {   flashMan(x, 0, fraction, -1, 0);  } }}`

Notice that now we don't have the button presses to control the movement of brightness between the two LEDs so we have to program it ourselves. Also notice that now the direction of movement is -1,0, i.e. to the left and horizontally.

The vertical move is more difficult because there are a number of different positions the commando could start from, but it is essentially the same:

`void moveVertical(int x, int h) { int y, fraction; if (h != 0)    uBit.display.image.setPixelValue(x, h - 1, 0); for (y = h; y <= 4; y++) {``  for (fraction = 0; fraction <= 10; fraction++) {``   flashMan(x, y, fraction, 0, 1);  } }}`

The commando starts at x,h and moves down to x,4.

What is the "h not equal to zero" all about?

Because the commando changes direction from up to down, one LED will be left behind if you don't explicitly turn it off. I have to admit that I didn't get this right first time and I had to fix it after I noticed that an LED was left on.

That's it - game over.

You can now play commando jump on the BBC micro:bit.

What could you do next?

Add some sound as the commando struggles to get up the wall and falls back down or gets over.

Alter the difficulty level if the player wins and start the game again.

The Listing

The complete program is:

`#include "MicroBit.h"MicroBit uBit;``int clickCount = 0;``void startGame();int playGame();void endGame(int position);``void countDown();void drawPlayArea();void flashMan(int x, int y, int p, int vx, int vy);void setUpButton();void moveHorizontal();void moveVertical(int x, int h);``void buttonClickCount(MicroBitEvent e);``int main() { uBit.init();`` startGame(); int position = playGame(); endGame(position);`` release_fiber(); return 0;}``void startGame() { countDown(); uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE); drawPlayArea(); setUpButton();}``void countDown() {``int t; for (t = 5; t >= 0; t--) {  uBit.display.scroll(t); } uBit.display.scroll("!");}``void drawPlayArea() { uBit.display.clear(); int y; for (y = 1; y <= 5; y++) {  uBit.display.image.setPixelValue(2, y, 255); }``}``int playGame() { int position = 4; clickCount = 0; int t1 = uBit.systemTime();  while (uBit.systemTime() - t1 < 20000) {``  flashMan(4,  position, clickCount, 0, -1);  if (clickCount > 10) {   position = position - 1;   clickCount = 0;   if (position == 0)break;  } }`` return position;``}``void endGame(int position) { if (position == 0) {  moveHorizontal();  moveVertical(0, 0); } else {  moveVertical(4, position); }}``void setUpButton() { uBit.buttonB.setEventConfiguration(                 MICROBIT_BUTTON_ALL_EVENTS); uBit.messageBus.listen(MICROBIT_ID_BUTTON_B,       MICROBIT_BUTTON_EVT_CLICK, buttonClickCount);}``void buttonClickCount(MicroBitEvent e) { clickCount++;``}``void flashMan(int x, int y, int p, int vx, int vy) { uBit.display.image.setPixelValue(x, y, 0); uBit.display.image.setPixelValue(x + vx, y + vy, 0); uBit.sleep(100); uBit.display.image.setPixelValue(x, y, 25 * (10 - p)); uBit.display.image.setPixelValue(x+vx, y+vy, 25*p); uBit.sleep(100);``}``void moveHorizontal() { int x, fraction; for (x = 4; x >= 0; x--) {  for (fraction = 0; fraction <= 10; fraction++) {   flashMan(x, 0, fraction, -1, 0);  } }}``void moveVertical(int x, int h) { int y, fraction; if (h != 0)   uBit.display.image.setPixelValue(x, h - 1, 0); for (y = h; y <= 4; y++) {``  for (fraction = 0; fraction <= 10; fraction++) {``   flashMan(x, y, fraction, 0, 1);  } }}`

If you would like to see how this is implemented in the other languages supported by the Micro:bit see the other alternatives:

Commando Jump Game For The Micro:bit In Python

Commando Jump Game For The Micro:bit In JavaScript

Micro:bit Commando Jump In The Microsoft Block Editor

Commando Jump Game For The Micro:bit In Touch Develop

You might also like Harry Fairhead's latest book:

which you can read over on our sister site IoT programmer.

Related Articles

Getting Started With C/C++ On The Micro:bit

Offline C/C++ Development With The Micro:bit

BBC Micro:bit Now On Sale and Shipping

BBC Giving Away 1 Million Microcomputers

Teach Code In School - Before It's Too Late!

UK Micros of the 1980s

Four Generations - Video of BBC Micro