簡體   English   中英

在C#中重新啟動while循環

[英]Restarting while loop in C#

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

    }

您可以使用:

continue;

跳到下一個循環重復(您在其中編寫goto restart; )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM