Giter Site home page Giter Site logo

microbit-ghoust's Introduction

Ghou.st for the micro:bit

Here is a video of the original Ghou.st game.

The original Ghou.st game

Get started

  • 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

Blocks

All of the following blocks are required to make it work.

On start

Forever

On radio

On button

On shake

Code

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

How to play

  • 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.

How it works

The code implements a simple, distributed algorithm, without a central coordinator :)

Getting a "unique" device ID

On start your device picks a random id between 0 and n (e.g. n = 300, n >> number of devices).

Broadcasting your ID and status

Forever, your device broadcasts (via Bluetooth radio) its id and alive status which is either 1 (alive) or 0 (not alive).

Keeping track of other devices

On radio, if a broadcast message was received, your device reads the remote device's id and alive status and updates its list.

Detecting that you're out

On shake, the device knows immediately, that it is no longer alive. All others learn it eventually, via broadcast.

Detecting that you win

A device knows it won, if it's alive, and all other devices in its list are not alive.

Make an enclosure

Cut a PET bottle

Pierce a hole and cut along the label.

Cut off the bottom part of the bottle.

Then make a short, vertical cut as shown.

Use a laser-cut adapter

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.

   

Add a button and a buzzer

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.

Insert and connect the wires

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.

Add button code to the game

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 buzzer code to the game

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 on the micro:bit and start playing

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.

microbit-ghoust's People

Contributors

tamberg avatar sacovo avatar

Stargazers

Tobias M. Schifferle avatar

Watchers

 avatar

Forkers

sacovo

microbit-ghoust's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.