Snake Game With Rust, JavaScript, and WebAssembly | Part 7

This is part of the course “Snake Game With Rust, JavaScript, and WebAssembly”.

Image for post
Image for post
GitHub Repository and Deployed Game

In this part, we will make that every time the snake eats the food, its tail grows longer. Changes that we will introduce in this part reflected in this commit.

Let’s add a method that will handle intersection with food, and call it from the process.

src/lib.rs

If the center of the food is inside of the head segment, it means that there is an intersection. If this is the case, we take the tail tip segment to calculate a new position for the tail tip to make the snake one cell longer.

Let’s compile Rust and go to the JavaScript side.

It is time to start saving the best score to local storage so that when the player visits the game next time, he could see his previous record.

www/src/storage.js

In the GameManager, we start passing the best score to the render method of the view, we updating the best score after calling the process method.

www/src/game-manager.js

Now we can open the web page and start hunting the food!

Image for post
Image for post
hunting the food

In the next part, we will complete the development of the game by adding logic that will restart the game when the player lost.

Reach the next level of focus and productivity with increaser.org.

Image for post
Image for post
Increaser

Written by

Software engineer, creator of increaser.org. More at geekrodion.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store