2016-11-17 316 views
0

我是新來的編程,我開始與C#。所以我遵循了一個關於在控制檯中製作簡單遊戲的教程。 Here's the tutorial。當我完成教程後,我留下了一個體面的比賽,但沒有打磨。遊戲不會重新啓動。我如何重新啓動遊戲機?

我試過一些不同的方式試圖繞過它,但沒有我試過似乎工作(因爲即時新的和一半的編碼沒有意義)。

所以我試了這個。爲什麼這不工作?

using System; 
using System.Threading; 
using System.Diagnostics; 

namespace DodgeGame 
{ 

public class Game 
{ 
    //This is our constructor, 
    // it gets run auto when new instance is made. 
    public Game() 
    { 
     //PLAYER 
     playerUnit = new PlayerUnit(5, 5, "@"); 

     //ENEMY 
     enemyUnits = new Unit[ numEnemies ]; 

     Random = new Random(); 
     Score = 0; 

      for(int i = 0; i < numEnemies; i++) 
      { 
       int row = Random.Next(0, Console.WindowHeight - 1); 
       enemyUnits[i] = new EnemyUnit(Console.WindowWidth -1, row , "<"); 
      } 

     stopwatch = new Stopwatch(); 
    } 



    private Stopwatch stopwatch; 

    private Unit playerUnit; 

    private int numEnemies = 20; 
    private Unit[] enemyUnits; 

    public static Random Random; 
    public static int Score; 

    public void Run() 
    { 

     stopwatch.Start(); 
     long timeAtPreviousFrame = stopwatch.ElapsedMilliseconds; 


     while(true) 
     { 
      //Time since last frame. 
      int deltaTimeMS = (int)(stopwatch.ElapsedMilliseconds 
            - timeAtPreviousFrame); 
      timeAtPreviousFrame = stopwatch.ElapsedMilliseconds; 


      //Updating Units. 
      playerUnit.Update(deltaTimeMS); 

      for(int i = 0; i < enemyUnits.Length; i++) 
      { 
       enemyUnits[i].Update(deltaTimeMS); 

       if(playerUnit.IsCollidingWith(enemyUnits[i])) 
       { 
        //Then game over. 
        GameOver(); 
        return; 
       } 
      } 
      playerUnit.Draw(); 

      foreach (Unit u in enemyUnits) 
      { 
       u.Draw(); 
      } 


      //Draw the score. 
      Console.SetCursorPosition(0, Console.WindowHeight - 1); 
      Console.Write("SCORE: " + Score.ToString()); 

      //Lets just do a tiny sleep to avoid running at too high of fps. 
      Thread.Sleep(5); 

     } 

     //PROGRAM END. 
    } 

    void GameOver() 
     { 
     Console.Clear(); 
     Console.WriteLine("Game Over. Your Score was: " + Score); 
     Console.WriteLine("Insert Name to LeaderBoard:"); 
     //TODO: Make a local leaderboard? 

     if(Console.KeyAvailable == true) 
     { 
      ConsoleKeyInfo cki = Console.ReadKey(true); 
      while(cki.Key != ConsoleKey.Escape) 
      { 
       if(cki.Key == ConsoleKey.Spacebar) Game(); 
       cki = Console.ReadKey(true); 
      } 


     Console.SetCursorPosition(0,Console.WindowHeight - 1); 


     } 

} 

}

我要重新啓動我的遊戲,但不關閉窗口。

對不起,如果這沒有足夠的信息來幫助。

+2

這不是你應該提供的信息的一部分,它不夠。 – Ogbe

+0

是的,最重要的是要展示主函數的外觀和遊戲循環過程中會發生什麼(我們當然可以檢查教程,但這意味着你的問題不包含足夠的信息) – Icepickle

+0

好吧,這證明了多少我不知道:/ – deviousPriest

回答

1

我想你有主要功能/調用函數如下;

static void Main(string[] args) 
{ 
    var game=new Game(); 
    //May be some initialization logic  
    game.Run(); 
} 

在,你可以添加一個while循環,並檢查用戶輸入,如果他想退出那麼他就可以按指定鍵(Esc鍵也許...)

請參閱下面的代碼;

static void Main(string[] args) 
{ 
    while(true) 
    { 
     var game = new Game(); 
     //May be some initialization logic  
     game.Run(); 

     Console.WriteLine("Press any key to restart, press Esc to close."); 
     var userInput = Console.ReadKey(); 
     if (userInput.Key == ConsoleKey.Escape) 
      return; 
    } 
} 

你可以根據你的需要調整這個邏輯,但我想你會明白我的觀點。

+0

感謝弗利平噸。很好地解釋了所有。正是我想要的:) – deviousPriest

+0

我有另一個問題,如果你不介意。我如何從這個聲明中刪除我的箭頭鍵(用它們來移動我的角色)。所以如果我死了,我立即按下箭頭鍵,遊戲就會重新啓動,而我不知道我的分數。對不起,如果im newb – deviousPriest

+0

添加'Console.ReadLine();'作爲'GameOver()'方法的最後一行。這將顯示分數,直到您按Enter鍵。按Enter後,您將收到有關重新啓動或關閉的消息。 –

1

您可以使用while循環。檢查用戶是否想要退出或開始遊戲(例如使用ESC和Space),然後進行適當的處​​理。

ConsoleKeyInfo cki = Console.ReadKey(true); 
while(cki.Key != KeyCode.Esc) 
{ 
    if(cki.Key == KeyCode.Space) Game(); 
    cki = Console.ReadKey(true); 
} 
+0

我試過這個,它沒有工作,它可能不是你的代碼中的缺陷,但我沒有正確使用它。你能簡單地解釋一下嗎? – deviousPriest

+0

也許告訴我什麼部分不起作用?粗略地解釋它通過代碼循環,並繼續閱讀一個鍵,直到你按下退出鍵。 –

+0

好吧,以及我不知道這就是即時通訊尋找我不知道。我可以做到這一點,當我按SpaceBar它只會'重置',並回到「遊戲」的開始? – deviousPriest

-2

什麼是遊戲()。你可以使用新的實例。

System.Diagnostics.Process.Start(Application.ExecutablePath); 

退出當前進程。 envoinment.exit();

+0

*我想在不關閉窗口的情況下重新開始遊戲。* 你有沒有讀過上面的問題? –

+1

Downvote reason:爲什麼不讓這個過程保持活躍狀態​​,而不是創建一個新過程並殺死當前過程?對我來說似乎是不必要的開銷,這使得應用程序比它應該更復雜。 –