Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 1-6: If... else...

9/16/2012

77 Comments

 
Picture
Lesson #1-9: If and else statements

In Java, we can control the "flow" of the program by using conditional statements.
Let's take a look at this simple example. 
Picture
This is a pretty simple class. Let me explain some of the things we have here.

1. class DrinkingAgeTest creates a class called DrinkingAgeTest.
2. We then create a static integer age (this does not have to be static if it is placed inside the main method).
3. Of course we create a main method, as all Java programs will look for this as a starting point.
4. Now the interesting parts: 
Picture
Meet the if statement. It is written like so: 
Picture
When Java encounters the if statement, it checks each condition to see if it is satisfied (true).
If so, it carries out everything inside the braces for that if statement. If not, the statement is completely skipped.

Applying this to the example class, the compiler checks if age, which is 18, is greater than or equal to 21.
It is not, so it skips the line: 
Picture
Since none of the previous conditions are met, it automatically goes into the else statement, which outputs: 
Picture
Note: If you have multiple "if" statements, each of them is checked to see if true. If only one of the conditions can be true (i.e. the conditions you are testing for are mutually exclusive), it is therefore  
Picture
Now we are going to apply this to simulate probability.


Lesson #1-10: Simulating Probability: Relational Operators

In the lesson #1-8, we created a random object called rand and invoked the method "nextInt." 
Picture
Recall that the above generates a number between 0 and 10 (11 numbers starting with zero).

Now let's simulate probability, starting with a simple example. 
import java.util.Random;

//Let's simulate a coin toss!
public class Simulation {
    public static void main(String[] args){
       
        Random rand = new Random();
       
        int result = rand.nextInt(2);
           
        if (result == 0){
            System.out.println("heads");
        }
       
        else if(result == 1){
            System.out.println("tails");
        }
       
        else if(result == 3){
            System.out.println("side. fix your random number generator");
        }
       
    }
}
 
This is also a pretty straightforward class. We use the random number generator to create an number between 0 and 1 (inclusive) and set it equal to a newly created integer: result.

We arbitrarily decide that the value of 1 is equal to heads and the value of 0 is equal to tails. Now we test the value of result using else if statements (mutually exclusive events) and display the appropriate string (text).

Since the value of result should never (theoretically) be 3, I created a third statement that is only read if result does somehow become 3 (it should be as common as landing a coin on its side).

In these two lessons, we used two operators: == and >=. 


Here are all six relational operators: 
Picture
Lesson #1-11: Conditional Operators and Our First Game

In this lesson, we are going to create a simple game. Oh, don't get too excited. By simple, I mean simple.
Before we do that, here's a quick lesson on conditional operators: 
Picture
When you have an if statement like below: 
Picture
Then if either conditionOne or conditionTwo is true, it will invoke the "doThis()" method;

When you have a statement like this:
Picture
Then both conditionOne and conditionTwo must be satisfied before "doThat" method is called.
NOTE: When you just write a boolean (variables with value of either true or false, such as conditionOne) without stating: == true or == false, it is assumed that you actually wrote boolean = true.

In other words:
Picture
and
Picture
Are equivalent.

Now back to our game!
Here is how the game works. You roll a 6-sided die. If you land a 1 or 2 you lose. If you get a 3, 4, or 5, you roll again. If you get a 6, you win.

To write good programs, you have to plan beforehand. You should think the following:

1. I need a random number generator.
2. I should test if:

I. a number is less than or equal to 2.
II. if not, I should test whether if that number is between 3 and 5 (inclusive)
III. if not, I should test whether if that number is 6.

3. I should carry out an appropriate response.

Simple. Now let's create this class.

figure 3: BySimpleIMeanSimple class, Complete

 import java.util.Random;

//The greatest game ever made follows.
class BySimpleIMeanSimple {


static int dieValue;

public static void main(String[] args) {

rollDie();

} // ends main


static void rollDie() {

Random rand = new Random();

// Assign a random number between 1 and 6 as dieValue
dieValue = rand.nextInt(6) + 1;

System.out.println("You rolled a " + dieValue);

// Why add 1? Think about it.
testDieValue(dieValue);

} // ends rollDie()


static void testDieValue(int dieValue) {

if (dieValue <= 2) {
System.out.println("You Lose.");
} // ends if

else if (dieValue == 3 || dieValue == 4 || dieValue == 5) {
System.out.println();
System.out.println("Rerolling.");
System.out.println();
rollDie();
} // ends else if

else if (dieValue == 6) {
System.out.println("You win! Congratulations!");
} // ends else if

} // ends testDieValue()

} // ends BySimpleIMeanSimple Class
I want you to spend time with this class. Copy and paste it into eclipse and play around with it.
We will discuss this class in detail in tomorrow's lesson!

You might be surprised by what you have already learned! 

A tip: if you don't know what the purpose of a certain line of code is, it is a good idea to remove it temporarily (comment it out and run it). You can compare results with and without the statement =)
You can also modify the code and see how it works differently.

If you have any questions:
Comment below!
Go to Day 5: More Math
Go to Day 7: More Control Flow
77 Comments

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.