We are gonna make a catching apple game. Game players control the cat to move forth and back to catch the falling apples while avoiding the yellow lightning.
- Press the button A and B on the micro: bit to control the cat move left and right; game over if the lightning hits the cat;
- Duplicate the sprite “Apple”, and the apples appear and fall from the upper of the screen constantly. If the apple touched the cat, it disappears and reappears in the upper-side of the screen;
- Duplicate the sprite “Apple”, revise the waiting time after it falls;
- Add the sprite “Lightning”, the falling effect is same to apple, but when it hits the cat, game over.
1. Create the main characters: cat, apple and lightning.
2. Program the cat: the cat has two shapes. When the cat moves left or right, change its shape to the related action gesture so as to make it more like a cat walking.
Press the button A and B on the micro:bit board to control the cat’s movement. Change the value of x axis and the movement direction of the cat. The cat will catch the apple as it walks, if the lightning hits the cat, stop all and game over. Figure 3-3 is the entire program.
Code Block Description:
The position of the apple：the value of y is unchanged, and
the value of x is random. Here we use the random function block. The block picks a pseudorandom number ranging from the first given number to the second, including both endpoints.
The state of apple: the value of y constantly minus 1 to realize the effect of falling.
Interrupt detecting: if the apple hits the cat, it will be hidden.
Control the apple to appear and disappear: the apple appears from the up-side and disappears when it arrives at the bottom.
The state when the apple is caught: when the apple hits the cat, it disappears which means that it was took by the cat.
Falling state control: since in the stage area, the value of Y ranges from 180 to -180, then the program needs run 360 times. The value of Y minus 1 every time.
We can directly duplicate the sprite apple and its codes. However, when there are 4 apples to be appearing on the stage, what will happen? Since only the value of x is random, that is to say, these apples will fall from high at a same time. To make the game look much more realistic, we need to set different initial falling time for the apple, how to do that? There are many acceptable ways. Here we decide to realize the effect by adding more “wait” blocks before “display”. And we can adopt random functions for the wait time. Just like this:
The rest apples’ program is shown in figure 3-5.
Duplicate the sprite and code orderly, as shown figure 3-6.
What’s the difference of the codes between the lightning and apple? The program of the lightning is shown below.
We can control the program through the buttons on the micro:bit now. Shown as below. Run the program, and check the result. Think what improvement we can make about this tutorial.
What if we make a two-player mode for the game as shown in figure3-9. One player uses the left and right key to control the sprite’s movement, another controls it by the buttons (A and B) on the micro:bit board, and then make a competition. Well, if so, what revises should be made based on this program?