In order for our game to dynamically respond to the player’s input, conform to the game’s rules and control the artificial intelligence of characters within the game; our code will need to make decisions. Programming languages achieve this by creating and testing for conditions based upon, the values held by our variables.
[widgets_on_pages id=”udemy_advert_java_1″][widgets_on_pages id=”udemy_code_details”]
We want to be able to do things like this:
if the player has no lives left end the game. Go back to the main menu screen if the last alien has been destroyed start the next level.
Note that the above is pseudo code to demonstrate how if works. It is not real Java. We will see real Java code using the if keyword shortly. We will use pseudo code from time to time to help explain things. It will usually be obvious when it is pseudo code but I will point it out when necessary. Also note how the action (e.g. Go back to main menu screen) is indented from the edge of the page? We will use indents allot to make our code more readable and I strongly encourage you to do the same.
Java has the if keyword and it works like this:
if(some condition is true) { // do whatever }
The if keyword is followed by the opening and closing parentheses (). Inside the parentheses, we have a condition and afterward contained in the opening and closing curly braces {} we have the code that will run if the condition is true. We will deal with the // do whatever part in the next tutorial. For this tutorial, we will look at the most useful possibilities we can use with the Java if keyword to create conditions we can then test to see if they are true. Let’s look at some more operators this time some comparison operators that we can use to check for conditions in our games.
Comparison operators
We can already add ( +), take away ( -), multiply( *), divide ( /), assign ( =), increment ( ++), compare ( ==), and decrement ( --) with operators. Let’s introduce some more super-useful operators, and then we will go straight to actually understanding how to use them in Java. Don’t worry about memorizing every operator given here. Just glance at the list of operators and their explanations and then move quickly on to the next section.
There, we will put some comparison operators to use and they will become much clearer as we see a few examples of what they allow us to do. They are presented here in a list just to make the variety and scope of operators apparent from the start.
== is the equality comparison operator not to be confused with the assignment operator. It tests for equality and is either true or false. We can create and test conditions like this.
if(10 == 9){ // is obviously false 10 is not equal to 9. }
! is the logical NOT operator. Here it is in action.
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. }
!= This is another comparison operator, which tests if something is NOT equal. For example:
if(10 != 9){ // is true because 10 is not equal to 9. }
>: This is another comparison operator, which tests if something is greater than something else. The expression:
if(10 > 9){ // is, of course, true. }
>=: This operator tests whether one value is greater than or equal to the other, and if either is true, the result is true. For example, the expression:
if(10 >= 9){ // is true. }
Also look at this for further clarification.
if(10 >= 10){ // is also true because 10 equals 10 // even though 10 is not greater than 10
<=: Like the preceding operator, this operator tests for two conditions but this time, less than or equal to. The expression,
if(10 <= 9){ // is false. }
However, both of these conditions are true.
if (10 <= 10){ // is true because 10 equals 10 } if (9 <= 10){ // is true because 9 less than 10 }
&&: This operator is known as logical AND. It tests two or more separate parts of an expression and ALL parts must be true in order for the result to be true. Logical AND is usually used in conjunction with the other operators to build more complex tests. The expression:
if((10 > 9) && (10 < 11)){ // is true because both parts are true. }
However, this turns out 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). }
|| This operator is called logical OR. It is just like logical AND except that only one of two or more parts of an expression need to be true for the expression to be true. Let’s look at the last example we used but replace the && sign with ||. The expression,
if((10 > 9) || (10 < 9)){ // is now true because... // one part of the expression is true (10 > 9) }
And as you might expect, we can use our game’s variables in if statements as well. Here are a few examples. You don’t need to fully understand how the code in the next few samples works just take a look at it and think about how we are controlling our game code with the if statements and their conditions. This is real code from a game.
boolean hitDetected = false; // Some code that detects // if the player ship was hit goes here if(hitDetected) { playSound(explosion); playerLives --; }
Now, look at how we might check to see if the player had got a new fastest time at the end of a racing game. The variables would have been declared and initialized previously.
//check for new fastest time if(timeTaken < fastestTime) { // the player took less time than the fastest time!! // So a new fastest time has been set // Now change the the fastest time variable // to the new fastest time (timeTaken) fastestTime = timeTaken; }
[widgets_on_pages id=”udemy_advert_java_3″][widgets_on_pages id=”udemy_code_details”]
The
if keyword and all of these operators are virtually useless without a way of properly using them to make real decisions that affect real variables and code. Let’s look at how we can structure our game’s code to get stuff done. Note that almost any code at all can go inside the curly braces
{} of a
if statement. In the next tutorial, we will see that we can branch our game code based on the conditions we have just learned to test for. Take a look at how to branch our game code.
a pretty helpful tutorial. Thanks John
Thanks very much!
Hey John, how do you import images
Take a look at this: http://gamecodeschool.com/android/drawing-graphics-demo/
Hey. It will be great if someone could tell me which version of Java I should download to execute whatever i have learned.
Hi Rohit,
The tutorials are just the theory but there are related projects where you actually get to start coding. The first one is how to set up Android Studio, where you can run code.
John, I am trying to learn how to program midp based apps. Can u help me with the tutorials or recommend some tutorials for me
Hi Ayomide,
Unfortunately, I have no experience of this
I really like the tutorials, but i was wondering if there are any pdf’s that you recommend i can use
Hi Albert,
Thanks for commenting. I am not aware of any PDF specifically and Kindle doesn’t format code very well. Was your enquiry because you want to read on-the-go?
i am 15 and was wandering, how long do you think it will take me to learn to do Java programming if I’m learning about 2-3 of these oarts a day?
Hi George,
Learning Java and being able to do cool things with it doesn’t take long at all. You could probably do all the tutorials and projects on this site in a few free days.
Mastering Java, however will come by continually using it over the years, including keeping up-to-date with changes.
Have a think about what you want to achieve with your programming in the future and focus your study accordingly. And don’t neglect your other school work either,
I hope this answers your question.
Good luck,
John.
Cheers, you’ve really helped and I’m definitely carrying on I’m hooked with trying to learn to code now and maybe one day I can be as good as people like you.
Hi George,
Thanks for your kind comment. Yes, it is easy to get hooked. I am an addict myself. If you carry on with this enthusiasm then I can guarantee you will be better than me quite quickly.
Keep coding!
Sorry about this but one last question, where can I find “good” 2d platform sprites or how can I learn to make them even if I’m not particularly good at drawing?
The best-known site for free 2D graphics is opengameart.org. There is also an old but still useful book on designing 2D game graphics called Designing Arcade Game Graphics. It is 500 pages and does contain some outdated content but might be what you’re looking for.
Download Designing Arcade Game Graphics for free.