Skip to content

Commit b3612ad

Browse files
committed
add konami code block
1 parent 5eb1224 commit b3612ad

6 files changed

Lines changed: 108 additions & 73 deletions

File tree

blocks/msg/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ Blockly.Msg.WEBDUINO_KEYBOARD_UP = "up";
168168
Blockly.Msg.WEBDUINO_KEYBOARD_DOWN = "down";
169169
Blockly.Msg.WEBDUINO_KEYBOARD_LEFT = "left";
170170
Blockly.Msg.WEBDUINO_KEYBOARD_RIGHT = "right";
171+
Blockly.Msg.WEBDUINO_KEYBOARD_KONAMI = "When input Konami Code, do:";
171172

172173
// Text logic
173174

blocks/msg/zh-hant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Blockly.Msg.WEBDUINO_KEYBOARD_UP = "上";
192192
Blockly.Msg.WEBDUINO_KEYBOARD_DOWN = "下";
193193
Blockly.Msg.WEBDUINO_KEYBOARD_LEFT = "左";
194194
Blockly.Msg.WEBDUINO_KEYBOARD_RIGHT = "右";
195+
Blockly.Msg.WEBDUINO_KEYBOARD_KONAMI = "當輸入 Konami Code,執行";
195196

196197
// Delay
197198

blocks/webduino.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3754,6 +3754,19 @@ Blockly.Blocks['document_keyboard'] = {
37543754
}
37553755
};
37563756

