Commando Jump Game For The Micro:bit In JavaScript
Written by Mike James   
Tuesday, 16 February 2016
Article Index
Commando Jump Game For The Micro:bit In JavaScript
Top Down Programming
Complete Listing

Endgame handling

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. 

endGame

 

If position==0 then the commando is over the wall and we call an animation function to move the LED to the far left.   If the commando has or hasn't made it then the moveVertical function is used to drop the commando down from its current position to its final position.

The horizontal move is easy:

movehoz

All we need is a for loop that flashes the LED on and off from 0,4 to 0,0. 

The vertical move is almost as easy but we have to remember to drop the commando from what ever y position he is at back to y=4.

movevert

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

That's it - game over. 

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

 

jump

 

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

Rather than give a complete block form of the program, it seems more useful to give the JavaScript version. If you want to you can copy and paste this into the JavaScript editor and switch to block view to see a complete block representation.

The complete program is:

/* When the BBC micro:bit runs */
function onStart( ) {
 globals.position = 0;
 globals.press = 0;
 globals.x = 0;
 microbit.startGame();
 microbit.playGame();
 microbit.endGame();
}

function startGame( ) {
 microbit.countDown();
 microbit.drawPlayArea();
}


function playGame( ) {
 var t = microbit.runningTime;
 globals.x = 4;
 globals.position = 4;
 microbit.on(4, globals.position);
 while (( microbit.runningTime - t ) < 20000) {
  if (globals.press >= 10) {
    globals.press = 0;
    microbit.off(4, globals.position);
    globals.position = globals.position - 1;
    microbit.on(4, globals.position);
    if (globals.position == 0) {
      break;
    }
  }
 }
}


function endGame( ) {
 if (globals.position == 0) {
   microbit.moveHorizontal();
 }
 microbit.moveVertical();
}

function countDown( ) {
 for (var i = 5; i > 1; i = i - 1) {
  microbit.say(i);
 }
 microbit.say("!");
}
function drawPlayArea( ) {
 microbit.clear();
 for (var i = 1; i < 5; i = i + 1) {

   microbit.on(2, i);
 }
}

function onPressA( ) {
 globals.press = globals.press + 1;
}

function moveHorizontal( ) {
 for (var i = 4; i >= 0; i = i - 1) {
  wait(400);
  microbit.on(i, 0);
  wait(400);
  microbit.off(i, 0);
 }
 globals.x = 0;
}

function moveVertical( ) {
 for (var i = globals.position; i < 5; i = i + 1) {
  wait(400);
  microbit.on(globals.x, i);
  wait(400);
  microbit.off(globals.x, i);
 }
 microbit.on(globals.x, 4);
}

 

 

 

More Information

https://www.microbit.co.uk/

Related Articles

Commando Jump Game For The Micro:bit In Python

Micro:bit Commando Jump In The Microsoft Block Editor

Commando Jump Game For The Micro:bit In Touch Develop

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

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

BBC Micro:Bit Finally Ships to 1 Million For Free    

BBC micro:bit Your Next Computer?       

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 

 

square

 



 

Comments




or email your comment to: comments@i-programmer.info

 

 



Last Updated ( Saturday, 23 April 2016 )