In this game we will have a health value and an infected value. At the start of the game everyone's health will be set to 9 and infected will be false.
Randomly someone in the group will be infected (infected = true and health = 0). If you get too close to their microbit then your health will start going down. Once your health is 0 you will become one of the infected zombie microbits!
Go to the Makecode.com Microbit website using the link below and click on the 'New Project' button underneath the 'My Projects' heading.
Install the micro:bit app on your iPad or tablet.
Open the app, tap 'Create code' and then create a new project.
Create two variables:
Once you've created the variables, add the following code to set your 'health' to 9 and 'infected' to false:
let infected = false let health = 9
In the Variables toolbox, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
As this is a miltiplayer game, we will be using the radios in your microbits to broadcast to each other. To do this we need to make sure we're all on the same radio group.
Add the 'set radio group' block to your code:
let infected = false let health = 9 infected = false radio.setGroup(1)
Our microbits will show our current health value and whether we're infected or not.
If you are infected (infected = true) then show a skull icon and broadcast the word "zombie". Players that are close enough to receive this message will start getting infected!
If you are not infected (infected = false) then show a happy face icon and also show your current health value.
Add the following code into the 'forever' block:
basic.forever(function () { if (infected) { basic.showIcon(IconNames.Skull) radio.sendString("zombie") } else { basic.showIcon(IconNames.Happy) basic.showNumber(health) } })
Next we will program what happens when we receive the "zombie" message from an infected microbit. If you're too close to an infected microbit then you start losing health and we'll show a warning icon! Add the following code:
radio.onReceivedString(function (receivedString) { if (radio.receivedPacket(RadioPacketProperty.SignalStrength) > -60) { health += -1 basic.showIcon(IconNames.Angry) } })
We're using the signal strength of the received message to determine how close the other microbit is. If it's greater than or equal to -60 then we're too close!
'signal strength' the value ranges from -128 to -42 (-128 means a weak signal and -42 means a strong one).