()
| 93 | //draw everything to the canvas |
| 94 | |
| 95 | function draw() |
| 96 | { |
| 97 | ctx.drawImage(ground,0,0); |
| 98 | |
| 99 | for(let i=0;i<snake.length;i++) |
| 100 | { |
| 101 | ctx.fillStyle=(i==0)?"green":"white"; |
| 102 | ctx.fillRect(snake[i].x,snake[i].y,box,box); |
| 103 | |
| 104 | ctx.strokeStyle="red"; |
| 105 | |
| 106 | ctx.strokeRect(snake[i].x,snake[i].y,box,box); |
| 107 | } |
| 108 | |
| 109 | ctx.drawImage(foodImg,food.x,food.y); |
| 110 | |
| 111 | //old head position |
| 112 | |
| 113 | let snakeX=snake[0].x; |
| 114 | let snakeY=snake[0].y; |
| 115 | |
| 116 | |
| 117 | //which direction |
| 118 | |
| 119 | if(d=="LEFT") snakeX-=box; |
| 120 | if(d=="UP") snakeY-=box; |
| 121 | if(d=="RIGHT") snakeX+=box; |
| 122 | if(d=="DOWN") snakeY+=box; |
| 123 | |
| 124 | //if the snake eats the food |
| 125 | if(snakeX==food.x&&snakeY==food.y) |
| 126 | { |
| 127 | score++; |
| 128 | eat.play(); |
| 129 | food={ |
| 130 | x:Math.floor(Math.random()*17+1)*box, |
| 131 | y: Math.floor(Math.random()*15+3)*box |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | else{ |
| 136 | |
| 137 | //remove the tail |
| 138 | snake.pop(); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | //add new Head |
| 143 | |
| 144 | let newHead={ |
| 145 | x:snakeX, |
| 146 | y:snakeY |
| 147 | } |
| 148 | |
| 149 | //game over |
| 150 | |
| 151 | if(snakeX<box||snakeX>17*box||snakeY<3*box||snakeY>17*box||collision(newHead,snake)){ |
| 152 |
nothing calls this directly
no test coverage detected