How to Build Simple Java Games

By Fred Larrey

If you're a beginning Java programmer and would like to write a game, it's best to start as simple as possible. Text-based games are the simplest genre of computer games. They allow for a lot of imagination and creativity from both the developer and player, and allow you to get accustomed to working with user interaction in Java. In order to create a Java game, you will first need to have Java properly installed on your computer and an editor to write your code in. Luckily, there is a free program available that does both: the Eclipse editor for Java Developers. We will be using Eclipse to create our Java project.

Things You'll Need

  • Eclipse for Java Developers IDE

Step 1

Navigate to the link in the Resources section and download and install the \"Eclipse for Java Developers IDE (91 MB)\".

Step 2

Brainstorm some ideas for your text-based game. Your limitations are the following: the computer will output some text to the player (such as \"you've entered a room, you can go left or right\"). Then the player will have a chance to respond by inputting text (\"left,\" for example). The computer program can then respond, and the dialog can go back and forth like that until you'd like the game to end. Write out some plot ideas, and keep in mind that the idea is to keep the player interested and having fun.

Step 3

Open Eclipse. Click \"File,\" \"New,\" \"Java Project.\" Type \"SimpleGame\" as the project name and then click \"Finish.\"

Step 4

Double click the SimpleGame folder in the Package Explorer, located on the left toolbar, and click \"New,\" \"Class.\"

Step 5

Type the name of your game (without spaces) in the \"Name\" box. We'll call our sample game, \"HauntedMansion\". Under \"Which method stubs would you like to create?\" click the \"public static void main(String[] args)\" check box, then click \"Finish.\"

Step 6

Select the whitespace above \"public class HauntedMansion\" and type the following line of code:import java.util.Scanner;. The Scanner will help us read what the user inputs to the console.

Step 7

Select some whitespace inside of \"public static void main(String[] args){\" and type the following two lines of code:String choice;Scanner user_in = new Scanner(;The user_in Scanner will read what the user types into the console, and dump it into the \"choice\" variable.

Step 8

To write to the console, we'll use the \"System.out.println()\" method. This means any time you want the computer to tell the player the current state of the game, you will call this function. In a new line type:System.out.println(\"Welcome to the Haunted Mansion. You see an expansive staircase ahead you and rooms to your left and right.\" +\"\\n\"+\"Type 'upstairs' and hit enter to go up the staircase, 'right' to go to the room on your right,\" +\"\\n\"+\"or 'left' to go to the room on your left.\");You can of course change what is inside of the println statement to whatever you'd like.

Step 9

In order to read what the user types in, use the user_in's nextLine() method and put it into the \"choice\" string, by typing the following code under the last line:choice = user_in.nextLine();Now the choice variable contains whatever the user typed in before they hit enter.

Step 10

Use a conditional statement to find out what the user typed in, by typing the following: if(choice.equals(\"upstairs\")){ System.out.println(\"You are now upstairs.\"); } else if(choice.equals(\"right\")){ System.out.println(\"You are now in the kitchen\"); } else if(choice.equals(\"left\")){ System.out.println(\"You are now in the dining room\"); } else { System.out.println(\"That was not valid\"); }

Step 11

Continue using Steps 8 through 10 to create as intricate a game as you'd like by adding more System.out.Println()'s from the computer and more possible responses from the player.