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();
}