Skip to content

Instantly share code, notes, and snippets.

@bennypowers
Last active June 14, 2023 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennypowers/9e0877743357f3a3efcdbd02505d9e96 to your computer and use it in GitHub Desktop.
Save bennypowers/9e0877743357f3a3efcdbd02505d9e96 to your computer and use it in GitHub Desktop.
function setScoreTo(num: number) {
serial.writeValue("score", num)
if (num >= 4) {
win()
} else if (num < 0) {
lose()
} else {
last = rounded
rope = num
rounded = Math.round(rope)
if (last != rounded) {
tone = list[rounded]
music.playSoundEffect(music.createSoundEffect(
WaveShape.Sine,
tone,
tone,
255,
255,
500,
SoundExpressionEffect.None,
InterpolationCurve.Linear
), SoundExpressionPlayMode.InBackground)
}
}
}
input.onButtonPressed(Button.A, function () {
if (!running && !won && !lost) {
radio.sendValue("game", 1)
start()
}
})
function win() {
running = false
won = true
radio.sendValue("game", 0)
basic.showIcon(IconNames.Happy)
}
function lose() {
lost = true
running = false
basic.showIcon(IconNames.Sad)
}
function draw() {
if (running) {
for (let index = 0; index <= 4; index++) {
if (index == rounded) {
led.unplot(2, index)
led.plot(1, index)
led.plot(3, index)
} else {
led.plot(2, index)
led.unplot(1, index)
led.unplot(3, index)
}
}
} else if (!won && !lost) {
basic.showNumber(channelChoice)
}
}
function send() {
setScoreTo(rope + 0.1)
radio.sendValue("rope", rope)
draw()
}
input.onButtonPressed(Button.B, function () {
if (!(running) && !won && !lost) {
if (channelChoice < 5) {
channelChoice += 1
} else {
channelChoice = 1
}
radio.setGroup(channelChoice)
draw()
}
})
input.onGesture(Gesture.Shake, function () {
if (running && !won && !lost) {
send()
}
})
radio.onReceivedValue(function (name, value) {
serial.writeLine(`received ${name}=${value}`)
if (name == "ping") {
radio.sendValue("pong", 1)
} else if (name == "game") {
if (value == 1) {
start()
} else if (value == 0) {
lose()
}
} else if (name == "rope") {
receive(value)
}
})
function start() {
for (let index2 = 0; index2 <= 2; index2++) {
music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, 1000, 1000, 255, 255, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), SoundExpressionPlayMode.InBackground)
basic.showNumber(3 - index2)
}
music.playSoundEffect(music.createSoundEffect(WaveShape.Sine, 2000, 2000, 255, 255, 500, SoundExpressionEffect.None, InterpolationCurve.Linear), SoundExpressionPlayMode.InBackground)
basic.showString("!")
running = true
draw()
}
function receive(score: number) {
if (running && !won && !lost) {
setScoreTo(4 - score)
draw()
}
}
let running = false
let won = false
let lost = false
let tone = 0
let last = 0
let list: number[] = []
let rounded = 0
let rope = 0
let channelChoice = 0
channelChoice = 1
rope = 2
rounded = 2
list = [
440,
494,
523,
587,
659
]
radio.setGroup(channelChoice)
draw()
radio.sendValue("ping", 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment