Commando Jump Game For The Micro:bit In C |
Written by Mike James | ||||
Thursday, 04 August 2016 | ||||
Page 3 of 3
The End GameWhile 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 ListingThe 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 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. More InformationRelated ArticlesGetting 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 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! Four Generations - Video of BBC Micro
Comments
or email your comment to: comments@i-programmer.info |
||||
Last Updated ( Friday, 19 August 2022 ) |