Skip to content

Commit 667bb83

Browse files
authored
Merge pull request patorjk#56 from dginovker/fix_input
This looks good! Thanks for the contribution! Sorry it took so long to review it.
2 parents e148290 + 14b1d9b commit 667bb83

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

js/snake.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ SNAKE.Snake = SNAKE.Snake || (function() {
115115
playingBoard = config.playingBoard,
116116
myId = instanceNumber++,
117117
growthIncr = 5,
118-
moveQueue = [], // a queue that holds the next moves of the snake
119-
currentDirection = 1, // 0: up, 1: left, 2: down, 3: right
118+
lastMove = 1,
119+
preMove = -1,
120+
isFirstMove = true,
121+
isFirstGameMove = true,
122+
currentDirection = -1, // 0: up, 1: left, 2: down, 3: right
120123
columnShift = [0, 1, 0, -1],
121124
rowShift = [-1, 0, 1, 0],
122125
xPosShift = [],
@@ -214,7 +217,7 @@ SNAKE.Snake = SNAKE.Snake || (function() {
214217
};
215218

216219
/**
217-
* This method is called when a user presses a key. It logs arrow key presses in "moveQueue", which is used when the snake needs to make its next move.
220+
* This method is called when a user presses a key. It logs arrow key presses in "currentDirection", which is used when the snake needs to make its next move.
218221
* @method handleArrowKeys
219222
* @param {Number} keyNum A number representing the key that was pressed.
220223
*/
@@ -229,37 +232,40 @@ SNAKE.Snake = SNAKE.Snake || (function() {
229232
if (isDead || isPaused) {return;}
230233

231234
var snakeLength = me.snakeLength;
232-
var lastMove = moveQueue[0] || currentDirection;
233235

234236
//console.log("lastmove="+lastMove);
235237
//console.log("dir="+keyNum);
236238

239+
let directionFound = -1;
240+
237241
switch (keyNum) {
238242
case 37:
239243
case 65:
240-
if ( lastMove !== 1 || snakeLength === 1 ) {
241-
moveQueue.unshift(3); //SnakeDirection = 3;
242-
}
244+
directionFound = 3;
243245
break;
244246
case 38:
245247
case 87:
246-
if ( lastMove !== 2 || snakeLength === 1 ) {
247-
moveQueue.unshift(0);//SnakeDirection = 0;
248-
}
248+
directionFound = 0;
249249
break;
250250
case 39:
251251
case 68:
252-
if ( lastMove !== 3 || snakeLength === 1 ) {
253-
moveQueue.unshift(1); //SnakeDirection = 1;
254-
}
252+
directionFound = 1;
255253
break;
256254
case 40:
257255
case 83:
258-
if ( lastMove !== 0 || snakeLength === 1 ) {
259-
moveQueue.unshift(2);//SnakeDirection = 2;
260-
}
256+
directionFound = 2;
261257
break;
262258
}
259+
if (currentDirection !== lastMove) // Allow a queue of 1 premove so you can turn again before the first turn registers
260+
{
261+
preMove = directionFound;
262+
}
263+
if (Math.abs(directionFound - lastMove) !== 2 && isFirstMove || isFirstGameMove) // Prevent snake from turning 180 degrees
264+
{
265+
currentDirection = directionFound;
266+
isFirstMove = false;
267+
isFirstGameMove = false;
268+
}
263269
};
264270

265271
/**
@@ -270,7 +276,6 @@ SNAKE.Snake = SNAKE.Snake || (function() {
270276

271277
var oldHead = me.snakeHead,
272278
newHead = me.snakeTail,
273-
myDirection = currentDirection,
274279
grid = playingBoard.grid; // cache grid for quicker lookup
275280

276281
if (isPaused === true) {
@@ -286,14 +291,20 @@ SNAKE.Snake = SNAKE.Snake || (function() {
286291
grid[newHead.row][newHead.col] = 0;
287292
}
288293

289-
if (moveQueue.length){
290-
myDirection = currentDirection = moveQueue.pop();
294+
if (currentDirection !== -1){
295+
lastMove = currentDirection;
296+
if (preMove !== -1) // If the user queued up another move after the current one
297+
{
298+
currentDirection = preMove; // Execute that move next time (unless overwritten)
299+
preMove = -1;
300+
}
291301
}
302+
isFirstMove = true;
292303

293-
newHead.col = oldHead.col + columnShift[myDirection];
294-
newHead.row = oldHead.row + rowShift[myDirection];
295-
newHead.xPos = oldHead.xPos + xPosShift[myDirection];
296-
newHead.yPos = oldHead.yPos + yPosShift[myDirection];
304+
newHead.col = oldHead.col + columnShift[lastMove];
305+
newHead.row = oldHead.row + rowShift[lastMove];
306+
newHead.xPos = oldHead.xPos + xPosShift[lastMove];
307+
newHead.yPos = oldHead.yPos + yPosShift[lastMove];
297308

298309
if ( !newHead.elmStyle ) {
299310
newHead.elmStyle = newHead.elm.style;
@@ -365,7 +376,6 @@ SNAKE.Snake = SNAKE.Snake || (function() {
365376

366377
isDead = true;
367378
playingBoard.handleDeath();
368-
moveQueue.length = 0;
369379
};
370380

371381
/**
@@ -374,6 +384,9 @@ SNAKE.Snake = SNAKE.Snake || (function() {
374384
*/
375385
me.rebirth = function() {
376386
isDead = false;
387+
isFirstMove = true;
388+
isFirstGameMove = true;
389+
preMove = -1;
377390
};
378391

379392
/**

0 commit comments

Comments
 (0)