StonePickaxes
|
 |
«
Posted
2011-12-04 01:06:26 » |
|
Hey, I'm Nathan (StonePickaxes) and I am new to java game development.
I am very excited to learn how to make games, but I am having a hard time getting started. I would appreciate it a ton if anyone could help me get started or maybe give me some pointers for how to write a basic java game (say, a 2d platformer or a space shooter).
Thanks so much in advance,
Nathan
EDIT-
What is a reliable way of taking user input from the command line? I am trying to write a text adventure game where the user presses 1, 2, 3, etc to determine what happens.
Thanks again.
|
|
|
|
roland
|
 |
«
Reply #1 - Posted
2011-12-04 01:23:46 » |
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| try { int key = System.in.read(); if (key == '1') System.out.println("1"); else System.out.println("Not 1: "+key); } catch (IOException e) { e.printStackTrace(); } |
|
|
|
|
StonePickaxes
|
 |
«
Reply #2 - Posted
2011-12-04 01:30:19 » |
|
Thanks.
I have a few questions:
1) Why did you do "try"? Would it work without this or the "catch"?
2) When I run this, even if I inputted a 1, it shows "Not 1: 13 Not 1: 10" What does that mean?
Thanks again.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
roland
|
 |
«
Reply #3 - Posted
2011-12-04 01:46:15 » |
|
Thanks.
I have a few questions:
1) Why did you do "try"? Would it work without this or the "catch"?
2) When I run this, even if I inputted a 1, it shows "Not 1: 13 Not 1: 10" What does that mean?
Thanks again.
1. Most things in java need a try catch - Its to handle unexpected errors. In this case, if something happens to the System input when you press a character which causes it to fail. Basically you put the things you want to do in try and then if something fails, it will jump to the catch block, where your code can handle the error. If you don't want to put try-catch around things, you can make the method you are in throw IOException. eg. 1 2 3 4
| public void DoSomething() throws IOException { } |
but then you must put try-catch around the method when you call it. If you don't use try-catch, your program won't run. It's kind of annoying when you are beginning, but it's actually really useful.  2. Did you just press 1 and then enter? it should work. Make sure you are on a new line before you press 1 though. http://www.asciitable.com/ shows that 10 is the newline character and 13 is the carriage return character. When you press enter, it creates a new line by adding \r\n (\r is carriage return and \n is new line). From the output you got, it seems like you didn't press 1 which is strange because you obviously did. I haven't used System.in much because it blocks execution until you enter a key, which is fine for text games but not much else, especially since you have to enter the key in the console and press enter.
|
|
|
|
StonePickaxes
|
 |
«
Reply #4 - Posted
2011-12-04 01:56:05 » |
|
I figured out why it wasn't diaplaying a 1. Where it said if it equals 1, it should have been if it equals 48 or whatever the ascii value of 1 is.
Thanks so much for the help. If I have any more questions, do you mind if I PM you?
|
|
|
|
roland
|
 |
«
Reply #5 - Posted
2011-12-04 02:22:08 » |
|
did you put the ' ' around the 1? 1 is not '1' '1' means (char) 1 = 48, but yeah, use 48 if that's easier. Yes, you can PM me if you want.
|
|
|
|
ra4king
|
 |
«
Reply #6 - Posted
2011-12-04 02:45:41 » |
|
Tis best to publicly post questions so if someone else has the same questions, they could find the answers. Welcome to JGO and wish you best of lucks in your journey with Java. If you need to understand the big picture of things, this post should prove to be quite helpful.  EDIT: Also, don't forget to handle new line characters since when you hit ENTER after inputting a line in the command prompt, it includes the \r\n (Carriage return + Line feed) characters (on Windows) and only the Line feed (\n) character on Unix.
|
|
|
|
roland
|
 |
«
Reply #7 - Posted
2011-12-04 02:57:18 » |
|
Tis best to publicly post questions so if someone else has the same questions, they could find the answers.
True, I forgot about that 
|
|
|
|
Mads
|
 |
