简体   繁体   中英

C# foreach won't stop displaying “Player 1 perdio”

Im currently working on a snake/Tron game with C# Windows form, i have some code for when to check if one snake has tocuhed the other one or bumped into a wall.

private void checkEndGame()
      {
            Boolean endGame = false;

            foreach(Point trail in bothtrail)
            {
                Rectangle rect = new Rectangle(trail, new Size(15, 15));

                if (p2.Bounds.IntersectsWith(rect) && p2trail.Count > 1)
                {
                    tmr1.Stop();
                    endGame = true;
                    MessageBox.Show("Player 1 perdio"); 
                }
                else if (p1.Bounds.IntersectsWith(rect) && p1trail.Count > 1)
                {
                    tmr1.Stop();
                    endGame = true;
                    MessageBox.Show("Player 2 perdio"); //Wont Stop Showing Message Box
                }
            }

                if (p1.Left < 0 || p1.Top < 0 || p1.Left > this.Width || p1.Top > this.Height)
                {
                    tmr1.Stop();
                    endGame = true;
                    MessageBox.Show("Player1 perdio");
                }

                else if (p2.Left < 0 || p2.Top < 0 || p2.Left > this.Width || p2.Top > this.Height)
                {
                    tmr1.Stop();
                    endGame = true;
                    MessageBox.Show("Player2 perdio");
                }

                if (endGame)
                {
                    newGame();
                }
        }

Once it runs the first time and resets with newGame(); the message box for the if and else if will continue to show nonstop. I have tried putting some breaks but I get the same result.

Should I be using continue; instead?

It looks like you are missing the condition for endGame after the foreach loop and break condition after in if statement in foreach loop. Try following:

    private void checkEndGame()
    {
        var endGame = false;

        foreach (Point trail in bothtrail)
        {
            var rect = new Rectangle(trail, new Size(15, 15));

            if (p2.Bounds.IntersectsWith(rect) && p2trail.Count > 1)
            {
                tmr1.Stop();
                endGame = true;
                MessageBox.Show("Player 1 perdio");
                break;
            }
            else if (p1.Bounds.IntersectsWith(rect) && p1trail.Count > 1)
            {
                tmr1.Stop();
                endGame = true;
                MessageBox.Show("Player 2 perdio"); //Wont Stop Showing Message Box
                break;
            }
        }

        if (!endGame)
        {

            if (p1.Left < 0 || p1.Top < 0 || p1.Left > this.Width || p1.Top > this.Height)
            {
                tmr1.Stop();
                endGame = true;
                MessageBox.Show("Player1 perdio");
            }

            else if (p2.Left < 0 || p2.Top < 0 || p2.Left > this.Width || p2.Top > this.Height)
            {
                tmr1.Stop();
                endGame = true;
                MessageBox.Show("Player2 perdio");
            }
        }


        if (endGame)
        {
            newGame();
        }
    }

In addition to this, you have lot of scope of code reusability here. You may want to create a common method something similar to below:

private bool _gameEnded = false;

private void EndGame(string player)
{
   tmr1.Stop();
   _gameEnded = true;
   MessageBox.Show($"{player} perdio");
}

And then call this method from checkEndGame wherever applicable.

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