Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Flutter Projects

You're reading from   Flutter Projects A practical, project-based guide to building real-world cross-platform mobile applications and games

Arrow left icon
Product type Paperback
Published in Apr 2020
Publisher Packt
ISBN-13 9781838647773
Length 490 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Simone Alessandria Simone Alessandria
Author Profile Icon Simone Alessandria
Simone Alessandria
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

Preface 1. Hello Flutter! 2. Miles or Kilometers? Using Stateful Widgets FREE CHAPTER 3. My Time - Listening to a Stream of Data 4. Pong Game - 2D Animations and Gestures 5. Let's Go to the Movies - Getting Data from the Web 6. Store That Data - Using Sq(F)Lite To Store Data in a Local Database 7. Firing Up the App - Integrating Firebase into a Flutter App 8. The Treasure Mapp - Integrating Maps and Using Your Device Camera 9. Let's Play Dice: Knockout - Creating an Animation with Flare 10. ToDo App - Leveraging the BLoC Pattern and Sembast 11. Building a Flutter Web App 12. Assessment 13. Other Books You May Enjoy Appendix

Adding randomness to the game

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:

  1. 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';
  1. Then, in the _PongState class, let's write a method, called randomNumber(), which returns a random double number...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime