Obtaining Input

Allowing for the player’s input is arguably the single-most important aspect of the game. Without input, the game is just a dazzling implementation of a screen saver. Three main types of input devices can be used. These devices are the keyboard, mouse, and game controller or joystick. The most common of these devices is the keyboard, which along with the mouse come pretty commonly with all computers. The Java platform already contains several utilities that allow the user to capture input from the mouse and keyboard for applications and applets written on the platform in the form of listeners. If you are using the AWT event model in your game, the MouseListener, the KeyListener, or the more generic EventListener classes should be implemented.

These classes work just as they would normally be expected to in a typical Java application or applet. If the game in question requires joystick or gamepad input, a number of useful classes are handled by the Jinput API discussed in Chapter 11, “Java Native Interface.” This section gives a quick overview of the classes most commonly used for obtaining keyboard and mouse input.

The KeyListener Interface

The KeyListener interface listens for keyboard input from the user. Depending on how the game is set up, this input might be the most common that comes into your game. Three methods are available when using the KeyListener interface—keyPressed, keyReleased, and keyTyped. Each of these methods takes a KeyEvent as its argument.

The KeyEvent class allows the program to analyze the actual key that was selected by using a set of virtual key (VK) codes. The VK codes cannot be generated on every keyboard, so be careful when determining input from them. The majority of them are available and listed in the Java documentation. If the program must know the actual character (for example, for a high-score table initial entry) use the getKeyChar method. This method returns the actual character pressed.

A quick code block indicating the basic use of the KeyListener interface follows:

public void keyPressed(KeyEvent e) {   int keyCode = e.getKeyCode():   if(keycode == KeyEvent.VK_W)   System.out.println("You pressed up!"); } //This one is empty, but must be defined public void keyReleased() {} public void keyTyped(KeyEvent e) { char key = e.getKeyChar();     switch(char)   {     case’r’:     {      System.out.println("You typed an R");     break;       }   } }

The MouseListener Interface

The mouse is also a great way to take input for a game. This listener is required to implement the following methods: mouseClicked, mouseEntered, mouseExited, mousePressed, and mouseReleased. These methods take a MouseEvent object to determine the result of the given input. When using listener interfaces, always remember to implement each of the methods unless the class is abstract, in which case you are just pushing the work to a later point in time.

A small code block of working with a MouseListener interface follows:

public void mouseClicked(MouseEvent e) { if(e.getModifiers()== MouseEvent.BUTTON1_MASK) {   //Fire the main weapons! } } public void mousePressed(MouseEvent e) { System.out.println("The mouse is at "+e.getX()+" "+e.getY()); }

These are just two of the numerous types of listener classes. When working with an AWT model of input, you will find they are reliable and allow players to get more immersed in your game.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net