This is going to be a game played between two micro:bits.
- Each player has a ship displayed on the LED matrix. A ship can be a single dot/LED or several LEDs.
- One LED in the ship is “the head” of the ship. When the “head” takes a hit, the game ends.
- Each player can select the location of the “missile” they are sending to the enemy. X coordinate is selected by pressing button A and Y coordinate is selected by pressing button B.
- The communication between them is via radio.
Step 1 - Display the ship
The “head” of the ship is the point of (3, 2) coordinates. Once this point takes a hit, the player loses the game.
The ship can be a also a single dot/LED. The advantage of this approach is that the ship can be randomly positioned to a location on the display in the beginning of the game.
Step 2 - Setting the missile’s coordinates
Pressing button A will select one value between 0 and 4 - this will be x coordinate of the missile.
Pressing button B will select one value between 0 and 4 - this will be y coordinate of the missile.
Step 3 - Sending the missile
I am going to combine the two values (of missile_x and missile_y) like so: 10 * missile_x + missile_y.
For example: if missile_x is 3 and missile_y is 2, then the number we are sending is: 10 * 3 + 2 = 32.
Step 4 - receive the enemy missile
When the number is received, we recover the coordinates and then check if they represent a “kill”.
The way to recover the coordinates is this:
- missile_y is the remainder of division between the receivedNumber and 10;
- missile_x is (receivedNumber - missile_y) / 10
For example:if receivedNumber = 41 then:
- missile_y = remainder of (41 / 10) = 1;
- missile_x = (41 - 1)/10 = 4.
If the coordinates are the same with the “head” of the battleship, the variable “iLost” is set to True (the variable was set to False in “on start”).
Step 5 - game ending
In case we lose, we can send a signal that we lost. This way, the other player will know they won.
The way to do this is by sending a number (that cannot be coordinates) when we take a fatal hit. This number needs to be above 44.
The forever loop looks like this:
The on radio received looks like this:
Ways to improve the game:
- develop a way to make sure that each player takes turns when shooting;
- restart the game without resetting and keep a score.