origin origin2
origin origin2

Reputation: 73

C# pressing key breaks movement of an object

I'm creating ping pong game. I managed to move both lines (picture box) at same time if keys for movement are pressed down. Problem is that if control for one player is pressed down and then other player JUST clicks (1 time) it breaks movement of other player,so that he needs to press key again. I tried to fix it with keypress and Keyboard.IsKeyDownbut no luck.

Here is my code:

public void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        Keys up1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor1.Text , true);
        Keys down1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol1.Text , true);

        Keys up2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor2.Text, true);
        Keys down2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol2.Text, true);

        if (e.KeyCode == Keys.Escape)
            Application.Exit();

        if(e.KeyCode == up1)
        {
            goup1 = true;
        }
        if (e.KeyCode == down1)
        {
            godown1 = true;
        }
        if (e.KeyCode == up2)
        {
            goup2 = true;
        }
        if (e.KeyCode == down2)
        {
            godown2 = true;
        }
        igra1();
    }

private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        Keys up1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor1.Text, true);
        Keys down1 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol1.Text, true);

        Keys up2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_gor2.Text, true);
        Keys down2 = (Keys)Enum.Parse(typeof(Keys), form1.p5_text_kontrole_dol2.Text, true);

        if (e.KeyCode == up1)
        {
            goup1 = false;
        }
        if (e.KeyCode == down1)
        {
            godown1 = false;
        }

        if (e.KeyCode == up2)
        {
            goup2 = false;
        }
        if (e.KeyCode == down2)
        {
            godown2 = false;
        }
        igra1();
    }

public void igra1()
    {
        if (goup1)
        {
            if (form1.p6_ploscek1.Top > form1.panel6_pongIgra.Top)
                form1.p6_ploscek1.Top -= 15;
        }
        if (goup2)
        {
            if (form1.p6_ploscek2.Top > form1.panel6_pongIgra.Top)
                form1.p6_ploscek2.Top -= 15;
        }
        if (godown1)
        {
            if (form1.p6_ploscek1.Bottom < form1.panel6_pongIgra.Bottom)
                form1.p6_ploscek1.Top += 15;
        }
        if (godown2)
        {
            if (form1.p6_ploscek2.Bottom < form1.panel6_pongIgra.Bottom)
                form1.p6_ploscek2.Top += 15;
        }
    }

Upvotes: 2

Views: 196

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

I believe you're relying on the fact that keys get repeated by Windows while you hold them down to make your game pieces move. The first key being held down will stop repeating because the new key being held down is repeating instead.

To fix this move the pieces in the Tick() event of a Timer control. In the KeyDown/KeyUp events simply change the state of a variable that represents whether the associated piece should be moving (and what direction it should go). The Timer code will look at the state variables and act accordingly...

Upvotes: 0

Related Questions