1

我对代码中的 Keys 事件和计时器有点陌生,但由于某种原因,存在这个奇怪的错误。(阅读最后的编辑)

public partial class Form1 : Form
{
    bool start = false;
    Snake ormen = new Snake(10);
    short direction = 3;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Hide();
        timer1.Enabled = true;
        start = true;
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;
        if (start)
        {
            ormen.rita(g);
        }
    }
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Up:
                direction = 3;
                break;
            case Keys.Down:
                direction = 1;
                break;
            case Keys.Left:
                direction = 2;
                break;
            case Keys.Right:
                direction = 0;
                break;
            default:
                break;
        }
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        ormen.flytta(direction);
        Invalidate();
    }
}
}

我还尝试在关键事件中使用调试,但它甚至在该时间段之后才激活。谁能告诉我为什么?另外,如果有任何明显的错误,如果您能提及它们,我将不胜感激。我还尝试使计时器间隔更高,以检查程序是否跟不上但可以。(阅读最后的编辑)

[编辑] 我认为这不会有帮助,但这里是“Snake”类的代码。我怀疑那里有问题,但无论如何。

        private short diameter = 0;
    //0 = Right, 1 = Down, 2 = Left, 3 = Up
    public Snake(short diameter)
    {
        this.diameter = diameter;
        ormensPlatser.Enqueue(new Point(0, 0));
        ormensPlatser.Enqueue(new Point(0 + diameter, 0));
        ormensPlatser.Enqueue(new Point(0 + diameter + diameter, 0));
    }
    Queue<Point> ormensPlatser = new Queue<Point>();
    public void rita(Graphics g)
    {
        Point temp;
        for (int i = 0; i < ormensPlatser.Count; i++)
        {
            temp = ormensPlatser.Dequeue();
            g.FillEllipse(new SolidBrush(Color.Red), temp.X, temp.Y, diameter, diameter);
            ormensPlatser.Enqueue(temp);
        }
    }
    private void ormNäraKant(short direction,ref Point ormBit)
    {
        switch (direction)
        {
            case 0:
                ormBit.X = ormBit.X + diameter >= 380 ? 0 : ormBit.X + diameter;
                break;
            case 1:
                ormBit.Y = ormBit.Y + diameter >= 360 ? 0 : ormBit.Y + diameter;
                break;
            case 2:
                ormBit.X = ormBit.X - diameter < 0 ? 370 : ormBit.X - diameter;
                break;
            case 3:
                ormBit.Y = ormBit.Y - diameter < 0 ? 350 : ormBit.Y - diameter;
                break;
        }
    }
    public void flytta(short direction)
    {
        Point temp;
        int temp1 = ormensPlatser.Count - 1;
        for (int i = 0; i < ormensPlatser.Count; i++)
        {
            if (i == temp1)
            {
                switch(direction)
                {
                    case 0:
                        temp = ormensPlatser.Dequeue();
                        ormNäraKant(0, ref temp);
                        ormensPlatser.Enqueue(temp);
                        break;
                    case 1:
                        temp = ormensPlatser.Dequeue();
                        ormNäraKant(1, ref temp);
                        ormensPlatser.Enqueue(temp);
                        break;
                    case 2:
                        temp = ormensPlatser.Dequeue();
                        ormNäraKant(2, ref temp);
                        ormensPlatser.Enqueue(temp);
                        break;
                    case 3:
                        temp = ormensPlatser.Dequeue();
                        ormNäraKant(3, ref temp);
                        ormensPlatser.Enqueue(temp);
                        break;
                }
            }
            else
            {
                ormensPlatser.Dequeue();
                temp = ormensPlatser.Peek();
                ormensPlatser.Enqueue(temp);
            }
        }
    }
}

}

[EDIT2] 好吧,显然不是关于时间或类似的东西,代码有效,但我认为这与 slawekwin 在评论中所说的程序启动时的焦点有关。所以问题仍然是一样的,但是如果我 ALT+tab 到另一个程序然后回到蛇程序它就可以工作。

4

1 回答 1

0

所以我终于弄明白了,我认为问题出在定时器事件和键输入事件一起工作时,但我的假设是错误的。所以我所做的是,我检查了表单的所有属性,我发现了一个名为“KeyPrevied”的设置,它是假的,所以我尝试将它设置为真,它工作:)。

于 2012-11-16T19:40:20.380 回答