One of the basic ingredients that make a game interesting is the random element. There are two moments in our game where we can add some randomness. One is the bouncing angle: it doesn't need to be exactly 45 degrees each time it bounces. Making the bouncing less regular will make the game less predictable. And we can also work with the speed of the ball.
Let's consider the perfect bouncing we are using now to be 1. If the bouncing could take a value between 0.5 and 1.5, the bouncing would be less regular, but still keep a degree of realism:
- In order to use random values in Flutter and Dart, we need to import the math library. In the pong.dart file, add the import statement as shown here:
import 'dart:math';
- Then, in the _PongState class, let's write a method, called randomNumber(), which returns a random double number...