We can slow our snake
down by updating its state less frequently. Rather than call update
each time romLoop
is called (i.e. updating on each tick, usually 60 times per second), we can keep track of the tick count and only call update
every “n” ticks (e.g. every 10 ticks)
We’ll add a global ticks
variable for our counter, and initialise it with a value of 0. Then each time romLoop
is called we’ll increment the ticks
count, and only update
the snake if the ticks
count is cleanly divisible by 10:
let ticks = 0;
function romLoop() {
ticks++;
clearGfx(COL_WHT);
if (ticks % 10 == 0) {
snake.update();
}
snake.draw();
}
If we refresh the browser we’ll see a snake that moves at a more reasonable speed:
Jump to step: Introduction · Project setup · Creating the snake · Drawing the snake · Moving the snake · Throttling the speed · Input handling · Placing the fruit · Eating the fruit · Losing the game · Managing state · Playing sound effects · Bug fixing · Distribution