«
Reply #8 - Posted
2011-12-04 07:06:22 » |
|
Thanks.
I have a few questions:
1) Why did you do "try"? Would it work without this or the "catch"?
2) When I run this, even if I inputted a 1, it shows "Not 1: 13 Not 1: 10" What does that mean?
Thanks again.
1. Most things in java need a try catch - Its to handle unexpected errors. In this case, if something happens to the System input when you press a character which causes it to fail. Basically you put the things you want to do in try and then if something fails, it will jump to the catch block, where your code can handle the error. If you don't want to put try-catch around things, you can make the method you are in throw IOException. eg. 1 2 3 4
| public void DoSomething() throws IOException { } |
but then you must put try-catch around the method when you call it. If you don't use try-catch, your program won't run. It's kind of annoying when you are beginning, but it's actually really useful.  2. Did you just press 1 and then enter? it should work. Make sure you are on a new line before you press 1 though. http://www.asciitable.com/ shows that 10 is the newline character and 13 is the carriage return character. When you press enter, it creates a new line by adding \r\n (\r is carriage return and \n is new line). From the output you got, it seems like you didn't press 1 which is strange because you obviously did. I haven't used System.in much because it blocks execution until you enter a key, which is fine for text games but not much else, especially since you have to enter the key in the console and press enter. Most things in Java do not need try-catches. Only stuff that can fail, or get interrupted, such as file i/o, and input in some cases. Things that only exists in the jvm, do not need try-catches. These include string building, math, all of your game logic, and your input if you're not reading it through the system. When I write a game, it is only when resources like sound and images are loaded into the memory I need a try-catch. That, and sometimes when you do sneaky rendering tricks to speed it all up.
|
|
|
|
gouessej
|
 |
«
Reply #9 - Posted
2011-12-04 11:29:08 » |
|
Hi
You should use java.util.Scanner.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
StonePickaxes
|
 |
«
Reply #10 - Posted
2011-12-04 18:47:11 » |
|
I have used the scanner before, but how can I check what a scanner is equal to? I need to check if the input is equal to an integer or a string.
|
|
|
|
ra4king
|
 |
«
Reply #11 - Posted
2011-12-04 18:49:23 » |
|
|
|
|
|
StonePickaxes
|
 |
«
Reply #12 - Posted
2011-12-04 19:19:15 » |
|
Thanks, I figured it out.
Is there any way to clear all of the text from the command prompt screen? I want to try to keep it clean.
|
|
|
|
ra4king
|
 |
«
Reply #13 - Posted
2011-12-04 19:26:59 » |
|
No 
|
|
|
|
StonePickaxes
|
 |
«
Reply #14 - Posted
2011-12-04 19:55:34 » |
|
Ah, oh well. Now I am trying to get the input from a method and use it in another method, and I can't get it to work. Here's the code - 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
| package source;
import java.util.Scanner;
public class Game {
public static boolean running = true; public static int loc = 1; public static String input;
public static void InputHandler() { Scanner rawInput = new Scanner(System.in); String input = rawInput.nextLine();
if (input.equals("exit")) { System.out.println("Thanks for playing, now exiting..."); running = false; } Game.InputRedirector(); }
public static void LocationIdentifier() {
if (loc == 1) { Game.MainMenu(); }
}
public static void InputRedirector() {
if (!running) { loc = 0; }
if (loc == 1) { Game.MainMenuInput(); }
}
public static void main(String[] args) { while (running) { Game.LocationIdentifier(); Game.InputHandler(); } } public static void Instructions() { System.out.println("-Welcome to *name*, an adventure game where you can level up, find treasure, and explore."); System.out.println("-"); System.out.println("-"); System.out.println("-"); System.out.println("-"); }
public static void MainMenu() { System.out.println("-Main Menu"); System.out.println("-play -instructions -exit"); }
public static void MainMenuInput() { if (input.equals("play")) { loc = 2; } if (input.equals("instructions")) { Game.Instructions(); } }
} |
Is there a better way i can get the input and then use it in the individual location methods? Or am I just doing this whole thing wrong? In that case, what can be changed? Thanks for everything guys.
|
|
|
|
ra4king
|
 |
«
Reply #15 - Posted
2011-12-04 20:06:30 » |
|
How about passing the String as a parameter?
|
|
|
|
StonePickaxes
|
 |
