如何让PictureBox反弹?

时间:2018-01-13 11:05:56

标签: c# winforms

对于我在C#WinForms中的Space Invaders游戏,我有这个代码根据箭头键输入移动玩家的大炮:

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

        }
    }

然而,当cannonBox到达形式的边界时,它只是继续前进,我想让它反弹并向另一个方向前进。我想并尝试使用这样的东西,但要找到与其相交的位置的确切位置真的很难:

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

1 个答案:

答案 0 :(得分:2)

对于您的请求,这里有一个示例,说明如何继续移动并使图片框来自表单的另一侧。
另一件事就是@ S.Serp评论说,最好不要为这个任务使用for循环,但我想你的代码是出于学习目的。
也:
1。使用Application.DoEvents()更好会导致问题(https://stackoverflow.com/a/5183623/5718868)。阅读C#中的async await个关键字,并使用它代替Application.DoEvents()
2.在我的示例中,不要硬编码表单/屏幕使用变量 - screenSize

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

    }
}