What is Game Loop?
The Heartbeat That Keeps Your Game Alive
Think of your heartbeat — it pumps steadily whether you're sleeping or sprinting, keeping you alive without you thinking about it. A game loop works the same way — it's a continuous cycle that checks for input, updates the game state, and draws everything on screen, running dozens of times per second to keep your game responsive and smooth.
The Never-Ending Cycle
Game Loop = Repeat forever: process input → update game state → render graphics. Use delta time so movement speed stays consistent regardless of frame rate.
When to Use Game Loop
Game Loop isn't always the right call. Here's a quick mental model:
You're building any real-time game
Platformers, shooters, racing games, action RPGs — anything where things move continuously needs a game loop. Even simple games like Pong require constant updates and rendering.
You need consistent physics
Want jumps to always reach the same height? Gravity to always feel the same? Use a fixed timestep for physics calculations inside your game loop.
Your game should run the same on all hardware
When you multiply movement by delta time (time since last frame), a fast computer with small time steps and a slow computer with big time steps produce identical results.
You want smooth animations
The game loop's render phase draws every frame. Combined with delta time, animations interpolate smoothly even when frame rates vary.
You're building a turn-based game with no animations
Pure turn-based games like chess (no animated pieces) can use event-driven programming. Wait for player input, update state, redraw once. No continuous loop needed.
Your app is static or event-driven
Web forms, CRUD apps, and most business software don't need continuous loops. They respond to user events, update, done. Don't add complexity you don't need.
Interactive Game Loop Demo
Watch how delta time keeps movement consistent. Compare a ball moving with and without time-based movement at different simulated frame rates.
AI Prompts for Game Loop
Now that you understand game loop, use these prompts with your AI coding agent. Copy the one that matches what you're building — the agent will handle the implementation.
Tip: These prompts help you implement game loops in any language or engine. Copy, paste, and fill in the [brackets]. The AI will generate code and explain each part.
Game Loop in Real Applications
Unity's Update vs FixedUpdate Unity provides two loop hooks: Update() runs every frame (variable timestep for visuals) while FixedUpdate() runs at fixed intervals (for physics). This is the fixed/variable timestep pattern built into the engine.
Godot's _process vs _physics_process Godot has _process(delta) for frame-dependent updates and _physics_process(delta) for physics. Both receive delta time automatically — Godot handles the game loop for you.
Browser Games with requestAnimationFrame Every browser game (Canvas, WebGL, PixiJS, Phaser) uses requestAnimationFrame as its game loop. It syncs with the display refresh rate and provides timestamps for delta time calculation.
Pygame's Clock Pygame provides pygame.time.Clock() with tick(FPS) that caps frame rate and returns delta time in milliseconds. It's a simple way to implement the game loop pattern.
Common Game Loop Mistakes to Avoid
Moving by pixels-per-frame instead of pixels-per-second
Writing "x += 5" instead of "x += 300 * deltaTime" makes your game run faster on better hardware. Always multiply movement by delta time for consistent speed everywhere.
Running physics on variable timestep
Physics simulations need consistent time steps. Variable timestep makes jumps inconsistent — sometimes higher, sometimes lower. Use fixed timestep for physics, variable for rendering.
Doing heavy calculations in the render phase
The render phase should only draw. Put AI, physics, and game logic in the update phase. Heavy render code causes frame drops and stuttering.
Not capping delta time
If your game freezes momentarily (loading assets, garbage collection), delta time spikes. Objects teleport across the screen. Cap deltaTime to a maximum (e.g., 0.1 seconds) to prevent this.
Related Building Blocks
Ready to Build Real Products?
Learn to ship MicroSaaS apps with AI in the Solo Builder course.