BP Forums
Java Game Help - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: Java (https://bpforums.info/forumdisplay.php?fid=19)
+---- Thread: Java Game Help (/showthread.php?tid=798)



Java Game Help - ThreeLeBlancs - 07-28-2013

Hello! I am creating my own game in java. I want to have it so it gives you choices and if you say left, there is a different option for it than if you typed right. When the first line of code pops up in the console, I can type, the same for second and the third, but when I want to type Left I can't type anything. Here is my code. Also I am using Eclipse. The same program that BrandonioProductions uses. Also you probably know his youtube is but here it is anyways. <!-- w --><a class="postlink" href="http://www.youtube.com/user/BrandonioProductions">www.youtube.com/user/BrandonioProductions</a><!-- w -->
========================================================

import java.util.Scanner;
public class Island {

static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws InterruptedException{
System.out.println("You awake to find yourself on an island in the middle of the sea.");
Thread.sleep(3000);
System.out.println("You then walk around the island, in which you find two paths.");
Thread.sleep(3000);
System.out.println("Choose from the left cobble path, or the right class path.");
}

public String message;

public Island() {
if (message.equals("Left"));
System.out.println("You then walk down the left path.");
}



}


Re: Java Game Help - brandonio21 - 08-04-2013

So what you want to do is get the users input directly after you ask what direction they want to go. That can be done using your Scanner variable: sc. So I have created a new code snippet that does exactly this. Then, we analyze the user's input to see if it contains directional information using the .equalsIgnoreCase() - if it corresponds to a certain direction (left or right) we then send the program to methods corresponding with that direction.

[code2=java]import java.util.Scanner;
public class Island {

static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws InterruptedException{
System.out.println("You awake to find yourself on an island in the middle of the sea.");
Thread.sleep(3000);
System.out.println("You then walk around the island, in which you find two paths.");
Thread.sleep(3000);
System.out.println("Choose from the left cobble path, or the right class path.");
//here we need to get the user's input
String direction = sc.nextLine(); //this holds the direction that the user chose
if (direction.equalsIgnoreCase("left"))
GoLeft();
else if (direction.equalsIgnoreCase("right"))
GoRight();
else
System.out.println("That is not a valid direction!");
}

//this method holds all of the information chosen if the user goes left
public void GoLeft()
{
System.out.println("You go left and find a trapdoor. What do you do?");
}

//this method holds all of the info chosen if the user goes right
public void GoRight()
{
System.out.println("You go right and find a dead end. Boo hoo.");
}
}[/code2]

This should be what you are looking for.