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. 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 ConditionsAndBranchingActivity.java 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import android.app.Activity; import android.os.Bundle; public class ConditionsAndBranchingActivity extends Activity { // This is the entry point to our game @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
// 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"); }else{ //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"; switch(command){ case "Go East": Log.d("Moving: ","You travel East"); break; case "Go West": // code to go west Log.d("Moving: ","You travel West"); break; case "Take sword": // code to take the sword Log.d("Tada: ","You take the sword"); break; //more possible cases default: // Sorry I don't understand your command Log.d("Hmm: ","Try again"); break; } // !!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.
John, the code runs but my phone screen remains blank. I can see the output on the pc log though.
This is just as it should be. The coming projects are much more visual.
Did I miss something or is the code missing the declarations and initializations for isComingOverTheBridge, friendlyTroops and enemyTroops?
Yes I left them out. In working code you would need them for the code to compile.