Your First Flutter Flame Game

Mar 6 2024 · Dart 3, Flutter 3.10.1, Android Studio 2021.3.1 or higher, Visual Studo Code 1.7.4 or higher

Part 3: Collision Detection & Overlays

17. Add a Heads-Up Display

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 16. Break Down Meteorites Next episode: 18. Make a Game Menu

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

While playing, it’s important to keep the player informed of what’s happening in the game at all times.

Demo

HUD

Open overlays/hud.dart to start.

class Hud extends PositionComponent with HasGameRef<MeteormaniaGame> {
}
late TextComponent _levelComponent;
late TextComponent _healthComponent;
late TextComponent _pointsComponent;
static const hudTextStyle = TextStyle(
  fontFamily: 'PressStart2P',
  color: Color.fromRGBO(255, 255, 255, 1),
);
@override
Future<void>? onLoad() async {
  return super.onLoad();
}
_levelComponent = TextComponent()
text: 'LVL ${game.manager.level}',
textRenderer: TextPaint(
  style: hudTextStyle.copyWith(
    fontSize: 18,
  ),
),
anchor: Anchor.center,
position: Vector2(
  GameConstants.cameraWidth / 2,
  GameConstants.cameraHeight - 32,
),
_healthComponent = TextComponent(
  text: 'Lives: ${game.manager.health}',
);
textRenderer: TextPaint(
  style: hudTextStyle.copyWith(
    fontSize: 14,
  ),
),
anchor: Anchor.centerLeft,
position: Vector2(32, 32),
_pointsComponent = TextComponent(
  text: 'Points: ${game.manager.points}',
  textRenderer: TextPaint(
    style: hudTextStyle.copyWith(
      fontSize: 14,
    ),
  ),
  anchor: Anchor.centerLeft,
  position: Vector2(32, 64),
);
addAll([
  _levelComponent,
  _healthComponent,
  _pointsComponent,
]);
@override
void update(double dt) {
  super.update(dt);
}
_levelComponent.text = 'LVL ${game.manager.level}';
_healthComponent.text = 'Lives: ${game.manager.health}';
_pointsComponent.text = 'Points: ${game.manager.points}';
if (game.manager.isGameOver) {
  removeFromParent();
}

MeteormaniaGame

In meteormania_game.dart, import your heads up display.

import 'overlays/hud.dart';
_world.add(Hud());