This is part of the course “Snake Game With Rust, JavaScript, and WebAssembly”.
In this part, we will start the game loop and make the snake move. Changes that we will introduce in this part reflected in this commit.
Process
It is time to add the method that will update the game state. For now, it will only receive milliseconds from the last update. We will handle snake direction change in the next part.
We will need two methods to move the snake, so let’s add them first.
By using the normalize method, we can convert a vector to a unit vector with the length of one.
Now we are ready to write logic that will change positions of snake edges.
We start by calculating the distance the snake was supposed to crawl. Then we update the tail by going over each edge, except the last one(head), and shifting it. Because we don’t handle direction change in this part, the logic to update head position is pretty simple, we add to the old position direction scaled by the distance.
Let’s compile the Rust and go to the JavaScript side.
Game Loop
Let’s update GameManager with the game loop.
We are calling the process method to update the game state and render everything 60 times per second.
In the next part, we will handle keypresses and update the snake direction. Stay tuned!
Reach the next level of focus and productivity with increaser.org.