Question

I'm trying to implement a cheat code for a simple paddle game, the cheat should ONLY activate when the specific order of key is pressed: UP, UP, DOWN, DOWN, LEFT, LEFT, RIGHT, RIGHT. I'm trying, but I can't get it!!

    if(up >= 2){
        if(down >= 2){
            if(left >= 2){
                if(right >= 2){
                    cheat = true;
                    g.setColor(0x00FF0000);
                    g.fillRect(x, y, canvas.getWidth(), 5);
                    g.fillRect(x, y - 5, 5, 5);
                    g.fillRect(canvas.getWidth() - 5, y - 5, 5, 5);
                    x = 0;
                    width = canvas.getWidth();
                    height = canvas.getHeight();
                }else{
                    g.setColor(0x00FF0000);
                    g.fillRect(x, y, width, height);
                    g.fillRect(x, y - height, height, height);
                    g.fillRect(x + width - height, y - height, height, height);
                    System.out.println(up + " " + down + " " + left + " " + right + "right");
                }
            }else{
                g.setColor(0x00FF0000);
                g.fillRect(x, y, width, height);
                g.fillRect(x, y - height, height, height);
                g.fillRect(x + width - height, y - height, height, height);
                right = 0;System.out.println(up + " " + down + " " + left + " " + right + "left");
            }
        }else{
            g.setColor(0x00FF0000);
            g.fillRect(x, y, width, height);
            g.fillRect(x, y - height, height, height);
            g.fillRect(x + width - height, y - height, height, height);
            left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "down");
        }
    }else{
        g.setColor(0x00FF0000);
        g.fillRect(x, y, width, height);
        g.fillRect(x, y - height, height, height);
        g.fillRect(x + width - height, y - height, height, height);
        down = 0; left = 0; right = 0;System.out.println(up + " " + down + " " + left + " " + right + "up");
    }

Program is being updated every 50ms. The up, left, down, right are the keylisteners, when those keys are pressed, it gets added like a counter. Ignore the whole g.fillRect and g.setColor and the System.out(was trying to see how it acted and how to solve the problem on my own). When the user doesn't enter the correct order of keys, the counters should reset, but since the update is every 50ms, it screws up the counter.

Is there a way to detect a specific order of keypresses or it's just not possible?

Était-ce utile?

La solution

You can do something like this

int cheat = 0; //keeps track of how many were pressed in order

if (cheat == 0 && upButtonPressed)
    cheat++;
elseif (cheat == 1 && upButtonPressed)
    cheat++;
elseif(cheat == 2 &&  downButtonPressed)
    cheat++;
//rest of the order
else
    cheat = 0;

if (cheat == 8) //or however many buttons need to be pressed
    //do whatever you want to happen when the cheat is activated

Autres conseils

You should create an Object that takes a KeyEvent every time a button is clicked. The object will keep track of the current List of keys that have been pressed, in order, and will also check the stack every time it is added to to see if the List matches the pattern you provide. Don't forget to clear the stack if the user inputs a key that doesn't match the pattern!

I would make two arrays with the key-codes. One for the game action codes, and one for the corresponding key codes. (Because you'll want to offer an alternative way of inputting the key combination on the phones that don't have a d-pad or joystick).

int[] keys1 = {getKeyCode(UP), getKeyCode(UP), getKeyCode(DOWN), getKeyCode(DOWN), getKeyCode(LEFT), getKeyCode(LEFT), getKeyCode(RIGHT), getKeyCode(RIGHT)};
int[] keys2 = {KEY_NUM2, KEY_NUM2, KEY_NUM8, KEY_NUM8, KEY_NUM4, KEY_NUM4, KEY_NUM6, KEY_NUM6};
int keyIterator = 0;

public void keyPressed(int kc) {
 if (kc == keys1[keyIterator] || kc == keys2[keyIterator]) { // Correct key
  keyIterator++;
  if (keyIterator > keys1.length) {
   // keyIterator has reached the length of the array, meaning the secret key combination has been entered successfully.
  }
 } else { // Wrong key. Start over
  keyIterator = 0;
 }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top