Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

Processing User Input

The LocationBeh behavior class deals with user input via the keyboard.The figure can be moved forward, back, left, right, up, or down, and turned clockwise or counterclockwise around the y-axis via the arrow keys. The code in LocationBeh is similar to the code in the TourControls class in Tour3D (from Chapter 18) but simpler in many cases since there's no viewpoint manipulation.

The figure reference is passed in through the constructor:

    // globals
    private Figure figure;
    private WakeupCondition keyPress;


    public LocationBeh(Figure fig)
    { figure = fig;
      keyPress = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
    }

If a key is pressed along with alt, then altMove() will be called; otherwise, standardMove() will be invoked. (An Alt-key pair is used to move the figure vertically up and down and slide it to the left and right.) Both move methods have a similar style:

 // global movement constants private final static int FWD = 0; private final static int BACK = 1; private final static int LEFT = 2; private final static int RIGHT = 3; private final static int UP = 4; private final static int DOWN = 5; private final static int CLOCK = 0; // clockwise turn private final static int CCLOCK = 1; // counter clockwise private void standardMove(int keycode) // moves figure forwards, backwards, rotates left or right { if(keycode == forwardKey) figure.doMove(FWD); else if(keycode == backKey) figure.doMove(BACK); else if(keycode == leftKey) figure.doRotateY(CLOCK); // clockwise else if(keycode ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page