Game Dev 101
Progress
0/22

Lesson 2 of 22, in world 1: Foundations

The Game Loop

Time
About 12 min
Builds on
What Makes a Game

Every game, from Pong to Elden Ring, is built around one loop that runs many times per second. Each pass through the loop produces one frame — one still picture. Show 60 of them a second and your brain sees motion, like a flipbook.

Input → Update → Render

  1. Process input — what keys, buttons or touches are active right now?
  2. Update — advance the simulation one step: move things, apply physics, check collisions, run AI.
  3. Render — clear the screen and draw the whole world in its new state.
LiveThe game loop in slow motion

Hold the arrow keys (or WASD). At 1–3 frames per second you can see the three phases one by one: the key is read, the position is updated, and only then is the new frame rendered. Push it to 60 and it becomes smooth motion.

JavaScript
function gameLoop(now) {
  const input = readInput()      // 1. what is the player doing?
  update(input)                  // 2. change the world's state
  render()                       // 3. draw the world
  requestAnimationFrame(gameLoop) // ask the browser to call us again next frame
}
requestAnimationFrame(gameLoop)

requestAnimationFrame is the browser’s way of saying “call me right before the next screen refresh” — usually 60 times a second, or 120/144 on fast monitors. Game engines run their own version of this loop for you.

Redraw everything, every frame

Unlike a web page, where you change one DOM element and the browser repaints just that, a game typically clears the whole canvas and draws every object again each frame. It sounds wasteful, but GPUs are built for exactly this, and it keeps the code simple: the picture is always a pure function of the current state.

The frame budget

At 60 FPS you have 1000 / 60 ≈ 16.7 ms to do all input, update and rendering work. At 144 FPS it’s under 7 ms. That’s why games care so much about performance: anything slow (loading a file, a big allocation, a long loop) inside the frame causes a visible hitch.

Key takeaways

  • A game is an infinite loop: input → update → render, once per frame.
  • Rendering redraws the whole world from state every frame.
  • At 60 FPS you get ~16.7 ms per frame for everything. Never block the loop.

Check yourself

Q1. In which phase should a bullet move forward?

Q2. How many milliseconds does one frame get at 60 FPS?

Q3. You need to wait 3 seconds before spawning an enemy. What do you do?

Exercises

  1. [object Object]
  2. [object Object]
  3. [object Object]