«
Reply #16 - Posted
2011-12-04 20:08:00 » |
|
I don't know what that means  Can you copy my code and edit it to show me what that means?
|
|
|
|
ra4king
|
 |
«
Reply #17 - Posted
2011-12-04 20:11:16 » |
|
Here's an example of passing variables as parameters: 1 2 3 4 5 6 7
| public static void myMethod(String myString) { System.out.println("You called myMethod with " + myString); }
public static void main(String[] args) { myMethod("an ordinary string."); } |
|
|
|
|
StonePickaxes
|
 |
«
Reply #18 - Posted
2011-12-04 20:18:27 » |
|
 I don't know how I would go about doing that with my code. I am pretty new at this, as you can see haha.
|
|
|
|
ra4king
|
 |
«
Reply #19 - Posted
2011-12-04 20:42:56 » |
|
Then I would suggest you keep learning Java and not stop and try to accomplish more advanced stuff as it will confuse you 
|
|
|
|
StonePickaxes
|
 |
«
Reply #20 - Posted
2011-12-04 20:47:55 » |
|
I figured it out! Thanks :DD
Now I have the game engine (funny calling this an engine) set up, and I just need to write the actual game.
Thanks! If I have any more questions I will post them here.
|
|
|
|
Kurten
|
 |
«
Reply #21 - Posted
2011-12-04 20:55:55 » |
|
The best one can do when itching to write a game is read!
Good literature will give you and understanding of java that is very useful when writing games, I would recommend Head First: Java, that's for the fundamental stuff within the language, like tr try catch statement and what it does.
When wanting to read more about game programming, developing games in Java by David Brackeen is good, it teaches you to write small things like threadpools and screen managers that can later be used to make a game.
//Kurten
|
|
|
|
Eli Delventhal
|
 |
«
Reply #22 - Posted
2011-12-04 21:44:18 » |
|
Thanks, I figured it out.
Is there any way to clear all of the text from the command prompt screen? I want to try to keep it clean.
This will probably be over your head, but you can use Swing to make a text area that looks like the console and then you have full control over it (clearing, scrolling, etc.).
|
|
|
|
StonePickaxes
|
 |
«
Reply #23 - Posted
2011-12-04 21:52:39 » |
|
I have seen several tutorials on swing, but I just don't know enough about the language yet to know how to use that. Thanks, though! Another issue I have is in my method names; they are all yellow-underlined and the reason is stated as "This method has a constructor name" but when I change any of them to a constructor, they don't work. Any ideas as to why? EDIT - Never mind, figured it out. Here's the code of the game so far - Game.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package source;
public class Game {
public static boolean running = true; public static int loc = 1; public static void main(String[] args) { while (running) { LocationIdentifier.LocationIdentifier(); InputHandler.InputHandler(); } } } |
LocationIdentifier.java 1 2 3 4 5 6 7 8 9 10 11 12
| package source;
import locations.MainMenu;
public class LocationIdentifier extends Game { public static void LocationIdentifier() {
if (loc == 1) { MainMenu.MainMenu(); } } } |
InputHandler.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package source;
import java.util.Scanner;
public class InputHandler extends Game { public static void InputHandler() { Scanner rawInput = new Scanner(System.in); String input = rawInput.nextLine();
if (input.equals("exit")) { System.out.println("Thanks for playing, now exiting..."); running = false; } InputRedirector.InputRedirector(input); } } |
MainMenu.java 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
| package locations;
import source.Game;
public class MainMenu extends Game {
public static void MainMenu() { System.out.println("-Main Menu"); System.out.println("-play -instructions -exit"); }
public static void MainMenuInput(String input) { if (input.equals("play")) { loc = 2; } if (input.equals("instructions")) { MainMenu.Instructions(); } } public static void Instructions() { System.out.println("-Welcome to *name*, an adventure game where you can level up, find treasure, and explore."); } } |
InputRedirector.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package source;
import locations.MainMenu;
public class InputRedirector extends Game { public static void InputRedirector(String input) {
if (!running) { loc = 0; }
if (loc == 1) { MainMenu.MainMenuInput(input); } if (loc == 2) { } } } |
I put it into multiple classes for my own sanity haha.
|
|
|
|
|