Removing the delay after KeyDown event?

前端 未结 3 877
野的像风
野的像风 2020-11-29 13:32

When I hold a key in my game to move my player:

public void MainForm_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        Pla         


        
3条回答
  •  忘掉有多难
    2020-11-29 14:13

    I was looking for solutions to create a small copy of Flappy Bird. Perhaps my decision will help someone. I used the addition of a player position with a variable in timer to simulate gravity. When I press W, I turned off the further reception of the command using bool and simply inverted gravity

    private void time_Tick(object sender, EventArgs e)
        {
            if (bird.Location.Y >= 540)
            {
                bird.Location = new Point(bird.Location.X, 540);
            }
            else 
            {
                bird.Location = new Point(bird.Location.X, bird.Location.Y+grav);
            } 
        }
    
    private void Form1_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.W && press == false)
            {
                press = true;
                grav = -10;
            }
            return;
        }
    
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.W && press == true)
            {
                press = false;
                grav = 5;
            }
            return;
        }
    

提交回复
热议问题