FlutterIntermediate 3 min read

Think in frames: the game loop

Understand update and render, frame-independent movement and why game state should not depend on frame count.

On this page

An interactive game repeatedly reads input, updates a world and presents a frame. The loop continues while the game is active. A tap-driven puzzle may need very little continuous motion; an action game usually needs regular updates.

Move with elapsed time

If an object moves five pixels every frame, it moves twice as fast at 120 frames per second as at 60. Instead, express speed per second and multiply by elapsed time.

main.dart
void main() {
  const speed = 120.0; // logical pixels per second
  var position = 0.0;
  final frameDurations = [0.016, 0.018, 0.014, 0.020];

  for (final dt in frameDurations) {
    position += speed * dt;
    print('After ${dt}s: ${position.toStringAsFixed(2)}');
  }
}

Update and render have different jobs

The update step changes world state: position, score, health or timers. The render step draws a view of that state. Keeping those responsibilities separate helps you reason about a game without inspecting every pixel.

Handle pauses and large time steps

When an app resumes, elapsed time can be unexpectedly large. A simple demo may clamp the step; a physics simulation can use fixed simulation steps with an accumulator. The right choice depends on the behavior you need to preserve.

Pick a small first game

Start with one action, one goal and one feedback signal. A tap-to-score target teaches interaction and state without requiring physics or assets.

Calculate what one frame contributes

At 120 logical pixels per second, an update lasting 0.016 seconds contributes 1.92 pixels. An update lasting 0.020 seconds contributes 2.4 pixels. The movement follows elapsed time rather than assuming every frame took the same duration.

Add those displacements to the previous position; do not replace the position with the displacement. The accumulated position is game state, while rendering uses that state to draw the current frame.

For a visible demonstration, run the complete Flame movement starter . It adds boundaries and a rendered player to the same update principle. Compare it with Memory Pairs , which needs no continuous loop because its changes are event-driven.

Try it yourself

Simulate one second using ten steps of 0.1, then twenty steps of 0.05. Both should move the same total distance. Compare that with a fixed number of pixels per frame.

Trace one second of movement

Suppose a player moves at 120 world units per second. At 60 updates per second, a typical step is about two units; at 30 updates, it is about four. Both should cover about 120 units after one second. A fixed “move two pixels” rule would slow down on the second device.

Decide how the game handles a long pause or a delayed frame. A very large elapsed value can move a player past a thin obstacle before the next overlap check. Use a deliberate fixed-step or bounded simulation policy for demanding physics, and test the resulting time behavior. Do not silently change the game’s clock to hide a collision bug.

The enemy AI lesson uses elapsed time for cooldowns. Test movement, cooldown and pause together so the visual state and game rules agree.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.