Commando Jump Game For The Micro:bit In Python
Written by Mike James
Friday, 15 April 2016
Article Index
Commando Jump Game For The Micro:bit In Python
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.

`def endGame(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:

`def moveHorizontal():  for x in range(4,0,-1):    for fraction in range(0,10):       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:

`def moveVertical(x,h):  if h!=0:    display.set_pixel(x,h-1,0)  for y in range(h,4):    for fraction in range(0,10):      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:

`from microbit import *``def startGame():  countDown()  drawPlayArea()``def flashMan(x,y,p,vx,vy):  display.set_pixel(x,y,0)  display.set_pixel(x+vx,y+vy,0)  sleep(100)  display.set_pixel(x,y,9-p)  display.set_pixel(x+vx,y+vy,p)  sleep(100)``def moveHorizontal():  for x in range(4,0,-1):    for fraction in range(0,10):      flashMan(x,0,fraction,-1,0) def moveVertical(x,h):  if h!=0:    display.set_pixel(x,h-1,0)  for y in range(h,4):    for fraction in range(0,10):       flashMan(x,y,fraction,0,1)def playGame():  position=4  button_a.reset_presses()  t1=running_time()  press=0  while(running_time()-t1<20000):    flashMan(4,position,press,0,-1)    press=button_a.get_presses()    if press>=10:      position=position-1      button_a.reset_presses()      press=0      if position==0:        break  return position``def endGame(position):  if position==0:    moveHorizontal()     moveVertical(0,0)  else:    moveVertical(4,position) def countDown():  for t in range(5,0,-1):    display.scroll(str(t))    display.scroll("!") def drawPlayArea():  display.clear()  for y in range(1,5):    display.set_pixel(2,y,9)``startGame()position=playGame()endGame(position)`

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

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

#### Related Articles

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

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

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 