科乐美code在C#科乐美、code

2023-09-02 01:52:48 作者:Agonie(折磨)

我期待有一个C#应用程序中实现了小浪code显示复活节彩蛋。 http://en.wikipedia.org/wiki/Konami_$c$c

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?

这是一个标准的C#Windows窗体应用程序。

This is in a standard C# windows forms app.

推荐答案

在Windows窗体我有知道的顺序是什么,拥有你在哪里序列中的状态的类。像这样的东西应该这样做。

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;
        }
    }
}

要使用它,你需要的东西在你的窗体的code响应键上的事件。像这样的东西应该这样做:

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!!!");
        }
    }

我希望这足够给你你所需要的。对于WPF,你需要细微的差别非常相似(见编辑历史#1)。

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

编辑:更新的WinForms,而不是WPF

updated for winforms instead of wpf.