3757+
Blockly.Blocks['document_keyboard_konami'] = {
3758+
init: function () {
3759+
this.appendStatementInput("do_")
3760+
.appendField(Blockly.Msg.WEBDUINO_KEYBOARD_KONAMI, "當輸入 Konami Code,執行");
3761+
this.setInputsInline(true);
3762+
this.setPreviousStatement(true);
3763+
this.setNextStatement(true);
3764+
this.setTooltip('');
3765+
this.setColour(35);
3766+
this.setHelpUrl('https://webduino.io');
3767+
}
3768+
};
3769+
37573770
Blockly.Blocks['document_keyboard_stop'] = {
37583771
init: function () {
37593772
this.appendDummyInput()
@@ -3800,10 +3813,10 @@ Blockly.Blocks['document_keycode'] = {
38003813
["Z", "90"],
38013814
[Blockly.Msg.WEBDUINO_KEYBOARD_SPACE, "32"],
38023815
["enter", "13"],
3803-
[Blockly.Msg.WEBDUINO_KEYBOARD_UP, , "38"],
3804-
[Blockly.Msg.WEBDUINO_KEYBOARD_DOWN, , "40"],
3805-
[Blockly.Msg.WEBDUINO_KEYBOARD_LEFT, , "37"],
3806-
[Blockly.Msg.WEBDUINO_KEYBOARD_RIGHT, , "39"],
3816+
[Blockly.Msg.WEBDUINO_KEYBOARD_UP, "38"],
3817+
[Blockly.Msg.WEBDUINO_KEYBOARD_DOWN, "40"],
3818+
[Blockly.Msg.WEBDUINO_KEYBOARD_LEFT, "37"],
3819+
[Blockly.Msg.WEBDUINO_KEYBOARD_RIGHT, "39"],
38073820
["0", "48"],
38083821
["1", "49"],
38093822
["2", "50"],

generators/webduino.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ Blockly.JavaScript['mobile_deviceorientation_event'] = function (block) {
402402
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
403403
var dropdown_type_ = block.getFieldValue('type_');
404404
var code;
405-
if(dropdown_type_==1){
405+
if (dropdown_type_ == 1) {
406406
code = 'setDeviceOrientationListener(async function(alpha, beta, gamma){\n' +
407407
statements_do_ +
408408
'});\n';
409-
}else{
409+
} else {
410410
code = 'setDeviceMotionListener(async function(x, y, z){\n' +
411411
statements_do_ +
412412
'});\n';
@@ -443,9 +443,9 @@ Blockly.JavaScript['mobile_devicemotion_event_val'] = function (block) {
443443
Blockly.JavaScript['mobile_deviceorientation_event_remove'] = function (block) {
444444
var dropdown_type_ = block.getFieldValue('type_');
445445
var code;
446-
if(dropdown_type_==1){
446+
if (dropdown_type_ == 1) {
447447
code = 'removeDeviceOrientationListener();\n';
448-
}else{
448+
} else {
449449
code = 'removeDeviceMotionListener();\n';
450450
}
451451
return code;
@@ -2384,13 +2384,9 @@ Blockly.JavaScript['joystick_off'] = function (block) {
23842384
Blockly.JavaScript['document_keyboard'] = function (block) {
23852385
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
23862386
var dropdown_event_ = block.getFieldValue('event_');
2387-
var e = Blockly.JavaScript.variableDB_.getDistinctName(
2388-
'e', Blockly.Variables.NAME_TYPE);
2389-
var code = 'document.' + dropdown_event_ + ' = async function(' + e + '){\n' +
2390-
' console.log(' + e + '.keyCode);\n' +
2391-
' switch(' + e + '.keyCode){\n' +
2387+
var code = 'document.' + dropdown_event_ + ' = async function(e){\n' +
2388+
' console.log(e.keyCode);\n' +
23922389
statements_do_ +
2393-
' }\n' +
23942390
'};\n';
23952391
return code;
23962392
};
@@ -2406,12 +2402,19 @@ Blockly.JavaScript['document_keyboard_stop'] = function (block) {
24062402
Blockly.JavaScript['document_keycode'] = function (block) {
24072403
var dropdown_keycode_ = block.getFieldValue('keycode_');
24082404
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
2409-
var code = ' case ' + dropdown_keycode_ + ':\n' +
2405+
var code = 'if(e.keyCode == ' + dropdown_keycode_ + '){\n' +
24102406
' ' + statements_do_ +
2411-
' break;\n';
2407+
'}\n';
24122408
return code;
24132409
};
24142410

2411+
Blockly.JavaScript['document_keyboard_konami'] = function (block) {
2412+
var statements_do_ = Blockly.JavaScript.statementToCode(block, 'do_');
2413+
var code = 'konami(e.keyCode,async function(){\n' +
2414+
statements_do_ +
2415+
'});\n';
2416+
return code;
2417+
};
24152418

24162419
Blockly.JavaScript['text_indexof'] = function (block) {
24172420
var value_input_ = Blockly.JavaScript.valueToCode(block, 'input_', Blockly.JavaScript.ORDER_ATOMIC);

views/index.handlebars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@
475475
<category id="catKeyCode">
476476
<block type="document_keyboard"></block>
477477
<block type="document_keycode"></block>
478+
<block type="document_keyboard_konami"></block>
478479
<block type="document_keyboard_stop"></block>
479480
</category>
480481
<category id="catSpeech">

webduino-blockly.js

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
var orientationEventListener = function () {};
1515
var motionEventListener = function () {};
1616

17+
var konamiCode = "";
18+
1719
function boardReady(options, callback) {
1820
var board;
1921

@@ -90,6 +92,19 @@
9092
return candidate;
9193
}
9294

95+
function konami(c, callback) {
96+
var k = "38384040373937396665";
97+
konamiCode = konamiCode + c;
98+
if (konamiCode.length > k.length) {
99+
konamiCode = konamiCode.replace(konamiCode.slice(0, 2), "")
100+
}
101+
console.log(konamiCode);
102+
if (typeof callback === "function" && konamiCode == k) {
103+
callback();
104+
}
105+
}
106+
107+
93108
function setDeviceOrientationListener(listener) {
94109
removeDeviceOrientationListener();
95110

@@ -217,79 +232,79 @@
217232
return new webduino.module.Soil(board, analogpin);
218233
}
219234

220-
function toyCar(board,RF,RB,LF,LB){
235+
function toyCar(board, RF, RB, LF, LB) {
221236
this._rf = getPin(board, RF);
222237
this._rb = getPin(board, RB);
223238
this._lf = getPin(board, LF);
224239
this._lb = getPin(board, LB);
225240
}
226241

227-
toyCar.prototype.goFront = function(){
228-
var self = this;
229-
self._rf.write(1);
230-
self._rb.write(0);
231-
self._lf.write(1);
232-
self._lb.write(0);
242+
toyCar.prototype.goFront = function () {
243+
var self = this;
244+
self._rf.write(1);
245+
self._rb.write(0);
246+
self._lf.write(1);
247+
self._lb.write(0);
233248
};
234-
toyCar.prototype.goBack = function(){
235-
var self = this;
236-
self._rf.write(0);
237-
self._rb.write(1);
238-
self._lf.write(0);
239-
self._lb.write(1);
249+
toyCar.prototype.goBack = function () {
250+
var self = this;
251+
self._rf.write(0);
252+
self._rb.write(1);
253+
self._lf.write(0);
254+
self._lb.write(1);
240255
};
241-
toyCar.prototype.goRight = function(){
242-
var self = this;
243-
self._rf.write(1);
244-
self._rb.write(0);
245-
self._lf.write(0);
246-
self._lb.write(0);
256+
toyCar.prototype.goRight = function () {
257+
var self = this;
258+
self._rf.write(1);
259+
self._rb.write(0);
260+
self._lf.write(0);
261+
self._lb.write(0);
247262
};
248-
toyCar.prototype.goLeft = function(){
249-
var self = this;
250-
self._rf.write(0);
251-
self._rb.write(0);
252-
self._lf.write(1);
253-
self._lb.write(0);
263+
toyCar.prototype.goLeft = function () {
264+
var self = this;
265+
self._rf.write(0);
266+
self._rb.write(0);
267+
self._lf.write(1);
268+
self._lb.write(0);
254269
};
255-
toyCar.prototype.turnRight = function(){
256-
var self = this;
257-
self._rf.write(0);
258-
self._rb.write(1);
259-
self._lf.write(1);
260-
self._lb.write(0);
270+
toyCar.prototype.turnRight = function () {
271+
var self = this;
272+
self._rf.write(0);
273+
self._rb.write(1);
274+
self._lf.write(1);
275+
self._lb.write(0);
261276
};
262-
toyCar.prototype.turnLeft = function(){
263-
var self = this;
264-
self._rf.write(1);
265-
self._rb.write(0);
266-
self._lf.write(0);
267-
self._lb.write(1);
277+
toyCar.prototype.turnLeft = function () {
278+
var self = this;
279+
self._rf.write(1);
280+
self._rb.write(0);
281+
self._lf.write(0);
282+
self._lb.write(1);
268283
};
269-
toyCar.prototype.backLeft = function(){
270-
var self = this;
271-
self._rf.write(0);
272-
self._rb.write(0);
273-
self._lf.write(0);
274-
self._lb.write(1);
284+
toyCar.prototype.backLeft = function () {
285+
var self = this;
286+
self._rf.write(0);
287+
self._rb.write(0);
288+
self._lf.write(0);
289+
self._lb.write(1);
275290
};
276-
toyCar.prototype.backRight = function(){
277-
var self = this;
278-
self._rf.write(0);
279-
self._rb.write(1);
280-
self._lf.write(0);
281-
self._lb.write(0);
291+
toyCar.prototype.backRight = function () {
292+
var self = this;
293+
self._rf.write(0);
294+
self._rb.write(1);
295+
self._lf.write(0);
296+
self._lb.write(0);
282297
};
283-
toyCar.prototype.stop = function(){
284-
var self = this;
285-
self._rf.write(0);
286-
self._rb.write(0);
287-
self._lf.write(0);
288-
self._lb.write(0);
298+
toyCar.prototype.stop = function () {
299+
var self = this;
300+
self._rf.write(0);
301+
self._rb.write(0);
302+
self._lf.write(0);
303+
self._lb.write(0);
289304
};
290305

291-
function getToyCar(board,RF,RB,LF,LB) {
292-
return new toyCar(board,RF,RB,LF,LB);
306+
function getToyCar(board, RF, RB, LF, LB) {
307+
return new toyCar(board, RF, RB, LF, LB);
293308
}
294309

295310
function getCar(board, F, B, L, R) {
@@ -441,5 +456,6 @@
441456
scope.removeDeviceOrientationListener = removeDeviceOrientationListener;
442457
scope.setDeviceMotionListener = setDeviceMotionListener;
443458
scope.removeDeviceMotionListener = removeDeviceMotionListener;
459+
scope.konami = konami;
444460

445461
}));

0 commit comments

Comments
 (0)