Here is a video of the original Ghou.st game.
- Open the editor https://makecode.microbit.org/
- Connect the micro:bit to your computer via USB
- Download the .hex file to the "MICROBIT" drive
- Wait for the micro:bit's LED to stop blinking
All of the following blocks are required to make it work.
input.onButtonPressed(Button.A, function () {
playing = 1
basic.showIcon(IconNames.Ghost)
})
radio.onReceivedValueDeprecated(function (remoteId, remoteAlive) {
list[parseFloat(remoteId)] = remoteAlive
win = playing == 1 && alive == 1
friends = 0
for (let listAlive of list) {
win = win && listAlive != 1
friends = friends + listAlive
}
if (win) {
basic.showIcon(IconNames.Heart)
} else if (!(playing)) {
basic.showNumber(friends + 1)
}
})
input.onGesture(Gesture.Shake, function () {
if (playing) {
alive = 0
basic.showIcon(IconNames.No)
}
})
let friends = 0
let win = false
let list: number[] = []
let alive = 0
let playing = 0
radio.setGroup(1)
playing = 0
let n = 300
let id = "" + Math.randomRange(0, n)
alive = 1
for (let i = 0; i < n; i++) {
list.push(0)
}
basic.showString(id)
basic.forever(function () {
radio.sendValue(id, alive)
basic.pause(500)
})
- Use one micro:bit per player flashed with the above code.
- Reset your micro:bit to display the device ID and watch it start counting.
- Wait until all players see the same number which should equal the total number of players.
- Once everybody is ready, press the A button to become a ghoust, then try to shake other players' devices.
The code implements a simple, distributed algorithm, without a central coordinator :)
On start your device picks a random id between 0 and n (e.g. n = 300, n >> number of devices).
Forever, your device broadcasts (via Bluetooth radio) its id and alive status which is either 1 (alive) or 0 (not alive).
On radio, if a broadcast message was received, your device reads the remote device's id and alive status and updates its list.
On shake, the device knows immediately, that it is no longer alive. All others learn it eventually, via broadcast.
A device knows it won, if it's alive, and all other devices in its list are not alive.
Pierce a hole and cut along the label.
Cut off the bottom part of the bottle.
Then make a short, vertical cut as shown.
Here's a design to fit the micro:bit into Chaya bottles.
Connect the wires, align the adapter with the battery plug.
Loosely strap the micro:bit to the adapter with a zip tie.
Apply hot glue to both sides and place the battery.
Pierce small holes in the bottle cap to add a button and a buzzer.
Cut two crocodile clip wires in half and remove the insulation.
Solder the wires to the pins - take care not to melt the cap.
Put the wires through the bottle, and connect them to the micro:bit.
Make sure the wires are connected right*, run some test code to verify.
*The button connects to 3V (any leg) and Pin 2.
**The buzzer goes to GND (short leg) and Pin 1.
In addition to the code above, add the following block.
pins.onPulsed(DigitalPin.P2, PulseValue.High, function () {
if (playing == 0) {
playing = 1
basic.showIcon(IconNames.Ghost)
basic.pause(500) // debounce button
} else { // playing == 1
alive = 0
radio.sendValue(id, 0)
basic.pause(1000)
control.reset() // sets playing to 0
}
})
Add code for the buzzer, e.g. to indicate "game over".
// existing code ...
input.onGesture(Gesture.Shake, function () {
if (playing && alive) {
alive = 0
basic.showIcon(IconNames.No)
// new code for buzzer >>
pins.digitalWritePin(DigitalPin.P1, 1)
basic.pause(500)
pins.digitalWritePin(DigitalPin.P1, 0)
// << end of buzzer code
}
})
Switch it on, then squeeze the top to add the micro:bit and re-assemble the bottle.
That's it! You are ready to play.