Skip to content

Commit 855f1ed

Browse files
committed
Fixed bug in move queue
1 parent edf90da commit 855f1ed

File tree

1 file changed

+62
-39
lines changed

1 file changed

+62
-39
lines changed

js/snake.js

+62-39
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ SNAKE.Snake = SNAKE.Snake || (function() {
192192
2
193193
*/
194194
me.handleArrowKeys = function(keyNum) {
195+
if (isDead) {return;}
195196

196197
var snakeLength = me.snakeLength;
197198
var lastMove = moveQueue[0] || currentDirection;
@@ -306,12 +307,20 @@ SNAKE.Snake = SNAKE.Snake || (function() {
306307
me.snakeHead.elm.style.zIndex = getNextHighestZIndex(me.snakeBody);
307308
me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-alive\b/,'')
308309
me.snakeHead.elm.className += " snake-snakebody-dead";
309-
//me.snakeHead.elm.style.backgroundImage = "url('./deadblock.png')";
310+
310311
isDead = true;
311312
playingBoard.handleDeath();
312-
moveQueue = [];
313+
moveQueue.length = 0;
313314
};
314315

316+
/**
317+
* This method sets a flag that lets the snake be alive again.
318+
* @method rebirth
319+
*/
320+
me.rebirth = function() {
321+
isDead = false;
322+
};
323+
315324
/**
316325
* This method reset the snake so it is ready for a new game.
317326
* @method reset
@@ -339,13 +348,11 @@ SNAKE.Snake = SNAKE.Snake || (function() {
339348
blocks[ii].elm.style.top = "-1000px";
340349
blocks[ii].elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'')
341350
blocks[ii].elm.className += " snake-snakebody-alive";
342-
//blocks[ii].elm.style.backgroundImage = "url('./snakeblock.png')";
343351
}
344352

345353
blockPool.concat(blocks);
346354
me.snakeHead.elm.className = me.snakeHead.elm.className.replace(/\bsnake-snakebody-dead\b/,'')
347355
me.snakeHead.elm.className += " snake-snakebody-alive";
348-
//me.snakeHead.elm.style.backgroundImage = "url('./snakeblock.png')";
349356
me.snakeHead.row = config.startRow || 1;
350357
me.snakeHead.col = config.startCol || 1;
351358
me.snakeHead.xPos = me.snakeHead.row * playingBoard.getBlockWidth();
@@ -570,7 +577,7 @@ SNAKE.Board = SNAKE.Board || (function() {
570577

571578
elmAboutPanel = document.createElement("div");
572579
elmAboutPanel.className = "snake-panel-component";
573-
elmAboutPanel.innerHTML = "<a href='http://patorjk.com/' class='snake-link'>patorjk.com</a> - <a href='http://patorjk.com/blog/software/' class='snake-link'>more online apps from patorjk.com</a> - <a href='http://patorjk.com/blog/about/#contact' class='snake-link'>contact</a>";
580+
elmAboutPanel.innerHTML = "<a href='http://patorjk.com/blog/software/' class='snake-link'>more patorjk.com apps</a> - <a href='https://github.com/patorjk/JavaScript-Snake' class='snake-link'>source code</a>";
574581

575582
elmLengthPanel = document.createElement("div");
576583
elmLengthPanel.className = "snake-panel-component";
@@ -579,6 +586,14 @@ SNAKE.Board = SNAKE.Board || (function() {
579586
elmWelcome = createWelcomeElement();
580587
elmTryAgain = createTryAgainElement();
581588

589+
SNAKE.addEventListener( elmContainer, "keyup", function(evt) {
590+
if (!evt) var evt = window.event;
591+
evt.cancelBubble = true;
592+
if (evt.stopPropagation) {evt.stopPropagation();}
593+
if (evt.preventDefault) {evt.preventDefault();}
594+
return false;
595+
}, false);
596+
582597
elmContainer.className = "snake-game-container";
583598

584599
elmContainer.appendChild(elmPlayingField);
@@ -612,11 +627,24 @@ SNAKE.Board = SNAKE.Board || (function() {
612627
welcomeTxt.innerHTML = "JavaScript Snake<p></p>Use the <strong>arrow keys</strong> on your keyboard to play the game. " + fullScreenText + "<p></p>";
613628
var welcomeStart = document.createElement("button");
614629
welcomeStart.appendChild( document.createTextNode("Play Game"));
615-
SNAKE.addEventListener(welcomeStart, "click", function() {
630+
631+
var loadGame = function() {
632+
SNAKE.removeEventListener(window, "keyup", kbShortcut, false);
616633
tmpElm.style.display = "none";
617634
me.setBoardState(1);
618635
me.getBoardContainer().focus();
619-
}, false);
636+
};
637+
638+
var kbShortcut = function(evt) {
639+
if (!evt) var evt = window.event;
640+
var keyNum = (evt.which) ? evt.which : evt.keyCode;
641+
if (keyNum === 32 || keyNum === 13) {
642+
loadGame();
643+
}
644+
};
645+
SNAKE.addEventListener(window, "keyup", kbShortcut, false);
646+
SNAKE.addEventListener(welcomeStart, "click", loadGame, false);
647+
620648
tmpElm.appendChild(welcomeTxt);
621649
tmpElm.appendChild(welcomeStart);
622650
return tmpElm;
@@ -631,12 +659,25 @@ SNAKE.Board = SNAKE.Board || (function() {
631659
tryAgainTxt.innerHTML = "JavaScript Snake<p></p>You died :(.<p></p>";
632660
var tryAgainStart = document.createElement("button");
633661
tryAgainStart.appendChild( document.createTextNode("Play Again?"));
634-
SNAKE.addEventListener(tryAgainStart, "click", function() {
662+
663+
var reloadGame = function() {
635664
tmpElm.style.display = "none";
636665
me.resetBoard();
637666
me.setBoardState(1);
638667
me.getBoardContainer().focus();
639-
}, false);
668+
};
669+
670+
var kbTryAgainShortcut = function(evt) {
671+
if (boardState !== 0 || tmpElm.style.display !== "block") {return;}
672+
if (!evt) var evt = window.event;
673+
var keyNum = (evt.which) ? evt.which : evt.keyCode;
674+
if (keyNum === 32 || keyNum === 13) {
675+
reloadGame();
676+
}
677+
};
678+
SNAKE.addEventListener(window, "keyup", kbTryAgainShortcut, true);
679+
680+
SNAKE.addEventListener(tryAgainStart, "click", reloadGame, false);
640681
tmpElm.appendChild(tryAgainTxt);
641682
tmpElm.appendChild(tryAgainStart);
642683
return tmpElm;
@@ -738,7 +779,6 @@ SNAKE.Board = SNAKE.Board || (function() {
738779
cHeight = getClientHeight()-5;
739780
document.body.style.backgroundColor = "#FC5454";
740781
} else {
741-
//elmContainer.style.display = "inline-block";
742782
cTop = config.top;
743783
cLeft = config.left;
744784
cWidth = config.width;
@@ -799,59 +839,41 @@ SNAKE.Board = SNAKE.Board || (function() {
799839
// setup event listeners
800840

801841
myKeyListener = function(evt) {
802-
803-
var keyNum;
804-
if(evt.which) {
805-
keyNum=evt.which;
806-
} else if(evt.keyCode) {
807-
keyNum=evt.keyCode;
808-
}
842+
if (!evt) var evt = window.event;
843+
var keyNum = (evt.which) ? evt.which : evt.keyCode;
809844

810-
if (me.getBoardState() === 0){
811-
// do nothing
812-
} else if (me.getBoardState() === 1) {
813-
if ( !(keyNum >= 37 && keyNum <= 40) ) {
814-
return; // if not an arrow key, leave
815-
}
845+
if (me.getBoardState() === 1) {
846+
if ( !(keyNum >= 37 && keyNum <= 40) ) {return;} // if not an arrow key, leave
816847

848+
// This removes the listener added at the #listenerX line
817849
SNAKE.removeEventListener(elmContainer, "keydown", myKeyListener, false);
818850

819851
myKeyListener = function(evt) {
820-
var keyNum;
821-
if(evt.which) {
822-
keyNum=evt.which;
823-
} else if(evt.keyCode) {
824-
keyNum=evt.keyCode;
825-
}
852+
if (!evt) var evt = window.event;
853+
var keyNum = (evt.which) ? evt.which : evt.keyCode;
854+
826855
mySnake.handleArrowKeys(keyNum);
827856

828-
if (!evt) var evt = window.event;
829857
evt.cancelBubble = true;
830858
if (evt.stopPropagation) {evt.stopPropagation();}
831859
if (evt.preventDefault) {evt.preventDefault();}
832860
return false;
833861
};
834862
SNAKE.addEventListener( elmContainer, "keydown", myKeyListener, false);
835863

836-
SNAKE.addEventListener( elmContainer, "keyup", function(evt) {
837-
if (!evt) var evt = window.event;
838-
evt.cancelBubble = true;
839-
if (evt.stopPropagation) {evt.stopPropagation();}
840-
if (evt.preventDefault) {evt.preventDefault();}
841-
return false;
842-
}, false);
843-
864+
mySnake.rebirth();
844865
mySnake.handleArrowKeys(keyNum);
845866
me.setBoardState(2); // start the game!
846867
mySnake.go();
847868
}
848-
if (!evt) var evt = window.event;
869+
849870
evt.cancelBubble = true;
850871
if (evt.stopPropagation) {evt.stopPropagation();}
851872
if (evt.preventDefault) {evt.preventDefault();}
852873
return false;
853874
};
854875

876+
// Search for #listenerX to see where this is removed
855877
SNAKE.addEventListener( elmContainer, "keydown", myKeyListener, false);
856878
};
857879

@@ -874,6 +896,7 @@ SNAKE.Board = SNAKE.Board || (function() {
874896
elmContainer.appendChild(elmTryAgain);
875897
elmTryAgain.style.zIndex = index;
876898
elmTryAgain.style.display = "block";
899+
me.setBoardState(0);
877900
};
878901

879902
// ---------------------------------------------------------------------

0 commit comments

Comments
 (0)