CodeAgeBy Sitezack
Canvas for MakersDraw and animate your game0 XP
Chapter progress0/6

Lesson 6 of 7

The game loop

A game lives to the beat of a "tick": a function called in a loop (by setInterval) that moves the world one small step forward.

Here, the obstacle races towards the car: on each tick, its x shrinks. We test the tick() function directly — animation is just tick() repeated!

var obstacle = {x: 300};
function tick() {
  obstacle.x = obstacle.x - 5;
}
setInterval(tick, 50);

🎯 Your goal

Create var obstacle = {x: 300} and the tick() function that subtracts 5 from obstacle.x (then start it with setInterval(tick, 50) to watch the animation).

PRO lesson

This advanced chapter is part of CodeAge PRO. Unlock it to keep your adventure going.

Discover PRO