`
);
context = c.getContext("2d");
bird.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAOCAYAAADABlfOAAAA/0lEQVQ4y72TQWrDMBBF38gCqwtvC120YOgFegL3DrlFeppSanqc5iAlPUCgCy+kQOLJJkrkWE4TKBXMRuK/+fojwX8t773b1wzQXHnvn6b0ktt0ggIEe0v9+jU4W84rVBURIYRgnHN6qrcZpt69dxRs2J4cRyAQwX3OnElh+2I5r36NSERQ1dhEc041OkhFFCX12+rqmcgUNIXXbZeNYGAgicBe66JuuwgZzSKC7bGZDNwerp+4jE77RZltaJq1AqOWB+pj+5Odfr8oMc36bJx2KuOc4v4jYJoK/RynJs+b848foEjAD0kEaizfLzdjwfbIkwvno5f8xNzj/7O1A51fZ+BxD7C5AAAAAElFTkSuQmCC";
//c.onclick = () => (birdDY = 9) ;
intervalID = setInterval(updateBird, interval)
}
function setBirdY(val){ // 0-1000. scaled to 0-400
birdY = 400 - val/2.5;
blit();
}
function stopGame(){
if(intervalID != null)clearInterval(intervalID);
}
//---------GAME
Blockly.Blocks['add_piano'] = {
init: function() {
this.appendDummyInput()
.appendField("Add Piano")
this.setColour(135);
this.setTooltip("Add a Piano");
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setHelpUrl("");
}
};
Blockly.JavaScript['add_piano'] = function(block) {
var code = 'addPiano();\nsleep(0.1);\n';
return code;
};
Blockly.Python['add_piano'] = function(block) {
var code = 'addPiano()\n';
return code;
};
Blockly.Blocks['play_piano'] = {
init: function() {
this.appendValueInput("NOTE")
.setAlign(Blockly.ALIGN_LEFT)
.appendField("Play Piano:(A,A#, B, C...")
this.setInputsInline(false);
this.setPreviousStatement(true, null);
this.setNextStatement(true, null);
this.setColour(230);
this.setTooltip("Play Piano Note");
this.setHelpUrl("");
}
};
Blockly.JavaScript['play_piano'] = function(block) {
var alt = Blockly.JavaScript.valueToCode(block, 'NOTE', Blockly.JavaScript.ORDER_NONE);
var code = 'playPiano(' + alt + ');\n';
return code;
};
Blockly.Python['play_piano'] = function(block) {
var alt = Blockly.Python.valueToCode(block, 'NOTE', Blockly.Python.ORDER_NONE);
var code = 'playPiano(' + alt + ')\n';
return code;
};
var note, hints;
var audios = {};
var mykeys = {};
var addPiano = function(){
registers.append(`
AC
WC#
SD
ED#
DE
FF
TF#
GG
YG#
HA
UA#
JB
K2C
O2C#
L2D
P2D#
;2E
`
);
const keys = document.querySelectorAll(".key");
note = document.querySelector(".nowplaying");
keys.forEach(key => key.addEventListener("transitionend", removeTransition));
keys.forEach(key => key.addEventListener("touchstart", clicked));
for(const x of ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B','2C','2C#','2D','2D#','2E' ]){
audios[x] = document.querySelector(`audio[data-note="${x}"]`);
mykeys[x] = document.querySelector(`.key[data-note="${x}"]`);
}
window.addEventListener("keydown", playNote);
}
function clicked(e) {
playPiano(this.getAttribute("data-note"));
}
function playPiano(notename) {
const audio = audios[notename]; // document.querySelector(`audio[data-note="${notename}"]`),
key = mykeys[notename];//document.querySelector(`.key[data-note="${notename}"]`);
if (!key) return;
key.classList.add("playing");
note.innerHTML = notename;
audio.currentTime = 0;
audio.play();
}
function playNote(e) {
key = document.querySelector(`.key[data-key="${e.keyCode}"]`);
if (!key) return;
const keyNote = key.getAttribute("data-note");
playPiano(keyNote);
}
function removeTransition(e) {
if (e.propertyName !== "transform") return;
this.classList.remove("playing");
}
//-------------------- API ------------------------
function initGames(interpreter, scope) {
// Add an API for the Add Game call
interpreter.setProperty(scope, 'addGame', interpreter.createNativeFunction(
function() {
return addGame();
})
);
// Add an API for the Add Game call
interpreter.setProperty(scope, 'stopGame', interpreter.createNativeFunction(
function() {
return stopGame();
})
);
// Add an API for the Add Game call
interpreter.setProperty(scope, 'setBirdY', interpreter.createNativeFunction(
function(y) {
return setBirdY(y);
})
);
// Add an API for the Add Piano call
interpreter.setProperty(scope, 'addPiano', interpreter.createNativeFunction(
function() {
return addPiano();
})
);
// Add an API for the Play Music call
interpreter.setProperty(scope, 'playPiano', interpreter.createNativeFunction(
function(mynote) {
return playPiano(mynote);
})
);
}