What is a Game Loop

A game loop is the core of any game. It runs continuously, updating the game state and rendering the screen as fast as possible. Without it, nothing moves, nothing responds, nothing happens.

html
1<!DOCTYPE html>2<html lang="en">3<head>4    <meta charset="UTF-8">5    <meta name="viewport" content="width=device-width, initial-scale=1.0">6    <title>My First Webpage</title>7</head>8<body>910    <h1>Welcome to My Website</h1>11    <p>This is a simple paragraph of text on a live webpage.</p>1213    <!-- This is a link to an external website -->14    <p>Learn more by visiting <a href="https://www.w3schools.com" target="_blank">W3Schools</a>.</p>1516    <!-- This displays an image -->17    <img src="https://placeholder.com" alt="Sample Placeholder Image">1819</body>20</html>

Every game you have ever played has one. The details differ but the structure is always the same.

The Basic Structure

At its simplest a game loop looks like this:

csharp
1while (running)2{3    ProcessInput();4    Update();5    Render();6}

Three steps, repeated forever until the game exits.

ProcessInput

This is where you read from the keyboard, mouse, or controller. You check what the player is doing and store that information for the update step.

csharp
1void ProcessInput()2{3    if (Keyboard.IsKeyDown(Key.Escape))4        running = false;56    if (Keyboard.IsKeyDown(Key.Left))7        player.velocity.x = -1;8    else if (Keyboard.IsKeyDown(Key.Right))9        player.velocity.x = 1;10    else11        player.velocity.x = 0;12}

Update

This is where the game logic runs. Physics, AI, collision detection, score tracking — all of it happens here.

csharp
1void Update(float deltaTime)2{3    player.position.x += player.velocity.x * speed * deltaTime;4    player.position.y += player.velocity.y * speed * deltaTime;56    CheckCollisions();7    UpdateEnemies(deltaTime);8    UpdateScore();9}

Render

This is where you draw everything to the screen based on the current game state.

csharp
1void Render()2{3    ClearScreen();4    DrawBackground();5    DrawPlayer(player.position);6    DrawEnemies(enemies);7    DrawHUD(score, health);8    SwapBuffers();9}

Delta Time

You may have noticed deltaTime in the update function. This is the time elapsed since the last frame in seconds.

Without delta time your game runs at different speeds on different machines. A fast computer runs the loop more times per second than a slow one, making everything move faster.

With delta time you multiply all movement by the elapsed time, so the game runs at the same speed regardless of frame rate.

csharp
1float deltaTime = currentTime - lastTime;2lastTime = currentTime;3Update(deltaTime);

Fixed vs Variable Timestep

There are two common approaches to the game loop:

Variable Timestep

The update runs as fast as possible and uses delta time to compensate. Simple but can cause physics instability at very low frame rates.

Fixed Timestep

The update runs at a fixed rate, say 60 times per second, regardless of the render rate. Physics is stable and deterministic. More complex to implement.

csharp
1float fixedStep = 1.0f / 60.0f;2float accumulator = 0;34while (running)5{6    float deltaTime = GetDeltaTime();7    accumulator += deltaTime;89    ProcessInput();1011    while (accumulator >= fixedStep)12    {13        Update(fixedStep);14        accumulator -= fixedStep;15    }1617    Render();18}

Conclusion

A game loop is simple in concept but the details matter. Start with a variable timestep, understand how delta time works, and move to a fixed timestep when you need stable physics.

Every game engine you will ever use has a game loop under the hood. Understanding it makes you a better developer regardless of what tools you use.