0

For my Space Invaders game in C# WinForms I have this code which moves the player's cannon depending on their arrow key input:

void Game_Screen_KeyDown(object sender, KeyEventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            if (Form1.lives != 0)
            {
                if (e.KeyCode == Keys.Left)
                {
                    cannonBox.Location = new Point(cannonBox.Left -= 2, cannonBox.Top); //Changes location of cannonBox to a new location to the left
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(10);
                }

                else
                if (e.KeyCode == Keys.Right)
                {
                    cannonBox.Location = new Point(cannonBox.Left + 2, cannonBox.Top); //Changes location of cannonBox to a new location to the right
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(10); //Delays the movement by couple milliseconds to stop instant movement
                }
            }

        }
    }

However, when the cannonBox reaches the form's boundaries it just keeps going and I would like to make it bounce off and go towards the other direction. I thought and tried of using something like this but it would be really difficult to find the precise location from where it intersects with:

if (cannonBox.Location == new Point(763, 50))
{
     for (int i = 0; i < 50; i++)
            {
                cannonBox.Location = new Point(cannonBox.Left - 2, cannonBox.Top);
            }
     Application.DoEvents();
     System.Threading.Thread.Sleep(10);
}
  • there are several issues in your code. you should not use for-loop like so to respond to user key inputs, just use key events for that. second, what you mean exactly by "when the cannonBox reaches the form's boundaries it just keeps going and I would like to make it bounce off and go towards the other direction"? if user keeps left key, you usually may keep it at left of form (zero x location), not moving to right. yes? – S.Serpooshan Jan 13 '18 at 11:18
  • @S.Serp but when I don't use the for loop, the movement is not that smooth. Moreover, when the cannonBox reaches the bounds of the Form, it just goes off and I don't want the user to do that –  Jan 13 '18 at 11:28
  • @wakiwaki why not to continue the movement and make the picture box to come from the other side of the form? a lot of games use that behavior.. – Jonathan Applebaum Jan 13 '18 at 11:30
  • @jonathana how would I be able to do that? I'm quite new to C# –  Jan 13 '18 at 11:30
  • `if (picturebox.Left<0) picturebox.Left=0;` or `picturebox.Left=Form1.Width-picturebox.Width;` – S.Serpooshan Jan 13 '18 at 11:38
  • @wakiwaki please see my answer with example – Jonathan Applebaum Jan 13 '18 at 11:55

1 Answers1

1

For your request here is an example how to continue the movement and make the picture box to come from the other side of the form.
another thing is as @S.Serp commented, its better not to use for loops for that task, but i guess your code is for learning purpose.
also:
1. it is better not to use Application.DoEvents() it can cause problems (https://stackoverflow.com/a/5183623/5718868). read about async await keywords in C# and use it instead of Application.DoEvents()
2. dont hard code the form / screen use a variable - screenSize in my example.

public Size screenSize;
private void Game_Screen_Load(object sender, EventArgs e)
{
    screenSize = this.Size;
}



private void Game_Screen_KeyDown(object sender, KeyEventArgs e)
{
    for (int i = 0; i < 100; i++)
    {

        if (Form1.lives != 0)
        {
            if (e.KeyCode == Keys.Left)
            {

                if (cannonBox.Location.X < 0)
                {
                    cannonBox.Location = new Point(cannonBox.Left = this.Width, cannonBox.Top);
                }

                cannonBox.Location = new Point(cannonBox.Left -= 2, cannonBox.Top); //Changes location of cannonBox to a new location to the left
                Application.DoEvents();
                System.Threading.Thread.Sleep(10);
            }

            else
            if (e.KeyCode == Keys.Right)
            {

                if (cannonBox.Location.X + cannonBox.Width > screenSize.Width)
                {
                    cannonBox.Location = new Point(cannonBox.Left = 0 - cannonBox.Width, cannonBox.Top);
                }

                cannonBox.Location = new Point(cannonBox.Left + 2, cannonBox.Top); //Changes location of cannonBox to a new location to the right
                Application.DoEvents();
                System.Threading.Thread.Sleep(10); //Delays the movement by couple milliseconds to stop instant movement
            }
        }

    }
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52