2017-04-22 63 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