Skip to content

Commit

Permalink
Bug fixing problem with canvas when zoomed and when pressing multiple…
Browse files Browse the repository at this point in the history
… arrows simultaneously kills snake.
  • Loading branch information
gamalielhere committed Feb 4, 2016
1 parent 0d7b445 commit 3763cd1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ $(document).ready(function() {
y: Math.round(Math.random()*(canvasHeight-snakeWidth)/snakeWidth),
}
}

var cWsW = Math.floor(canvasWidth/snakeWidth);
var cHsW = Math.floor(canvasHeight/snakeWidth);
function render(){
canvasContext.fillStyle = canvasFill;
canvasContext.fillRect(0,0,canvasWidth,canvasHeight);
Expand Down Expand Up @@ -134,7 +137,7 @@ $(document).ready(function() {
newY ++;
}
// GAME OVER CONDITION
if(newX === -1 || newX === canvasWidth/snakeWidth || newY === -1 || newY === canvasHeight/snakeWidth || collide(newX,newY,snake)){
if(newX === -1 || newX === cWsW || newY === -1 || newY === cHsW || collide(newX,newY,snake)){
$(".game").css("display", "none");
$(".notification").css("display", "inline-block");
$(".start").css("display", "none");
Expand Down Expand Up @@ -200,14 +203,15 @@ $(document).ready(function() {

function collide(x, y, array){
for (var i = 0; i < array.length; i++) {
if (x === array[i].x && y === array[i].y)
if (x === array[i].x && y === array[i].y){
console.log("should die");
return true;
}
}
return false;
}


$(document).keydown(function(event) {
$(document).on('keyup',function(event) {
var arrow = event.which; // return which key was pressed
if(arrow === 37 && direction !== "right"){
direction = "left";
Expand All @@ -217,8 +221,6 @@ $(document).ready(function() {
direction = "right";
} else if(arrow === 40 && direction !== "up"){
direction = "down";
} else if(arrow === 32){
startGame();
}
});
});

0 comments on commit 3763cd1

Please sign in to comment.