I have a setInterval that draws my snake game, and everytime the snake "eats" I have a clearInterval and then the setInterval var with adjusted speed. The original setInterval starts on load. How can I make it start with the press of a button?
My attempts at placing the setInterval inside the button and using functions have worked in making the button start the setInterval, but it always breaks my clearInterval inside the draw function.
<button onclick="">Start game</button>
if (snakeX == food.x && snakeY == food.y){
food = {
x : Math.round(Math.random()*(cvsWidth/snakeWidth-1)),
y : Math.round(Math.random()*(cvsHeight/snakeHeight-1))
};
score++;
if (speed >= 40) speed = speed-3;
clearInterval(interval);
interval = setInterval(draw, speed);
}else{
//Remove last
snake.pop();
}
var speed = 140;
var interval = setInterval(draw, speed);