Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  New to Java  (Read 1568 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-04 02: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.

Check out my website!
Offline roland
« Reply #1 - Posted 2011-12-04 02: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); //key is in ascii
     } catch (IOException e)
      {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2011-12-04 02: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.

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roland
« Reply #3 - Posted 2011-12-04 02: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
{
       //do Stuff here
}


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.  Smiley

 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.











Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2011-12-04 02: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?

Check out my website!
Offline roland
« Reply #5 - Posted 2011-12-04 03: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.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-12-04 03: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. Smiley

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.

Offline roland
« Reply #7 - Posted 2011-12-04 03: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  Smiley
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #8 - Posted 2011-12-04 08: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
{
       //do Stuff here
}


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.  Smiley

 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.



Offline gouessej
« Reply #9 - Posted 2011-12-04 12:29:08 »

Hi

You should use java.util.Scanner.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #10 - Posted 2011-12-04 19: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.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #11 - Posted 2011-12-04 19:49:23 »

http://ra4king.is-a-geek.net/javadocs/java/util/Scanner.html

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #12 - Posted 2011-12-04 20: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.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2011-12-04 20:26:59 »

No Smiley

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #14 - Posted 2011-12-04 20: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.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #15 - Posted 2011-12-04 21:06:30 »

How about passing the String as a parameter?

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #16 - Posted 2011-12-04 21:08:00 »

I don't know what that means Cry

Can you copy my code and edit it to show me what that means?

Check out my website!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2011-12-04 21: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.");
}

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #18 - Posted 2011-12-04 21:18:27 »

Sad

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.

Check out my website!
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2011-12-04 21: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 Wink

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #20 - Posted 2011-12-04 21: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.

Check out my website!
Offline Kurten

Junior Member


Medals: 3
Exp: 3 years


-w-


« Reply #21 - Posted 2011-12-04 21: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
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11


Game Engineer


« Reply #22 - Posted 2011-12-04 22: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.).

See my work:
OTC Software
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #23 - Posted 2011-12-04 22: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() { // YELLOW-UNDERLINED

      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() { // YELLOW-UNDERLINED
     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() { // YELLOW-UNDERLINED
     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) { // YELLOW-UNDERLINED

      if (!running) {
         loc = 0;
      }

      if (loc == 1) {
         MainMenu.MainMenuInput(input);
      }
     
      if (loc == 2) {
         
      }
     
   }
}


I put it into multiple classes for my own sanity haha.

Check out my website!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!