Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Cheat Code  (Read 555 times)
0 Members and 1 Guest are viewing this topic.
Offline 8bitooze

Senior Newbie





« Posted 2011-04-28 03:49:04 »

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:
1  
2  
3  
4  
5  
6  
7  
if(KeyJ == true)
{
   if(KeyA== true)
   {
      ...
   }
}

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

Sincerely,

8BitOoze

[size=3]8BitOoze[/size]
Offline ReBirth
« Reply #1 - Posted 2011-04-28 05:57:59 »

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.

Offline loom_weaver

JGO Coder


Medals: 16



« Reply #2 - Posted 2011-04-28 06:06:11 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dx4

Junior Member


Medals: 5



« Reply #3 - Posted 2011-04-28 12:01:17 »

in your class add

1  
StringBuffer keyboardString = new StringBuffer();


in keyPressed add

1  
2  
3  
4  
5  
6  
keyboardString.append(kEvt.getKeyChar());

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (54 views)
2013-05-17 21:29:12

alaslipknot (63 views)
2013-05-16 21:24:48

gouessej (94 views)
2013-05-16 00:53:38

gouessej (91 views)
2013-05-16 00:17:58

theagentd (101 views)
2013-05-15 15:01:13

theagentd (93 views)
2013-05-15 15:00:54

StreetDoggy (139 views)
2013-05-14 15:56:26

kutucuk (161 views)
2013-05-12 17:10:36

kutucuk (160 views)
2013-05-12 15:36:09

UnluckyDevil (170 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.082 seconds with 20 queries.