Game Controls

Enter and Arrow Keys



1. The game will change states each time the ENTER key is pressed. This means the GamePanel class must implement the KeyListener interface (and all its unimplemented methods). The GamePanel class should now look like this:

public class GamePanel extends JPanel 
                    implements ActionListener, KeyListener{  }

2. In the keyPressed() method, if ENTER is pressed, we want the game to change to the next state as shown below:

if (e.getKeyCode()==KeyEvent.VK_ENTER) {
    if (currentState == END) {
        currentState = MENU;
    } else {
        currentState++;
    }
}   

3. We also want to be notified if any of the arrow keys are pressed, since they will eventually be controlling the movement of our game character. For now, just add print statements to verify that the arrow keys are "working". See below for an example of how this would work for the "up arrow" key. NOTE: Only check the arrow keys when the game is in GAME state.

if (e.getKeyCode()==KeyEvent.VK_UP) {
    System.out.println("UP");
}

4. Add code for the other 3 arrow keys on the keyboard (DOWN, LEFT, RIGHT).


LeagueInvaders

5. In the LeagueInvaders class. Add a KeyListener to your JFrame. The GamePanel object is the listener, so the code might look something like this:

frame.addKeyListener(panel);

TESTING

6. Run your program. Every time the ENTER key is pressed, you should see the game window change color (blue, black, red, blue, black, red, etc.) When the window is red, you should now see your END game text. Each of the arrow keys should print a message to the console (the timer events are still printing "action").
Do not continue until you have this working