Book Image

Learning Java by Building Android Games - Second Edition

By : John Horton
Book Image

Learning Java by Building Android Games - Second Edition

By: John Horton

Overview of this book

Android is one of the most popular mobile operating systems presently. It uses the most popular programming language, Java, as the primary language for building apps of all types. However, this book is unlike other Android books in that it doesn’t assume that you already have Java proficiency. This new and expanded second edition of Learning Java by Building Android Games shows you how to start building Android games from scratch. The difficulty level will grow steadily as you explore key Java topics, such as variables, loops, methods, object oriented programming, and design patterns, including code and examples that are written for Java 9 and Android P. At each stage, you will put what you’ve learned into practice by developing a game. You will build games such as Minesweeper, Retro Pong, Bullet Hell, and Classic Snake and Scrolling Shooter games. In the later chapters, you will create a time-trial, open-world platform game. By the end of the book, you will not only have grasped Java and Android but will also have developed six cool games for the Android platform.
Table of Contents (30 chapters)
Learning Java by Building Android Games Second Edition
Contributors
Preface
Index

Combining different control flow blocks


And you might have been able to guess that we can combine any of the decision-making tools like if, else, and switch within our while loops and all the rest of the loops too. For example:

int x = 0;
int tooBig = 10;

while(true){
   x++; // I am going to get mighty big!
   if(x == tooBig){
      break;
   } // No you're not
   
   // code reaches here only until x = 10
}

It would be simple to go on for many more pages demonstrating the versatility of control flow structures but at some point, we want to get back to finishing the game.

Now we are confident with if and else, let's have a look at one more concept to do with loops. So here is one last concept combined with loops.

Continue...

The continue keyword acts in a similar way to break- that we learned about in the previous chapter- up to a point. The continue keyword will break out of the loop body but will also check the condition expression afterward so the loop could run again. An example will help...