2016-01-05 75 views
-1

我是C#的初學者,創建了一個小型遊戲,用戶可以在其中以某種重力,速度和隨機風來擲球。球應該打到球門,給用戶點數(擊球時),然後回到開始位置讓用戶再次投擲。除了讓球回到起跑位置之外,我已經做好了一切工作。任何幫助將不勝感激!在C#中重新啓動while循環#

下面是我的一些代碼部分:

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

    } 
+0

你是什麼意思「重啓」while循環? – AustinWBryan

+1

什麼構成「讓球回到起跑位置」?你在哪裏設置球的「起始位置」?你不能只是再次執行同樣的邏輯嗎? – David

+1

您應該創建一個'Reset'方法並重新初始化該方法中的每個必需事物。只需在遊戲結束時調用它。 – Shaharyar

回答

2

您可以使用:

continue; 

跳到循環的下一個重複(你寫goto restart;)。