简体   繁体   English

如何使PictureBox弹起?

[英]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: 对于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
                }
            }

        }
    }

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. 但是,当cannonBox到达表单的边界时,它会继续前进,我想使其弹起并朝另一个方向移动。 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. 另一件事是@ S.Serp评论的,最好不要为该任务使用循环,但是我想您的代码仅供学习。
also: 也:
1. it is better not to use Application.DoEvents() it can cause problems ( https://stackoverflow.com/a/5183623/5718868 ). 1.最好不要使用Application.DoEvents()它可能导致问题( https://stackoverflow.com/a/5183623/57188​​68 )。 read about async await keywords in C# and use it instead of Application.DoEvents() 阅读有关C#中的async await关键字的信息,并使用它代替Application.DoEvents()
2. dont hard code the form / screen use a variable - screenSize in my example. 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
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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