Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Konami Code in C#

Tags:

I am looking to have a C# application implement the Konami Code to display an Easter Egg. http://en.wikipedia.org/wiki/Konami_Code

What is the best way to do this?

This is in a standard C# windows forms app.

like image 336
Anthony D Avatar asked Jan 22 '09 16:01

Anthony D


People also ask

Can you use the Konami Code in Super C?

The codes were removed from the special Contra 4 version. Super C (NES) – Entering the code( → ← ↓ ↑ A B Start ) before the title screen appears, gives the player 10 lives in the US version of the game while in the Japanese version the code gives 30 lives.

What is the Konami code for PC?

In the PC version of the game the code is: UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, ESC, ENTER.

Is the Konami Code real?

The Konami Code originated as a cheat code—a sequence of button presses that unlocks secret features in a video game, usually making it easier to play. The first-ever game to feature the Konami Code was Gradius for the NES, published by Japanese third-party developer Konami in 1986.

What did the Konami code do?

Kazuhisa Hashimoto, the man who invented the "Konami code" cheat that became pervasive in video gaming and pop culture, has died. To use the code, players would press up, up, down, down, left, right, left, right, B, A and the Start button on the controller to make games easier.


1 Answers

In windows forms I would have a class that knows what the sequence is and holds the state of where you are in the sequence. Something like this should do it.

using System; using System.Collections.Generic; using System.Windows.Forms;  namespace WindowsFormsApplication3 {     public class KonamiSequence {          List<Keys> Keys = new List<Keys>{System.Windows.Forms.Keys.Up, System.Windows.Forms.Keys.Up,                                         System.Windows.Forms.Keys.Down, System.Windows.Forms.Keys.Down,                                         System.Windows.Forms.Keys.Left, System.Windows.Forms.Keys.Right,                                         System.Windows.Forms.Keys.Left, System.Windows.Forms.Keys.Right,                                         System.Windows.Forms.Keys.B, System.Windows.Forms.Keys.A};         private int mPosition = -1;          public int Position {             get { return mPosition; }             private set { mPosition = value; }         }          public bool IsCompletedBy(Keys key) {              if (Keys[Position + 1] == key) {                 // move to next                 Position++;             }             else if (Position == 1 && key == System.Windows.Forms.Keys.Up) {                 // stay where we are             }             else if (Keys[0] == key) {                 // restart at 1st                 Position = 0;             }             else {                 // no match in sequence                 Position = -1;             }              if (Position == Keys.Count - 1) {                 Position = -1;                 return true;             }              return false;         }     } } 

To use it, you would need something in your Form's code responding to key up events. Something like this should do it:

    private KonamiSequence sequence = new KonamiSequence();      private void Form1_KeyUp(object sender, KeyEventArgs e) {         if (sequence.IsCompletedBy(e.KeyCode)) {             MessageBox.Show("KONAMI!!!");         }     } 

Hopefully that's enough to give you what you need. For WPF you will need slight differences is very similar (see edit history #1).

EDIT: updated for winforms instead of wpf.

like image 126
Sam Meldrum Avatar answered Dec 23 '22 14:12

Sam Meldrum