docs: Update t-rex game to use TapCallbacks (#2888)

Update t-rex game to use `TapCallbacks` instead of `TapDetector`, in
anticipation of [deprecating the
latter](https://github.com/flame-engine/flame/pull/2886).

Also update the term high score to avoid a dictionary entry.

Co-authored-by: Lukas Klingsbo <me@lukas.fyi>
This commit is contained in:
Luan Nico
2023-12-01 03:19:17 -05:00
committed by GitHub
parent 1aa4098dd2
commit 81930e2384
2 changed files with 6 additions and 7 deletions

View File

@ -11,7 +11,6 @@ gamepads
gameplay gameplay
gapless gapless
grayscale grayscale
highscore
hoverable hoverable
hoverables hoverables
inactives inactives

View File

@ -15,7 +15,7 @@ import 'package:trex_game/player.dart';
enum GameState { playing, intro, gameOver } enum GameState { playing, intro, gameOver }
class TRexGame extends FlameGame class TRexGame extends FlameGame
with KeyboardEvents, TapDetector, HasCollisionDetection { with KeyboardEvents, TapCallbacks, HasCollisionDetection {
static const String description = ''' static const String description = '''
A game similar to the game in chrome that you get to play while offline. A game similar to the game in chrome that you get to play while offline.
Press space or tap/click the screen to jump, the more obstacles you manage Press space or tap/click the screen to jump, the more obstacles you manage
@ -33,11 +33,11 @@ class TRexGame extends FlameGame
late final TextComponent scoreText; late final TextComponent scoreText;
int _score = 0; int _score = 0;
int _highscore = 0; int _highScore = 0;
int get score => _score; int get score => _score;
set score(int newScore) { set score(int newScore) {
_score = newScore; _score = newScore;
scoreText.text = '${scoreString(_score)} HI ${scoreString(_highscore)}'; scoreText.text = '${scoreString(_score)} HI ${scoreString(_highScore)}';
} }
String scoreString(int score) => score.toString().padLeft(5, '0'); String scoreString(int score) => score.toString().padLeft(5, '0');
@ -99,7 +99,7 @@ class TRexGame extends FlameGame
} }
@override @override
void onTapDown(TapDownInfo info) { void onTapDown(TapDownEvent event) {
onAction(); onAction();
} }
@ -125,8 +125,8 @@ class TRexGame extends FlameGame
currentSpeed = startSpeed; currentSpeed = startSpeed;
gameOverPanel.visible = false; gameOverPanel.visible = false;
timePlaying = 0.0; timePlaying = 0.0;
if (score > _highscore) { if (score > _highScore) {
_highscore = score; _highScore = score;
} }
score = 0; score = 0;
_distanceTraveled = 0; _distanceTraveled = 0;