Please share...

  • Video courses

    All game programming videos just $19 - For a very limited time these three videos and thousands more are up to 90% off!

    Just enter coupon code NEWU19 in cart/checkout

Unity Pro Development A to Z – Build 10 Android/iOS Games

The Complete Android & Java Developer Course – Build 21 Apps

The Complete Java Developer Course. Learn Step by Step

In this quick mini-project we will practice how to test for conditions using Java in an Android game project. Condition testing is a basic staple of all programming including games. If you think it sounds complicated you will be pleasantly surprised.

  • Learning java by building Android games

    Learning Java by Building Android Games

    Want to learn Java for Android? Even if you are completely new to either Java, Android, or game programming but are aiming to publish Android games, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms or other object-oriented languages.
    • Setup your own Android game programming environment using Android Studio
    • Control logic, branch your code, and add real decision-making depth to your games
    • Design and use 2d sprite animations, smooth pixel graphics, sound FX, simple collision detection and artificial intelligence (AI)
    • Build around a dozen sample test apps and 4 complete working games!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

    Android Game Programming by Example

    Learn to build exciting Android game projects. Build a simple flappy-bird like game to a multi-environment, tough, retro platformer then an OpenGL ES 2 Asteroids clone, running at hundreds of frames per second .
    • Animate your characters with sprite sheets, add multiple scrolling parallax backgrounds, and implement genuinely tough, playable levels in your games
    • Every single line of code is printed in the book! No need to refer to the code files while trying to follow along. All the code files are also supplied separately so you can refer to them in their completed form and copy/paste them into your project if you like.
    • Implement a multitude of other game features such as pickups, firing weapons, HUD’s, generating and playing sound FX, scenery, level transition, high scores, and more
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US Only)

About this project

Skill level 1
Time to complete 30 minutes

New concepts

  • Using variables in condition tests
  • Branching our code based upon the result

To get started create a new Android project and enter ConditionsAndBranching in the Aplication Name field and enter ConditionsAndBranchingActivity in the Activity Name field. When Android Studio has finished generating the project read on.

Delete the generated code in then enter the required code that we need to work within as shown below and as explained in the previous mini-project Game variables demo.

import android.os.Bundle;

public class ConditionsAndBranchingActivity extends Activity {

    // This is the entry point to our game
    protected void onCreate(Bundle savedInstanceState) {

        // Our Java code will start here

        // !!Our code must end here - for now.

Now we can get on with practicing our condition checking and branching. In between the two comments that denote where our code goes enter the following condition checks and branching. Be sure to read the comments in the code for a reminder of how each part of the code works. There is lots of code but don’t be put off it is not complicated. It just demonstrates in a number of different ways the concepts we discussed in the tutorials.

If the Log.d... code looks strange and alien to you be sure to read the previous mini-project Game variables demo first. Also remember to use the Alt|Enter keyboard combination to add the import... code for it as well.
         // Our Java code will start here

        // First we demonstrate the bleedin' obvious
        if(10 == 9){

            // is obviously false 10 is not equal to 9.
            Log.d("Duhh","!!10 is 9!!");
            // The above will NOT be printed
            // because the code is never reached


        // Now a condition which IS true
        if(!(10 == 9)){

            // This is true because 10 is NOT 9.
            // This may seem convoluted but we will see
            // this type of comparison can be useful.
            Log.d("!(10 == 9)"," is true");


        // Both parts of this condition are true
        // Both parts must be because we use && (and)
        if((10 > 9) && (10 < 11)){
             // is true because both parts are true.
             Log.d("(10 > 9) && (10 < 11)"," is true");
         }         // However this turns out false
         // because just one part is false
         if((10 > 9) && (10 < 9)){
             // is false because only one part of the expression
             // is true (10 > 9) and the other is false (10 < 9).
             Log.d("(10 > 9) && (10 < 9)"," False! Can't see me!!");
         }         // However, the below evaluates true because
         // we use || (or).
         if((10 > 9) || (10 < 9)){
             // is now true because...
             // one part of the expression is true (10 > 9)
            Log.d("(10 > 9) || (10 < 9)"," but you can see me!!");


        //check for new fastest time

        // Declare and initialize two variables
        int timeTaken = 50; // 50 second lap
        int fastestTime = 55; // Previous best 55 seconds

        if(timeTaken < fastestTime) {
             // the player took less time than the fastest time!!
             // So a new fastest time has been set
             Log.d("YAY"," New fastest time!!");
             // Now change the the fastest time variable
             // to the new fastest time (timeTaken)
             fastestTime = timeTaken;
             Log.d("fastestTime=","" + fastestTime);
         // Now let's do some branching with our conditions
         // First we will play with if and else.
         // When you run this code try playing with the
         // three values below to see the effect it has.
         boolean isComingOverTheBridge = true;
         int enemyTroops = 999;
         int friendlyTroops = 10;
         if(isComingOverTheBridge && friendlyTroops > enemyTroops){

            //shoot them
            Log.d("Here they come: ","OPEN FIRE!!!!");

        }else if(isComingOverTheBridge && friendlyTroops < enemyTroops) {

            //blow the bridge
            Log.d("Here they come: ","Blow the bridge Sgt.Dohun");


            //Hold position
            Log.d("Hmm: ","Nothing to see here");


        // Now let's see switch in action
        // get input from user in a String variable called command

        // Try playing with the value we initialize command to
        String command = "Fly up";


            case "Go East":
                Log.d("Moving: ","You travel East");

            case "Go West":
                // code to go west
                Log.d("Moving: ","You travel West");

            case "Take sword":
                // code to take the sword
                Log.d("Tada: ","You take the sword");

            //more possible cases

                // Sorry I don't understand your command
                Log.d("Hmm: ","Try again");


        // !!Our code must end here - for now.

Now finally, attach an Android device and deploy/run the game. Note that although the output to the actual device is the same static display as our blank game project, it is the  output in the Logcat window on the  Android tab  that we are interested in. The output is shown below.



Be sure to run the program several times and play with the values of the variables where suggested. You are now ready for the tutorial Looping our game code.

Please visit the Android category of our game coding bookstore for beginners