Example #1
0
 public bool GenerateNewPos(ref Worm worm)
 {
     pos.X = 0;
     pos.Y = 0;
     bool b = false;
     int fsl = fieldSize - worm.Length;
     if (fsl <= 0) return false;
     int r = rand.Next(fsl);
     for (int i=0; i<r; i = (b) ? i : i+1)
     {
         pos.X++;
         if(pos.X >= maxX)
         {
             pos.X = 0;
             pos.Y++;
         }
         int j = 0;
         b = false;
         while(!b && j < worm.Length)
         {
             b = (pos.X == worm[j].X) && (pos.Y == worm[j].Y);
             j++;
         }
     }
     return true;
 }
Example #2
0
 static void GameStart()
 {
     AutoSetting();
     Direction dir = Direction.Right;
     ConsoleKeyInfo cki = new ConsoleKeyInfo();
     Worm worm = new Worm(WormLen, (byte)((Conf.FieldWidth - WormLen) / 2), (byte)(Conf.FieldHeight / 2));
     Food food = new Food(Conf.FieldHeight, Conf.FieldWidth);
     food.GenerateNewPos(ref worm);
     PrintWorm(worm);
     PrintFood(food.Element);
     while (true)
     {
         if (Console.KeyAvailable == true)
         {
             cki = Console.ReadKey(true);
             KeyToDir(cki.Key, ref dir);
         }
         if(!worm.Go(dir, ref food))
         {
             Console.Clear();
             Console.WriteLine("The end");
             Console.ReadLine();
             return;
         }
         if((worm[0].X == food.Element.X) && (worm[0].Y == food.Element.Y))
         {
             if(!food.GenerateNewPos(ref worm))
             {
                 Console.Clear();
                 Console.WriteLine("You win");
                 Console.ReadLine();
                 return;
             }
             PrintFood(food.Element);
         }
         PrintAction(worm[0], worm.DeletedElement);
         Thread.Sleep(200);
     }
 }
Example #3
0
 static void PrintWormText(Worm worm)
 {
     for (int i = 0; i < worm.Length; i++)
     {
         Console.WriteLine("\t{0}: {1} x {2}", i, worm[i].X, worm[i].Y);
     }
     Console.WriteLine();
 }
Example #4
0
 static void PrintWorm(Worm worm)
 {
     for (int i = 0; i < worm.Length; i++)
     {
         Console.SetCursorPosition(worm[i].X, worm[i].Y);
         Console.WriteLine(Conf.WormSymbol);
     }
 }