How do I move the paddle with the arrow keys?

I am making a pong game using gdi+ and c#. I want to move the paddle using the arrow keys by using the move variable, but it does not work; the paddle does move when I use the int move=0; global variable.

I am thinking about nesting my methods.

Is it possible to nest the Form1_Paint into the Form1_Keypress method?

    int move = 0;

public void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)56)//key 8
{
move--;
}

if (e.KeyChar == (char)50)//key 2
{
move++;
}
}

public void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush blueBrush = new SolidBrush(Color.Blue);
SolidBrush redBrush = new SolidBrush(Color.Red);
SolidBrush whiteBrush = new SolidBrush(Color.White);
Rectangle rect1 = new Rectangle(0, 250, 35, 100); //blue paddle
Rectangle rect2 = new Rectangle(750, 250 + move, 35, 100); //red paddle
Rectangle rect3 = new Rectangle(390, 290, 10, 10); //white ball
g.FillRectangle(blueBrush, rect1);
g.FillRectangle(redBrush, rect2);
g.FillRectangle(whiteBrush, rect3);
g.Dispose();
}


This is likely because there is nothing that triggers the paint event.

So in your code the paddle variable is updated by the keypesses, but since there is no ‘gameloop’ the redraw is not triggered and thus the movement is not shown.

There are two ways around this:

1. Trigger the repaint event by ‘invalidating’ the form or calling the InvalidateRgn(hWnd, NULL, TRUE); This way the normal eventing of the form handles the drawing updates.

2. Set up a renderthread- a more complex way but better in general. This gives you best control over the drawing aspect of your game. There are some samples out there to explain in more detail like this.

So- if you go for option one, you could do a redraw with each keypress, or... You would need to set a timer anyway for other parts of the game (ball, AI) and handle the repaint trigger there.

• I added the Invalidate() command and it worked, I have got the paddle to move. – gamer67 Dec 23 '18 at 3:18
• You might mark the answer as the accepted solution? – Felsir Dec 23 '18 at 8:45
• I used the invalidate() command to move my paddle and ball. – gamer67 Dec 25 '18 at 20:52

You have no logic to actually change the position of the paddles. You just create them, give them a colour and paint them to the screen.

• It certainly looks like they have code modifying a move variable based on key presses, and are using that move variable to position where the red paddle is drawn. Can you be more precise about what's missing that would be needed to see the effect of this positioning offset? – DMGregory Dec 20 '18 at 10:58
• My bad. When I first read this code I somehow didn't see the move variable here :( Rectangle rect2 = new Rectangle(750, 250 + move, 35, 100); – RianF2 Dec 21 '18 at 19:20
• how do I implement the logic to change the position of the paddles. – gamer67 Dec 22 '18 at 21:52

You could try using arrow keys: http://net-informations.com/q/faq/arrowkeys.html

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
//capture up arrow key
if (keyData == Keys.Up )
{
move--;
MessageBox.Show("You pressed Up arrow key");
return true;
}
//capture down arrow key
if (keyData == Keys.Down )
{
move++;
MessageBox.Show("You pressed Down arrow key");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

• How would this improve the ability to move the paddles over the current code that uses the 2 & 8 numpad keys? If the movement isn't visible on screen using those keys, how would changing to the arrow keys solve that problem? – DMGregory Dec 20 '18 at 11:01
• Maybe he has numlock off. Arrow keys sound like a better idea. WASD may be better yet. – Jaybird Dec 21 '18 at 22:49

well here is my code to use the arrow keys

        int move = 0;

public void Form1_KeysDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)//key up
{
move--;
MessageBox.Show("Key Press");
}

if (e.KeyCode == Keys.Down)//key down
{
move++;
MessageBox.Show("Key Press");
}
}