Microbit Zombies

Game MicrobitHard

There's a zombie virus that's infecting the microbits! It gets transmitted when you get too close to an infected microbit, watch out!!

In this game we have a health value that decreases if you're near an infected microbit. Once your health is gone you become a zombie and start infecting other microbits.

This is a multiplayer game so you will need multiple people with a microbit each (at least 3 people and microbits).

1

Create a new microbit project

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!

2

Create two variables

Create two variables:

  1. 'health' to remember our health value
  2. 'infected' to remember if we're infected or not

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

3

Set the radio group

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)

4

Display if you're infected or not

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)
    }
})

5

Lose 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).

6

Get infected

If your health gets down to 0 then you will become infected! Add an 'if then' block to the bottom of the 'forever' block. 

basic.forever(function () {
    if (infected) {
        basic.showIcon(IconNames.Skull)
        radio.sendString("zombie")
    } else {
        basic.showIcon(IconNames.Happy)
        basic.showNumber(health)
    }
    if (health <= 0) { // new code
        infected = true
    }
})
7

Randomly infect a microbit

At the start of the game everyone's health will be 9 and their infected variable will be equal to false. So we need to randomly infect one player to get the fun started!

We're going to pick a random number between 1 and 100. If that random number equals 1 then infect this player. This means that every time this code runs (and remember it's inside a 'forever' block) you have a 1 in a 100 chance that you'll be infected.

Add an 'if then' block at the bottom of the 'forever' block.

basic.forever(function () {
    if (infected) {
        basic.showIcon(IconNames.Skull)
        radio.sendString("zombie")
    } else {
        basic.showIcon(IconNames.Happy)
        basic.showNumber(health)
    }
    if (health <= 0) {
        infected = true
    }
    if (Math.randomRange(1, 100) == 1) {
        infected = true
        health = 0
    }
})

Do you like those odds?! As this same code will be running on everyone's microbit the odds come down on someone's random number being equal to 1.

  • 1 player: 100 divided by 1, divided by 1 = 1 in 100 chance someone will get infected
  • 2 players: 100 divided by 1, divided by 2 = 1 in 50 chance will get infected
  • 3 players: 100 divided by 1, divided by 3 = 1 in 33 chance will get infected
  • 5 players: 100 divided by 1, divided by 5 = 1 in 20 chance will get infected
  • 10 players: 100 divided by 1, divided by 10 = 1 in 10 chance will get infected
  • etc.

8

Play the game!

That's it you've created all the code that's needed. Now download the project to your computer and transfer the .HEX file onto your microbit.

When all the players are ready they should all turn on their microbits at the same time. When one of the microbits randomly becomes infected it will start infecting nearby microbits!

Depending on the number of players you may wish to choose a smaller or larger random number.

if (Math.randomRange(1, 200) == 1) {

}

Also you may need to tweak the radio signal until it's working at a distance that you're happy with.

if (radio.receivedPacket(RadioPacketProperty.SignalStrength) >= -40) {

}
End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.