进步的konami代码 [英] progressive konami code
问题描述
我正在尝试为网站创建一个.js文件,一旦输入konami代码Up,Up,Down,Down,Left,Right,Left,Right,B,A,Start(enter)它将嵌入一个视频。
但是,当输入正确的键时,网页应显示继续的内容,如果输入了错误的键,则应显示错误,再试一次,并允许它们重新开始。
I am trying to create a .js file for a website that upon entering the konami code Up, Up, Down, Down, Left, Right, Left, Right, B, A, Start(enter) it will embed a video. However while entering the right keys the webpage should display something like "keep going", if a wrong key is entered it should display "wrong, try again", and allow them to start over.
我已经设法让JavaScript工作,在输入正确的代码时会显示警告,输入错误的代码会显示不同的代码。
I've manged to get the JavaScript working where upon entering the right code it displays an alert, and entering the wrong code displays a different code.
我已经使用在线资源来获取这么多代码,但没有一个解释如何出错,请再试一次
i've manged to get this much code using online resources but none of them explain how to get wrong, try again part
if (window.addEventListener) {
var keys = [],
konami = "38,38,40,40,37,39,37,39,66,65,13";
window.addEventListener("keydown", function(e){
keys.push(e.keyCode);
if (keys.toString().indexOf(konami) >= 0)
{
alert('Right');
keys = [];
};
if (keys.toString().indexOf(konami) < 0)
{
alert('Wrong');
keys = [];
}
}, true);
};
任何帮助都将是非常感谢。
Any help would be greatly appreciated.
推荐答案
if (window.addEventListener) {
var index = 0;
var konami = [38,38,40,40,37,39,37,39,66,65,13];
window.addEventListener("keydown", function(e){
if (e.keyCode === konami[index])
{
index++; //valid key at the valid point
if (index == konami.length)
{
alert("Correct");
} else {
alert("Keep going");
}
} else {
// incorrect code restart
index = 0;
alert("Wrong");
}
});
}
这篇关于进步的konami代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!