Beispiel #1
0
 // 'true' if alive
 public bool Go(Direction headdirection, ref Food food)
 {
     if (Math.Abs(headdirection - HeadDirection) == 2)
         return true;
     HeadDirection = headdirection;
     WormElement elem = Elements[0];
     int x = elem.X,
         y = elem.Y;
     if ((int)headdirection % 2 == 0)
     {
         y += (int)headdirection - 1;
         if (y < 0)
             y = Program.Conf.FieldHeight - 1;
         else if (y >= Program.Conf.FieldHeight)
             y = 0;
     }
     else
     {
         x -= (int)headdirection - 2;
         if (x < 0)
             x = Program.Conf.FieldWidth - 1;
         else if (x >= Program.Conf.FieldWidth)
             x = 0;
     }
     foreach (WormElement e in Elements)
     {
         if ((e.X == x) && (e.Y == y))
             return false;
     }
     bool bfood = (x == food.Element.X) && (y == food.Element.Y);
     Elements.Insert(0, new WormElement((byte)x, (byte)y, bfood));
     DeletedElement = Elements[Elements.Count - 1];
     Elements.Remove(DeletedElement);
     if (DeletedElement.Food)
     {
         Elements.Add(new WormElement(DeletedElement.X, DeletedElement.Y, false));
         DeletedElement = null;
     }
     return true;
 }
Beispiel #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);
     }
 }