Adding the score and completing the game

A game wouldn't be complete without a way to measure performance. In this case, the action to perform is pretty obvious. Every time the ball touches the bat, we can add one point to the score. Let's look at how we can apply this action:

  1. Let's create a variable that will contain the score, at the top of the _PongState class:
  int score = 0;
  1. Next, in the build() method, add a new Positioned widget to the stack. This will contain a Text with the score:
return Stack(    children: <Widget>[        Positioned(            top: 0,            right: 24,            child: Text('Score: ' + score.toString()),        ),
  1. Then, in the checkBorders() method, update the score each time the ball touches the bat:
if (posX >= (batPosition - diameter) && posX <= ...

Get Flutter Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.