0
votes

While trying to create a simple game where a square is manipulated via the keyboard keys, I have come across a small, rather irritating problem. I would like it to work so that when the opposite directional key is pressed, the character will stop; the character may move the other two directions while stopped in this situation.

This works perfectly with LEFT and RIGHT held down; the player may move UP or DOWN. If UP and DOWN are held down, however, the player will not move, nor will Java recognize that the LEFT or RIGHT keys were pressed.

import java.util.ArrayList;
import java.util.Random;

import org.lwjgl.*;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;

import static org.lwjgl.opengl.GL11.*;

public class Main {
    private Man p;
    private ArrayList<Integer> keysDown, keysUp;
    public Main() {
        try {
            Display.setDisplayMode(new DisplayMode(640, 480));
            Display.setTitle("LWJGLHelloWorld");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
        }

        p = new Man(0, 0);
        keysDown = new ArrayList<>();
        keysUp = new ArrayList<>();

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, 640, 480, 0, 1, -1);
        glMatrixMode(GL_MODELVIEW);

        while (!Display.isCloseRequested()) {
            glClear(GL_COLOR_BUFFER_BIT);

            checkKeys();

            p.draw();

            Display.update();
            Display.sync(60);
        }

        Display.destroy();
    }

    public void checkKeys() {
        ArrayList<Integer> keys = new ArrayList<>();
        keys.add(Keyboard.KEY_A);
        keys.add(Keyboard.KEY_D);
        keys.add(Keyboard.KEY_W);
        keys.add(Keyboard.KEY_S);

        for (int key : keys) {
            if (Keyboard.isKeyDown(key)) 
                keysDown.add(key);
            else 
                keysUp.add(key);
        }

        keysDown.removeAll(keysUp);
        keysUp = new ArrayList<>();

        int speed = 4;
        int dx = 0;
        int dy = 0;
        if (keysDown.contains(keys.get(2))) {
            System.out.println("keyUP");
            dy -= speed;
        }
        if (keysDown.contains(keys.get(3))) {
            System.out.println("keyDOWN");
            dy += speed;
        }
        if (keysDown.contains(keys.get(0))) {
            System.out.println("keyLEFT");
            dx -= speed;
        }
        if (keysDown.contains(keys.get(1))) {
            System.out.println("keyRIGHT");
            dx += speed;
        }
        //if (keysDown.contains(keys.get(0)) && keysDown.contains(keys.get(1))) dx = 0;

        //if (keysDown.contains(keys.get(2)) && keysDown.contains(keys.get(3))) dy = 0;
        p.update(dx, dy);

    }

    public static void main(String[] args) {
        new Main();
    }

    class Man {
        public int x, y, w, h;
        public float cR, cG, cB;

        public Man(int x, int y) {
            this.x = x;
            this.y = y;
            w = 50;
            h = 50;

            Random rand = new Random();
            cR = rand.nextFloat();
            cG = rand.nextFloat();
            cB = rand.nextFloat();
        }

        public void draw() {
            glColor3f(cR, cG, cB);
            glRecti(x,  y,  x+w,  y+h);
        }

        public void update(int dx, int dy) {
            x += dx;
            y += dy;
        }
    }
}

That is the code that I am working with. In addition, I am unsure how to compile an executable jar that is using the lwjgl library in addition to slick-util.

EDIT: I forgot to mention that with the current set-up: ASDW, it has problems. I also experienced these issues with JKLI and LEFT RIGHT UP DOWN. If you change it to keys in a line (ex: ASDF) it works as expected.

1
You may be running in to a hardware limitation of your keyboard. This answer describes it better than I can: gaming.stackexchange.com/questions/6669/…Talon876
That's what I was thinking, but with the keys set up as ASDF, it works as expected.AKrush95
Right, so if the code is proving that it works with some character sets and not others, it probably means that when it's not working, you're running in to keyboard limitations. I would try testing it on another keyboard, a PS/2 one if possible and see if you're still running in to problems.Talon876
You would suggest using a PS/2 keyboard..? Regardless, I'm on a laptop and that's going to be a little difficult because I can't compile my jar to work. I think it has to do with the native library.AKrush95
Yes, ps/2 keyboards support full n-key rollover. Though just another usb keyboard might be able to help troubleshoot whether or not it's actually a code problem or a hardware limitation.Talon876

1 Answers

1
votes

You're running in to a hardware limitation of your keyboard, which is described in the answer here.

You may want to try using another keyboard, a ps/2 one if possible because they allow full n-key rollover.