• 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)>
      • 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
  • Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Donate
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAy 1-7: More Ctrl. Flow

09/16/2012

30 Comments

 
Picture
Lesson #1-12: Analysis of the BySimpleIMeanSimple class

Looking at yesterday's "game" in-depth.

Yesterday, we created our very first game. Now we will dissect the class behind it.

Here's the code: 
 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
1. The first line is simple: import the Random class.
2. We then declare the name of the class: BySimpleIMeanSimple
3. Then a static integer called dieValue, which will hold the value of the rolled die is created 
4. Next, we create the main method, which just invokes a method called rollDie();

5. The rollDie() method is then created. It has the following parts.

I. Creation of the Random object called rand.
II. Assignment of a random number between 0 and 5 (+1 since numbers range from 1 to 6 on a die) to the dieValue integer we created earlier.
III. It uses System.out.println to display the value of the integer dieValue.
IV. It invokes the method testDieValue(), which is defined below the rollDie() method. This testDieValue() method requires an integer parameter, so we input the new dieValue, which contains the random number between 1 and 6.

6. Next we declare the testDieValue() method. This is where the Control Flow statements happen (we control the way that the program proceeds based on conditional statements).
It takes the value of integer dieValue and then carries out the appropriate if statement.

I. If the value is less than or equal to 2, the player loses, so it displays, "You lose."
II. If the value is exactly 6 (==), it displays "You Win! Congratulations!"
III. If the value is 3 or 4 or 5 (This could've been written in many ways, such as: if (dieValue >= 3 && dieValue <= 5)...), it rerolls, invoking again the rollDie() method, which creates a random number and assigns it as the value of dieValue and then uses this value to invoke the testDieValue();

Someone asked earlier, what is the purpose of the: System.out.println();
Well, it is there to create an empty line in the console (where the output is displayed). It helps organize the way the program "speaks" to you.

NOTE: Make sure you are keeping up with the braces. When you do actually programming, braces won't be color coded to show you where the boundaries of each class or method are!

Lesson #1-13: Switches in Java

Java, much like railroad tracks, incorporates switches to direct the path of execution.
We will look at switches using an example that we have used before: a coin toss.

NOTE: Make sure that the class name in the Package Explorer to the left side of the screen on Eclipse matches that of the one stated in the code. Otherwise, you will get an error. The reason for this is that when you press Play, Eclipse looks for the class that with the name of the file that you are executing, and it will not be able to locate it.

(i.e. You should create a class named CoinTossSwitch.java and copy this code): 
 import java.util.Random;

class CoinTossSwitch {

public static void main(String[] args) {

Random rand = new Random();
int randomInt = rand.nextInt(2);

System.out.println(randomInt);

switch(randomInt) {

case 0:
System.out.println("Tails!");
break;
case 1:
System.out.println("Heads!");
break;

} // Ends switch

} // Ends main

} // Ends Class
Let's examine the switch in detail:
 switch (randomInt) {

case 0:
System.out.println("Tails!");
break;
case 1:
System.out.println("Heads!");
break;

} // Ends switch
As you can tell, the format of a switch is as follows  :
 switch (variable){

case 1: // if variable == 1
doThis();
break;

case 2: // if variable == 2
doThat();
break;

case 3:
doOther();
break;

case 4: case 5: case 6: // if variable == 4, 5 or 6
doSomethingElse();
break;

default:
doDefault();
break;
}

Inside the parentheses (), you write the variable name that you are testing.
If this variable is an integer, you write:

case 1, case 2, and so on (You can combine cases and include a default case for all other cases).

If it is is a boolean, you would write:

case true, case false.

If it is a String, you would write whichever values that the String variable can take.
For example, if your String can contain any color value, you would use:

case "red", case "blue", and so on.

The purpose of the "break;" is to separate the cases. If you forget to put the break, the code will not stop executing after a case is satisfied. It will keep going. Break can also be used to end loops (which we will discuss in the coming lessons).

__________

Applying this to the example code above,

1. We are testing an integer called randomInt.
2. If randomInt is 0, the computer will output: "Heads!" and break the case.
3. If randomInt is 1, the computer will output: "Tails!" and break the case.

What would be a practical application of this code? Let's say that we have a game in which the player can select one of three characters.
Let's name them Mario, Luigi, and Yoshi.

We would then create a String variable that holds the value of the name, and then use a switch to carry out the appropriate response.

Note: The following will only work in Java 1.7. On Java 1.6 and below, we would use integers to represent our characters, like in the example at this link.
 //Sample Character Selection Screen

class CharacterSelect {

public static void main(String[] args) {

String currentCharacter = "Mario";

int maxLife;
int maxJump;
int maxSpeed;

switch (currentCharacter) {

case "Mario":
System.out.println("You have selected Mario!");
maxLife = 70;
maxJump = 50;
maxSpeed = 25;
break;

case "Luigi":
System.out.println("You have selected Luigi!");
maxLife = 40;
maxJump = 70;
maxSpeed = 30;
break;

case "Yoshi":
System.out.println("You have selected Yoshi!");
maxLife = 50;
maxJump = 30;
maxSpeed = 40;
break;

}

}

}
We store the current character's value in the String variable currentCharacter and use a switch to change the character's attributes.


Tomorrow we will discuss looping. We are getting closer to developing a game, so hang tight and stay tuned!

If you want to thank me for the guide, you can donate here or download TUMBL +!

Thank you guys for reading and I'm here if you have any questions!

Comment below and Like us on Facebook. 
Go to Day 6: If... else...
Go to Day 8: Looping
30 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.