Easily move a control using the KeyDown event

I am trying to make a simple game in C # using a Visual Studio Windows Form application. I want the user to be able to freely move the blue square up, right, down and left using the appropriate keys.

I use a timer that detects a new window location every 0.1 seconds, and a keydown event that actually changes the location of the field. The box should move in the appropriate direction while the key is being held.

My problem is that my current program is doing the job, except that when the user first presses the key, the field moves a few times and pauses for a moment before it continues to move. I want to make this block smoother from the first keystroke, without stopping in this way. It can be hard to explain in words, so I added a gif file.

enter image description here

Is there any way to fix this? Here is my current code.

private int posX, posY; //Both initialized in Form Load event

private void Form1_KeyDown(object sender, KeyEventArgs e)
{ 
    if (e.KeyCode == Keys.Up)
        posY -= 3;
    else if (e.KeyCode == Keys.Right)
        posX += 3;
    else if (e.KeyCode == Keys.Down)
        posY += 3;
    else if (e.KeyCode == Keys.Left)
        posX -= 3;
}

//Timer ticks every 0.1 second
private void Timer_Tick(object sender, EventArgs e)
{
    Box.Location = new Point(posX, posY);
    labelPosX.Text = posX.ToString(); //Testing purposes
    labelPosY.Text = posY.ToString(); //Testing purposes
}

I would like to use the KeyDown event to achieve this, but if there is a better or more common way actually used in real game worlds, I would also like to know about it!

+4
source share
1 answer

Keyboard.IsKeyDown Timer_Tick keydown.

:

double posX, posY;

private void Timer_Tick(object sender, EventArgs e)
{
    double velocity = /*(speed: pixels per seconds)*/ 100 * /*(timer tick time in seconds)*/ 0.003;

    if (Keyboard.IsKeyDown(Keys.Up))
    {
        posY -= velocity;
    }
    else if (Keyboard.IsKeyDown(Keys.Down))
    {
        posY += velocity;
    }
    //Also, don't put else here, so you can go diagonally.
    if (Keyboard.IsKeyDown(Keys.Left))
    {
        posX -= velocity;
    }
    else if (Keyboard.IsKeyDown(Keys.Right))
    {
        posX += velocity;
    }

    Box.Location = new Point((int)posX, (int)posY);
    labelPosX.Text = posX.ToString(); //Testing purposes
    labelPosY.Text = posY.ToString(); //Testing purposes
}

public static class Keyboard
{
    private static readonly HashSet<Keys> keys = new HashSet<Keys>();

    public static void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (keys.Contains(e.KeyCode) == false)
        {
            keys.Add(e.KeyCode);
        }
    }

    public static void OnKeyUp(object sender, KeyEventArgs e)
    {
        if (keys.Contains(e.KeyCode))
        {
            keys.Remove(e.KeyCode);
        }
    }

    public static bool IsKeyDown(Keys key)
    {
        return keys.Contains(key);
    }
}

Keyboard Form1 KeyDown KeyUp InitializeComponent.

KeyDown += Keyboard.OnKeyDown;
KeyUp += Keyboard.OnKeyUp;

, .

+4

Source: https://habr.com/ru/post/1691352/


All Articles