2017-09-06 139 views
1

我是一名初學者,使用Visual Studio製作C#Windows窗體。所以我有一個磚塊遊戲(比如帶有磚塊和磚塊的經典磚塊遊戲),如果玩家不會將球彈回,那麼基本上游戲結束了。但是,當發生這種情況時,計時器剛剛停止,無法重新啓動。我現在剛剛說的代碼是用「你輸了」來創建一個消息框,你必須退出程序才能再次播放。如果你達到30分,那就是一場勝利,如果你再次參加比賽,你也必須退出。遊戲結束後,如何在遊戲中實現重新啓動功能?

我試過Google搜索這個,我看到的建議是,如果它的所有設置已經在他們的案例,所以實施它,而不必重新做到這一切都很難。我想做一個簡單的實現(或編輯)哪裏有一個再玩功能。我如何修改現有的代碼來做到這一點?非常感謝您的幫助。

這是計算遊戲結束if語句的代碼部分。 EDIT =提供的整個代碼。

 using System; 
     using System.Collections.Generic; 
     using System.ComponentModel; 
     using System.Data; 
     using System.Drawing; 
     using System.Linq; 
     using System.Text; 
     using System.Threading.Tasks; 
     using System.Windows.Forms; 
     using System.Media; 
     using System.Reflection; 
     using System.IO; 

     namespace FinalProjectGame 
{ 

public partial class FrmGame : Form 
{ 

    bool goLeft; 
    bool goRight; 
    int speed = 10; 

    int pBallx = 5; 
    int pBally = 5; 

    int score = 0; 

    public FrmGame() 
    { 
     InitializeComponent(); 

    } 

    private void keyisdown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left && btnGamer.Left > 0) 
     { 
      goLeft = true; 
     } 

     if (e.KeyCode == Keys.Right && btnGamer.Left + btnGamer.Width < 920) 
     { 
      goRight = true; 
     } 
    } 

    private void keyisup(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Left) 
     { 
      goLeft = false; 
     } 

     if (e.KeyCode == Keys.Right) 
     { 
      goRight = false; 
     } 
    } 

    private void TmrMainTimer_Tick(object sender, EventArgs e) 
    { 
     pBall.Left += pBallx; 
     pBall.Top += pBally; 

     lblScore.Text = "Score: " + score; //keeping score 

     if (goLeft) { btnGamer.Left -= speed; } //izquierda 
     if (goRight) { btnGamer.Left += speed; } //derecha 

     if (btnGamer.Left < 1) 
     { 
      goLeft = false; //disables "no-clip" 
     } 
     else if (btnGamer.Left + btnGamer.Width > 920) 
     { 
      goRight = false; 
     } 
     if (pBall.Left + pBall.Width > ClientSize.Width || pBall.Left < 0) 
     { 
      pBallx = -pBallx; //left, right wall bounce 

     } 

     if (pBall.Top < 0 || pBall.Bounds.IntersectsWith(btnGamer.Bounds)) 
     { 
      pBally = -pBally; //top, bottom wall bounce 

     } 

     foreach (Control x in this.Controls) 
      //main code brickies subtraction 
     { 
      if (x is PictureBox && x.Tag == "blockies") 
      { 
       if (pBall.Bounds.IntersectsWith(x.Bounds)) 
       { 
        this.Controls.Remove(x); 
        pBally = -pBally; 
        score++; 

       } //end of main 

        //ALT method foreach (Control x in this.Controls) 
        //{ 
         if (!(x is PictureBox)) 
          continue; 

         //this is needed if some specific property of the PictureBox is needed. 
         PictureBox ctl = (PictureBox)x; 

         if (ctl.Tag.ToString() != "blockies") 
          continue; 

         if (!pBall.Bounds.IntersectsWith(ctl.Bounds)) 
          continue; 



         //this.Controls.Remove(x); 

         x.Visible = false; 

         pBally = -pBally; 
         score++; 
        //} 

      } 

     }//---- 
     //This accounts for user score. Upon completing the game, the user wins. If the user does not capture the ball, it is automatically a game over. 
     if (score == 30) 
     { 
      gameOver(); 
      MessageBox.Show("YES! WINNER!"); 
     } 
     if (pBall.Top + pBall.Height > ClientSize.Height) 
     { 
      gameOver(); 
      MessageBox.Show("SORRY YOU LOSE!"); 


     } 

     //====== 
    } 
    private void gameOver() 
    { 
     TmrMainTimer.Stop(); 
    } 

    private void FrmGame_Load(object sender, EventArgs e) //main form load 
    { 
     //load the splash screen form that was made with design view 
     SplashScreen splash = new SplashScreen(); 
     splash.Show(); 

     //using system media to play the audio which is played upon start of the main form 
     SoundPlayer Sndplayr = new SoundPlayer(Properties.Resources.RoleMusic); 
     Sndplayr.Play(); 
    } 

} 

}

+2

基本上,您將希望將所有變量重置回其初始狀態。你如何存儲你的數據模型? –

+2

我們不可能告訴你如何在不知道你的整個遊戲是如何實現的情況下實現它。你有一個遊戲循環?你有遊戲狀態對象嗎?如果你的整個遊戲「數據」存儲在遊戲狀態對象中,那麼你所要做的就是將狀態重置爲默認狀態。 – Tom

+2

這真的聞起來像是功課給我! – Moher

回答

-1

如果你想重新啓動WinForms應用程序,使用Application.Restart()。之後,您可以關閉您的遊戲窗體並使用靜態Main()方法重新啓動。

private void TmrMainTimer_Tick(object sender, EventArgs e) 
{ 
    ... 

    if (pBall.Top + pBall.Height > ClientSize.Height) 
    { 
    gameOver(); 
    MessageBox.Show("SORRY YOU LOSE!"); 

    // close the game form 
    this.Close(); 
    } 
} 

而在你的啓動代碼:

static class Program 
    { 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new FrmGame()); 

     // restart application 
     Application.Restart(); 
    } 
    } 

編輯:

以上IST玩這種遊戲從開始最簡單的方法的例子。當然,還有很多其他的選擇:

  1. 作爲擴展,你可以從一個(可能是隱藏的)程序的啓動形式開始你的遊戲形式。如果您在FrmGame.FormClosed事件中註冊事件處理程序,則可以在上一次遊戲丟失後再次重新啓動該表單。

  2. 像其他人一樣,StartAgain()方法,重置您的邏輯,也是一種選擇。

  3. 從遊戲邏輯(控制器)分離繪圖邏輯(查看)。對於這樣一個簡單的遊戲來說,這不是必要的,而且是過度的。但將事情簡單明瞭總是一個好主意。請參閱下一個項目的MVC模式...