Cheat Code

As most of you know i am working on my 8-bit style game and need help figuring out how to implement something. I want to implement an easter egg by having the user type in a certain word. However since i am creating a game, using a textfield would look out of place. So the person would need to enter the code while the game is running, in other words by pressing the correct keyboard letters to spell the secret code. public void keyPressed(KeyEvent e) only accepts on key at a time. I could use booleans for each key, but then it would require a lot of nested ifs not to mention it would decrease performance alot:


if(KeyJ == true)
{
   if(KeyA== true)
   {
      ...
   }
}


If someone could give me an elegent solution it would be greatly appreciated.

Sincerely,

8BitOoze

If it’s a cheat then there’s no need interface for it. GTA lets you type cheat but doesn’t let you know about what you pressed. Array can be used for this. Let the array filled by keyCode (it’s integer so easier) first and compare it to matched array to see if they have typed right.

Basically keep a sliding buffer (FIFO queue) of the last X key-presses.

During each key-press event, also check this buffer and if it matches your cheat code then enable your easter egg.

in your class add


StringBuffer keyboardString = new StringBuffer();

in keyPressed add


keyboardString.append(kEvt.getKeyChar());

if (keyboardString.equals("yourcheat")) {
  lives = 999999999; // or whatever..
  keyboardString = new StringBuffer();
}