|Commando Jump Game For The Micro:bit In Python|
|Written by Mike James|
|Friday, 15 April 2016|
Page 3 of 3
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.
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:
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:
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 complete program is:
If you would like to see how this is implemented in the other languages supported by the Micro:bit see the other three alternatives:
or email your comment to: firstname.lastname@example.org
|Last Updated ( Saturday, 23 April 2016 )|