当我释放方向键之一时,播放器停止移动

时间:2019-09-19 13:05:09

标签: c# winforms 2d-games

我一直在尝试使用Winforms为大学编写此多人游戏,我知道它非常不便,但我们必须这样做。对于玩家的移动,我在this.KeyDown方法中使用了Form1,因此它可以记录Form1打开时进行的所有点击。

 public Form1()
    {
        InitializeComponent();

        this.KeyPreview = true;

        this.KeyDown += Form1_KeyDown;           

    }

很奇怪,因为它是按钮释放顺序,所以会出现此问题。例如:如果我按下按钮UP-RIGHT,播放器将向对角移动,并且如果我释放第一按的按钮UP,则播放器开始向右移动,但是如果我释放第二按的按钮RIGHT,则播放器应开始向右移动UP,但是它停止了。你能帮我解决这个问题吗?

这是Form1_KeyDown()方法的代码:

        private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {

        if (Keyboard.IsKeyDown(Key.Up) && Keyboard.IsKeyDown(Key.Right))
        {
            location.X += 4;
            location.Y -= 4;
            richTextBox1.AppendText("Up Right \n");
        }
        else if (Keyboard.IsKeyDown(Key.Up) && Keyboard.IsKeyDown(Key.Left))
        {
            location.X -= 4;
            location.Y -= 4;
            richTextBox1.AppendText("Up Left \n");

        }
        else if (Keyboard.IsKeyDown(Key.Down) && Keyboard.IsKeyDown(Key.Left))
        {
            location.X -= 4;
            location.Y += 4;
            richTextBox1.AppendText("Down Left \n");

        }
        else if (Keyboard.IsKeyDown(Key.Down) && Keyboard.IsKeyDown(Key.Right))
        {
            location.X += 4;
            location.Y += 4;
            richTextBox1.AppendText("Down Right \n");

        }
        else if (Keyboard.IsKeyDown(Key.Right))
        { location.X += 4;
            richTextBox1.AppendText("Right \n");
        }
        else if (Keyboard.IsKeyDown(Key.Left))
        { location.X -= 4;
            richTextBox1.AppendText("Left \n");
        }
        else if (Keyboard.IsKeyDown(Key.Up))
        { location.Y -= 4;
            richTextBox1.AppendText("Up \n");
        }
        else if (Keyboard.IsKeyDown(Key.Down))
        { location.Y += 4;
            richTextBox1.AppendText("Down\n");
        }


        PlayingField_Paint();
        DrawPlayer(location, Color.Red);           
    }

0 个答案:

没有答案