Save score

This commit is contained in:
Pierre HUBERT 2020-03-23 17:39:20 +01:00
parent 4aa6e5b2f1
commit 380abb24b0
2 changed files with 7 additions and 0 deletions

View File

@ -20,6 +20,7 @@
<div id="gameScreen"> <div id="gameScreen">
<h1>Game</h1> <h1>Game</h1>
<p>Score: <span id="scoreTarget"></span></p>
<div id="canvasTarget"></div> <div id="canvasTarget"></div>

View File

@ -48,6 +48,7 @@ const levelChoicesTarget = byId("levelChoice")
const startGameBtn = byId("startGameBtn") const startGameBtn = byId("startGameBtn")
const stopGameBtn = byId("stopGameBtn") const stopGameBtn = byId("stopGameBtn")
const canvasTarget = byId("canvasTarget") const canvasTarget = byId("canvasTarget")
const scoreTarget = byId("scoreTarget")
/** /**
@ -89,6 +90,7 @@ async function startGame(gameID) {
// Initialize the map & snake arrays // Initialize the map & snake arrays
let map = []; let map = [];
let snake = []; let snake = [];
let score = 0;
/// First with empty cells... /// First with empty cells...
for(let i = 0; i < level.dimensions[0]; i++) { for(let i = 0; i < level.dimensions[0]; i++) {
@ -164,6 +166,7 @@ async function startGame(gameID) {
case FOOD: case FOOD:
increaseSize = true; increaseSize = true;
score++;
break; break;
case SNAKE: case SNAKE:
@ -186,6 +189,9 @@ async function startGame(gameID) {
} }
// Refresh score
scoreTarget.innerHTML = score;
// Redraw screen // Redraw screen