This is the last part of the “Snake in CreateJS Tutorial”. Here you can check the other ones: Part 1 and Part 2.
In this part, we are going to implement a scoring system and add new views – main menu and game over.
The code can be found in the repository: Repository.
Implementation of score
The score is a really simple thing. You just need to add the points every time when the snake eats food and draw a new score on the screen.
So, let’s add a score field to World class:
// source/objects/World.js
export class World {
constructor(stage) {
this.stage = stage;
this.score = 0;
...
}
...
}
And increase it by 10 every time when the snake eats food:
// source/objects/World.js
export class World {
render() {
...
if (this.checkFoodCollision()) {
this.score += 10;
this.snake.grow();
this.food.generateRandomPosition();
this.drawFood();
}
...
}
}
Continue reading