Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Created February 22, 2021 02: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 dustinknopoff/9dbc2a4a59c2fc926649e5bdc92b2271 to your computer and use it in GitHub Desktop.
Save dustinknopoff/9dbc2a4a59c2fc926649e5bdc92b2271 to your computer and use it in GitHub Desktop.
// music.setVolume(100)
enum PlaneState {
Idle, // No Sound
Liftoff, // playTone Note.D
Cruising, // playTone Note.CSharp
Left, // playTone Note.G
Right, // playTone Note.B
Turbulence, //music.playMelody("G B C C -- F F G", 300)
Landing, //playTone Note.C
GunA, // Note.GSharp5 @ 6 ms
GunB // Note.GSharp3 @ 6ms
}
/**
* void func producing sound based on current state
* Pure
* state: {PlaneState} the current state
*/
function stateToSound(state: PlaneState) {
switch (state) {
case PlaneState.Idle:
return
case PlaneState.Liftoff:
music.playTone(music.noteFrequency(Note.G), 500)
music.playTone(music.noteFrequency(Note.D), 500)
music.playTone(music.noteFrequency(Note.B), 500)
return
case PlaneState.Cruising:
return music.playTone(music.noteFrequency(Note.CSharp), 1000)
case PlaneState.Left:
return music.playTone(music.noteFrequency(Note.G), 1000)
case PlaneState.Right:
return music.playTone(music.noteFrequency(Note.B), 1000)
case PlaneState.Turbulence:
music.playTone(music.noteFrequency(Note.G), 500)
music.playTone(music.noteFrequency(Note.B), 500)
music.playTone(music.noteFrequency(Note.C), 500)
music.playTone(music.noteFrequency(Note.D), 500)
return
case PlaneState.GunA:
return music.playTone(music.noteFrequency(Note.GSharp5), 6)
case PlaneState.GunB:
return music.playTone(music.noteFrequency(Note.GSharp3), 6)
case PlaneState.Landing:
music.playTone(music.noteFrequency(Note.CSharp), 1000)
music.playTone(music.noteFrequency(Note.C), 1000)
return //music.playMelody("C# C", 120)
}
}
let currentReceived = PlaneState.Idle
let lastReceived = PlaneState.Idle
input.onButtonPressed(Button.A, function () {
currentReceived = PlaneState.GunA
})
input.onButtonPressed(Button.B, function () {
currentReceived = PlaneState.GunB
})
input.onGesture(Gesture.TiltLeft, function () {
currentReceived = PlaneState.Left
})
input.onGesture(Gesture.TiltRight, function () {
currentReceived = PlaneState.Right
})
input.onGesture(Gesture.Shake, function () {
currentReceived = PlaneState.Turbulence
})
input.onGesture(Gesture.FreeFall, function () {
currentReceived = PlaneState.Turbulence
})
input.onGesture(Gesture.LogoUp, function () {
currentReceived = PlaneState.Liftoff
})
input.onGesture(Gesture.LogoDown, function () {
currentReceived = PlaneState.Landing
})
let notPlaying = true;
music.onEvent(MusicEvent.MelodyEnded, function () {
notPlaying = true;
})
music.onEvent(MusicEvent.BackgroundMelodyEnded, function () {
notPlaying = true;
})
music.onEvent(MusicEvent.MelodyStarted, function () {
notPlaying = false;
})
music.onEvent(MusicEvent.BackgroundMelodyStarted, function () {
notPlaying = true;
})
basic.forever(function () {
if (currentReceived != lastReceived && notPlaying) {
stateToSound(currentReceived)
lastReceived = currentReceived
// basic.showNumber(currentReceived)
// basic.pause(100)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment