Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Input and Output: How to Receive and Present Information in a Java Game (Basics)

A topic by cloudyheavengames created Jun 06, 2018 Views: 550 Replies: 2
Viewing posts 1 to 3
Host

At the heart of every game is user interaction between the player and the game.  The basis of this interaction is an input/output cycle.  Input is when a user provides information to the software.  For example, types of player input could include pressing a directional control pad or using a joystick to move the player character, clicking a button to fire a weapon, selecting dialog choices from a menu, or entering text to name the player character.  In turn, the game or software can produce output in the opposite direction, providing information or game results to the player.  Examples can include displaying non-player dialog, informing the player about battle results, and playing sound effects to alert the player about a nearby enemy.  

You can see that a game is a give-and-take relationship between the software and the player.  We can also think of several types of input devices and methods, which vary depending on the game and the platform (or system).  Some possibilities include:

  • Keyboard: The keyboard is a standard input method for computer games.  In addition to text input, the keyboard can be used to control player actions and other functionality, such as the game camera. 
  • Touch-screen: Touch-screens are a primary input method for games on smartphones and tablets.  The Nintendo DS also has a touch-screen, which is paired with a stylus.  For smart phones and tablets, games might also have an onscreen keyboard to replace the physical ones from computers.
  • Mouse and pointer: If you are using a computer, you will probably use a mouse to point and click or even move, combined with a keyboard.
  • Gamepad/controller: Most game consoles come with some sort of gamepad or controller.  At the least, there are usually some buttons, and there may also be analog sticks (similar to mini-joysticks) and shoulder buttons for additional controls.
  • Joystick: A common sight in arcades, the joystick is the common input device for arcade cabinets, but also some computer games can be controlled by a plug-in joystick.
  • Microphone: Some games, such as several Legend of Zelda games on Nintendo DS, allow the player to use sound and breath to provide input and meet challenges.
  • Motion sensors: Systems such as the Nintendo Wii and Microsoft Kinect use devices that detect player motion and react accordingly.  Smartphones and tablets might also use accelerometers to track changes in device height, angle, and other motion variables.
  • Other specialized devices: Some games have specific special input devices.  For example, the original NES system had a light gun for games such as Duck Hunt.  Popular arcade game Dance Dance Revolution required a foot pad to process players' dance steps, and Guitar Hero provides a special guitar replica device. 

As far as output, there are several common means and devices, such as:

  • Screen: Probably the most common output device for video games.  The game can present text and graphics to the player.  However, some games might not need a screen.  For example, they might be based on audio.
  • Speakers: Game sound effects, and even music, are an important way for the game to convey information to players.  Some visually-impaired players have even been able to enjoy mainstream games using just sound alone (for example: https://kotaku.com/5766791/how-a-blind-man-plays-video-games).  With devices such as Alexa, as well as screen readers and other software, there are surely plenty of opportunities to make audio-based games.
  • Controllers: Some systems include controllers that implement vibration, based on what's happening in the game.  For example, if there is an in-game explosion, the controller might vibrate, and the vibration intensity could vary, depending on how close the player is to the explosion.

One of the first things you see in many games is an introduction screen and an instruction to press a button to start.  Then there might be some sort of backstory text to introduce you to the game's context.  This approach was particularly true with older games, before technology allowed long, elaborate cut scenes.  For example, have a look at the introduction for the original Legend of Zelda game on the NES: https://www.youtube.com/watch?v=CcCAYwh_05I

Host

In Java, it's pretty easy to get simple input from a keyboard and output to the computer screen.  It might seem simple, but this functionality is a building block for more interesting games and interactions.  Right now, let's focus on output to start.   

Hopefully, you went through the post about running your first Java program.  If you haven't, go back and do that step, and then come back to this post.  From this point, I'm assuming that you have either set up an IDE or have been able to use a text editor and command line to run a program. 

How Do You Output Text in Java?  

To output text, we use a method called System.out.println().  We'll discuss methods very soon.  The usage looks like this:   System.out.println("Your output text here");  

Pay close attention to the case, the System part should always be capitalized.  The case does not matter for the text in between the quotation marks.  

Simply replace the code text in between the parentheses (which says "Your output text here") with whatever text you want.  Make sure that your output text is all between quotation marks and inside the parentheses, and that you end the line with a semicolon after the closing parenthesis.  

So now we can write a very simple program asking the player to press a button.  Let's look at some short example code.  Have a look at the code I posted at https://codepad.co/snippet/AR6xKxde.  If you want to run it yourself, copy and paste it into an empty file on your computer, and name the file GameIOExample.java.   

You'll notice on lines 7 and 14, we have our System.out.println("…"), and then in between the parentheses, there's quotation marks and text.  If you run the program, you'll see that everything in between those parentheses is output as text to the screen.  It's that simple to output basic text!  Try the following:  

  • Change the text in between the quotation marks, run the program, and see how the output changes.
  • Add another System.out.println() statement, after line 6 but before line 9, and put whatever text you want between the parentheses.  Don't forget to put the text between quotation marks, and don't forget to put a semicolon after the closing parenthesis.
Host

How Do You Get Keyboard Input in Java?  

Ok, so for now, I'm assuming you're using a keyboard, we'll talk about how to get keyboard input.  In Java, we need to use something called a Scanner object (we'll talk about what objects are very soon as well).  First, at the top of the program, let's be sure to include this line:  

import java.util.Scanner;  

Next, we need to set up and create the object, which looks something like this:  

Scanner inputReader = new Scanner(System.in);  

Scanner is the type of object we're creating, and we're giving it a name of inputReader in this example.  You can call it something different if you want, and in fact, in the code snippet, we've called it readInput.  Giving it a name allows us to call and use it later in the code.  Note that we need to create and name the object before we can use it, otherwise we'll get an error when the program runs.

The new Scanner part of the line actually creates the new Scanner object, and the System.in part in between the parentheses tells us that the Scanner will read input from System.in, which in this case, represents the user's keyboard.   

So taken all together, in plain English, we can say that this line:

Scanner inputReader = new Scanner(System.in);

is basically saying "We're creating a new Scanner object called inputReader, which will check for input from the player's keyboard."  

After we set up a Scanner object, we need to actually use it to read input from the user.  Let's say that we want to get a line of text that the player enters, such as a player name, for example.  We could do something like this:

String playerName = inputReader.nextLine();  

A String is a type of Java object that represents text.  Remember when we used System.out.println() to output text to the screen?  Everything in between the quotation marks was a String object.

Our example line above, in plain English, means "Create a String object called playerName, and use it to store the next line that the player types on the keyboard."  A line of keyboard input ends when the player presses the Enter key, and at that point the Scanner object will read that line.  

Again, since we named the String object, we can use it later in the program if we want to.  For example, we can print out a welcome message:

System.out.println("Welcome to the game, " + playerName);  

We're using System.out.println() again, but now we see a + sign.  This will print out the first part between the quotation marks, which is a String, and then it will add whatever we entered for playerName.  Everything between the quotation marks is called a "string literal," because the program will print it out exactly as it's shown.  playerName is a variable, that represents a String value that can be changed.  Remember in math how you had variables called x and y, where you didn't necessarily know the value at first, but you knew it had to be a number?  Variables in Java are the same basic principle.  So since both "Welcome to the game, " and playerName are both Strings, we can use the + sign to combine, or concatenate, them together.  If the player had typed in the name "Terra" for playerName, then the output would be "Welcome to the game, Terra".  

We will be talking more about variables soon.  But for now, this is one basic way to work with input and output in Java.   

Now let's look back at the code snippet I posted at https://codepad.co/snippet/AR6xKxde.  I've put in a few lines of code that wait for the player to press the Enter key.  In the code snippet, on line 9, you can see that I've set up my Scanner object, which I've named readInput.  We then have a String called keyPress, which waits for the player to type something, then press the Enter key.  There's a bit of a trick here on line 12.  In some games, you might want the player to just press Enter, and not worry about any other input.  On line 12, the player didn't enter any text, but just pressed Enter, then the keyPress String should be empty, or "".  Notice that there is NO SPACE or anything in between the quotation marks, it's just an empty String.  Whitespace counts as part of a String, so if there's a space there, let's say that the player hit the space bar before pressing Enter, it's not an empty String.  Line 12 checks to see if the player entered an empty String, and if the String is empty, the program prints a nice intro.  Else, on line 15, the program will let the player know about following instructions :)