2017-04-22 54 views
-4

所以我做了這個簡單的蛇遊戲我的指導做了一個學校項目,現在我希望通過添加一個比分反超,以改進和提高蛇的移動速度每時間你得到的食物幫助表示讚賞
這是目前我遇到 enter image description here使蛇遊戲分數計數器和增加移動速度

namespace Snake 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WindowHeight = 32; 
      Console.WindowWidth = 64; 
      int screenwidth = Console.WindowWidth; 
      int screenheight = Console.WindowHeight; 
      Random randomnummer = new Random(); 
      int emtiaz = 5; 
      int bakht = 0; 
      pixel gz = new pixel(); 
      gz.xpos = screenwidth/2; 
      gz.ypos = screenheight/2; 
      gz.rangsafe = ConsoleColor.Red; 
      string movement = "RIGHT"; 
      List<int> xposlijf = new List<int>(); 
      List<int> yposlijf = new List<int>(); 
      int berryx = randomnummer.Next(0, screenwidth); 
      int berryy = randomnummer.Next(0, screenheight); 
      DateTime tijd = DateTime.Now; 
      DateTime tijd2 = DateTime.Now; 
      string buttonpressed = "no"; 
      while (true) 
      { 
       Console.Clear(); 
       if (gz.xpos == screenwidth - 1 || gz.xpos == 0 || gz.ypos == screenheight - 1 || gz.ypos == 0) 
       { 
        bakht = 1; 
       } 
       for (int i = 0; i < screenwidth; i++) 
       { 
        Console.SetCursorPosition(i, 0); 
        Console.Write("*"); 
       } 
       for (int i = 0; i < screenwidth; i++) 
       { 
        Console.SetCursorPosition(i, screenheight - 1); 
        Console.Write("*"); 
       } 
       for (int i = 0; i < screenheight; i++) 
       { 
        Console.SetCursorPosition(0, i); 
        Console.Write("*"); 
       } 
       for (int i = 0; i < screenheight; i++) 
       { 
        Console.SetCursorPosition(screenwidth - 1, i); 
        Console.Write("v"); 
       } 
       Console.ForegroundColor = ConsoleColor.Green; 
       if (berryx == gz.xpos && berryy == gz.ypos) 
       { 
        emtiaz++; 
        berryx = randomnummer.Next(1, screenwidth - 2); 
        berryy = randomnummer.Next(1, screenheight - 2); 
       } 
       for (int i = 0; i < xposlijf.Count(); i++) 
       { 
        Console.SetCursorPosition(xposlijf[i], yposlijf[i]); 
        Console.Write("*"); 
        if (xposlijf[i] == gz.xpos && yposlijf[i] == gz.ypos) 
        { 
         bakht = 1; 
        } 
       } 
       if (bakht == 1) 
       { 
        break; 
       } 
       Console.SetCursorPosition(gz.xpos, gz.ypos); 
       Console.ForegroundColor = gz.rangsafe; 
       Console.Write("*"); 
       Console.SetCursorPosition(berryx, berryy); 
       Console.ForegroundColor = ConsoleColor.DarkBlue; 
       Console.Write("*"); 
       tijd = DateTime.Now; 
       buttonpressed = "no"; 
       while (true) 
       { 
        tijd2 = DateTime.Now; 
        if (tijd2.Subtract(tijd).TotalMilliseconds > 500) { break; } 
        if (Console.KeyAvailable) 
        { 
         ConsoleKeyInfo toets = Console.ReadKey(true); 
         if (toets.Key.Equals(ConsoleKey.UpArrow) && movement != "DOWN" && buttonpressed == "no") 
         { 
          movement = "UP"; 
          buttonpressed = "yes"; 
         } 
         if (toets.Key.Equals(ConsoleKey.DownArrow) && movement != "UP" && buttonpressed == "no") 
         { 
          movement = "DOWN"; 
          buttonpressed = "yes"; 
         } 
         if (toets.Key.Equals(ConsoleKey.LeftArrow) && movement != "RIGHT" && buttonpressed == "no") 
         { 
          movement = "LEFT"; 
          buttonpressed = "yes"; 
         } 
         if (toets.Key.Equals(ConsoleKey.RightArrow) && movement != "LEFT" && buttonpressed == "no") 
         { 
          movement = "RIGHT"; 
          buttonpressed = "yes"; 
         } 
        } 
       } 
       xposlijf.Add(gz.xpos); 
       yposlijf.Add(gz.ypos); 
       switch (movement) 
       { 
        case "UP": 
         gz.ypos--; 
         break; 
        case "DOWN": 
         gz.ypos++; 
         break; 
        case "LEFT": 
         gz.xpos--; 
         break; 
        case "RIGHT": 
         gz.xpos++; 
         break; 
       } 
       if (xposlijf.Count() > emtiaz) 
       { 
        xposlijf.RemoveAt(0); 
        yposlijf.RemoveAt(0); 
       } 
      } 
      Console.SetCursorPosition(screenwidth/5, screenheight/2); 
      Console.WriteLine("bazi ra bakhti, emtiaz: " + emtiaz); 
      Console.SetCursorPosition(screenwidth/5, screenheight/2 + 1); 
     } 
     class pixel 
     { 
      public int xpos { get; set; } 
      public int ypos { get; set; } 
      public ConsoleColor rangsafe { get; set; } 
     } 
    } 
} 
+1

這不是一個代碼編寫的服務。 –

+1

你的問題是什麼? SO是針對具體問題的具體問題,而不是針對如何做到這一點的一般幫助。 – HimBromBeere

+0

它不會有任何問題,我只是想用變量,而不是改善它 –

回答

0

看看下面的2個代碼

if (tijd2.Subtract(tijd).TotalMilliseconds > 500) 

使問題一個變量,用它來代替500 不是使用該變量在以下塊

if (berryx == gz.xpos && berryy == gz.ypos) 
{ 
    emtiaz++; 
    berryx = randomnummer.Next(1, screenwidth - 2); 
    berryy = randomnummer.Next(1, screenheight - 2); 
} 

測試和修改,看看你的遊戲的行爲,它會引導你到你正在嘗試做的。 您的變量emtiaz已經計數次蛇的數量得到食物

+0

500的工作,但得分計數器顯示了遊戲區 –

+0

你需要你的遊戲區域內確定座標之外(如果您想這就是打印計數器),那麼只打印emtiaz在這些COORDS ... 看看下面的代碼,並試圖瞭解是什麼在那裏發生。這就是所有你需要做打印的分數.. Console.SetCursorPosition(berryx,berryy); Console.ForegroundColor = ConsoleColor.DarkBlue; Console.Write(「*」); – user1063108