在C#中循环时重新启动

本文关键字:重新启动 循环 | 更新日期: 2023-09-27 18:29:07

我是C#的初学者,创建了一个小游戏,用户可以在其中以一定的重力、速度和随机风投球。球应该击中一个目标,(当它击中时)给用户分数,然后回到起始位置,让用户再次投球。除了把球带回首发位置之外,我已经把一切都做好了。如有任何帮助,我们将不胜感激!

以下是我的代码的一些部分:

   private void goButton_Click(object sender, EventArgs e) 
    {
        if (running == true) 
        {
            b1.Location = new Point(0, 300); 
            b1.speedX = (double)upDownX.Value; 
            b1.speedY = (double)upDownY.Value; 
            running = false; 
            b1.Start();
            return;
        }
        running = true;
        worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(RunMe);             worker.RunWorkerAsync(); 
    }
public void RunMe(object sender, DoWorkEventArgs e)
    {
        while (running)
        {
            if (b1.speedY > 0 && b1.Location.Y > panel.Size.Height - b1.Size.Height)
            {
                b1.posY = panel.Size.Height - b1.Size.Height;
                addPoints();
                running = false;
                BeginInvoke((MethodInvoker)delegate
                {
                    b1.Location = new Point(0, 300);
                });
            }
            if (b1.speedX > 0 && b1.Location.X > panel.Size.Width - b1.Size.Width)
            {
                running = false;
                if (b1.Location.Y < 290 && b1.Location.Y > 150 && b1.Location.X > 430)
                {
                    if (diffValue.Text == "Normal")
                    {
                        Ball.score += 10;
                    }
                    if (diffValue.Text == "Easy")
                    {
                        Ball.score += 7;
                    }
                }
                else if (b1.Location.Y < 390 && b1.Location.Y > 60 && b1.Location.X > 430)
                {
                    if (diffValue.Text == "Normal")
                    {
                        Ball.score += 5;
                    }
                    if (diffValue.Text == "Easy")
                    {
                        Ball.score += 3;
                    }
                }
                addPoints();
                b1.BounceX();
                goto restart;
            }
            if (b1.speedX < 0 && b1.Location.X < 0)
            {
                b1.BounceX();
            }

            this.Invoke(new MoveBallCallback(MoveBall), b1);
            Thread.Sleep(10);
        }
    }

public void addPoints()
    {
        if (Ball.tries >= 1)
        {
            Ball.tries -= 1;
            string triesLeft = Ball.tries.ToString();
            this.Invoke(new Action(() => this.shotsLeft.Text = triesLeft));
            string score = Ball.score.ToString();
            this.Invoke(new Action(() => this.scores.Text = score));
            Normal();
            BeginInvoke((MethodInvoker)delegate
            {
                b1.Location = new Point(0, 300);
            });

        }
        else 
        {
            MessageBox.Show("No shots left!");
            string currentHighscore = System.IO.File.ReadAllText(@"highscore.txt");
            this.highscoreValue.Text = currentHighscore;
            int Highscore = Convert.ToInt32(currentHighscore);
            string score = Ball.score.ToString();
            this.Invoke(new Action(() => this.scores.Text = score));
            int Score = Convert.ToInt32(Ball.score);
            if (Score > Highscore)
            {
                MessageBox.Show("New highscore!");
                string highScore = score;
                System.IO.File.WriteAllText(@"highscore.txt", highScore);
                string highscore = Highscore.ToString();
                this.Invoke(new Action(() => this.highscoreValue.Text = highscore));
            }
            this.Invoke(new Action(() => goButton.Enabled = false));
        }
    }

在C#中循环时重新启动

您可以使用:

continue;

跳到循环的下一个重复(在这里您编写goto restart;)。