简体   繁体   中英

How to make PictureBox bounce off?

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

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
            }
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM