Game Loops — The Heartbeat of Every Game
Learn about the game loop, the fundamental concept that makes every video game tick, update, and render frame by frame.
What Makes a Game Run?
A Simple Game Loop
// The basic structure of every game loop
function gameLoop() {
// 1. INPUT - Check what the player is doing
handleInput();
// 2. UPDATE - Move things, check collisions
updateGameState();
// 3. RENDER - Draw everything on screen
drawEverything();
// Do it again! (aims for 60 times per second)
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();60 Frames Per Second
The requestAnimationFrame function in JavaScript is the best way to create a game loop in the browser. It automatically syncs with your monitor's refresh rate (usually 60Hz) and pauses when the tab is not visible, saving battery and CPU power.
Think about your favorite video game. Can you identify what happens in each part of the game loop? What inputs does it check (keyboard, mouse, controller, touch)? What gets updated each frame (player position, enemy AI, physics, timers)? What gets drawn on screen? Write down the input-update-render cycle for your game!
Ready to build?
Put what you learned into practice — pick a project and start coding.
Start Building Free