0
\$\begingroup\$

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();
    }
\$\endgroup\$
0

4 Answers 4

3
\$\begingroup\$

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.

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

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Commented Dec 20, 2018 at 10:58
  • \$\begingroup\$ 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); \$\endgroup\$
    – RianF2
    Commented Dec 21, 2018 at 19:20
  • \$\begingroup\$ how do I implement the logic to change the position of the paddles. \$\endgroup\$
    – gamer67
    Commented Dec 22, 2018 at 21:52
0
\$\begingroup\$

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);
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – DMGregory
    Commented Dec 20, 2018 at 11:01
  • \$\begingroup\$ Maybe he has numlock off. Arrow keys sound like a better idea. WASD may be better yet. \$\endgroup\$
    – Jaybird
    Commented Dec 21, 2018 at 22:49
0
\$\begingroup\$

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");
        }
    }
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .