Created
June 8, 2020 18:27
-
-
Save anotherjesse/5841099235b9d7c8658b2f82551100e3 to your computer and use it in GitHub Desktop.
microbit microjoust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| input.onButtonPressed(Button.A, function () { | |
| radio.sendValue("ready", control.deviceSerialNumber()) | |
| markReadyDevice(control.deviceSerialNumber()) | |
| }) | |
| input.onButtonPressed(Button.B, function () { | |
| radio.sendValue("ensure", control.deviceSerialNumber()) | |
| basic.showNumber(devices.length) | |
| }) | |
| radio.onReceivedString(function (receivedString) { | |
| if (receivedString == "restart") { | |
| startGame() | |
| } | |
| }) | |
| radio.onReceivedValue(function (name, value) { | |
| if (name == "ensure") { | |
| addDevice(value) | |
| basic.showNumber(devices.length) | |
| } else if (name == "dead") { | |
| markDeviceDead(value) | |
| } else if (name == "ready") { | |
| markReadyDevice(value) | |
| } | |
| }) | |
| function startGame () { | |
| playing = false | |
| deadDevices = [] | |
| readyDevices = [] | |
| basic.showLeds(` | |
| . . . . . | |
| . . . . . | |
| . . . . . | |
| . . . . . | |
| . . . . . | |
| `) | |
| for (let x = 0; x <= 4; x++) { | |
| for (let y = 0; y <= 4; y++) { | |
| led.plot(x, y) | |
| basic.pause(50) | |
| } | |
| } | |
| basic.showIcon(IconNames.Heart) | |
| playing = true | |
| } | |
| function addDevice (deviceId: number) { | |
| if (devices.indexOf(deviceId) < 0) { | |
| devices.push(deviceId) | |
| } | |
| } | |
| function markDeviceDead (deviceId: number) { | |
| if (deadDevices.indexOf(deviceId) < 0) { | |
| deadDevices.push(deviceId) | |
| } | |
| if (devices.length == deadDevices.length + 1) { | |
| playing = false | |
| basic.showIcon(IconNames.Happy) | |
| } | |
| } | |
| function markReadyDevice (deviceId: number) { | |
| if (readyDevices.indexOf(deviceId) < 0) { | |
| readyDevices.push(deviceId) | |
| } | |
| if (readyDevices.length == devices.length) { | |
| startGame() | |
| } | |
| } | |
| function doDie () { | |
| playing = false | |
| radio.sendValue("dead", control.deviceSerialNumber()) | |
| basic.showNumber(devices.length - deadDevices.length) | |
| } | |
| let deadDevices: number[] = [] | |
| let playing = false | |
| let devices: number[] = [] | |
| let readyDevices: number[] = [] | |
| let maxG = 2048 | |
| readyDevices = [] | |
| devices = [] | |
| devices.push(control.deviceSerialNumber()) | |
| radio.setGroup(42) | |
| basic.forever(function () { | |
| if (playing) { | |
| if (input.acceleration(Dimension.Strength) > maxG) { | |
| doDie() | |
| } | |
| } | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment