public Snake() { Body = new List <Possison> { new Possison(10, 10) }; LastPoss = Body[0]; Direction = ConsoleKey.LeftArrow; Speed = 200; }
private static void PrintApple(int x1, int x2, int y1, int y2) { var xExclude = Snake.Body.Select(x => x.XPoss); var yExclude = Snake.Body.Select(y => y.YPoss); var xNums = Enumerable.Range(x1, x2).Where(x => !xExclude.Contains(x)); var yNums = Enumerable.Range(y1, y2).Where(x => !yExclude.Contains(x)); int xPoss = xNums.ElementAt(Rng.Next(0, xNums.Count() - 1)); int yPoss = xNums.ElementAt(Rng.Next(0, yNums.Count() - 1)); var poss = new Possison(xPoss, yPoss); poss.PrintPossision('@', ConsoleColor.DarkRed); ApplePoss = poss; }
public void PrintSnake() { var poss = new Possison(); switch (Direction) { case ConsoleKey.LeftArrow: poss.XPoss = LastPoss.XPoss - 1; poss.YPoss = LastPoss.YPoss; break; case ConsoleKey.RightArrow: poss.XPoss = LastPoss.XPoss + 1; poss.YPoss = LastPoss.YPoss; break; case ConsoleKey.UpArrow: poss.XPoss = LastPoss.XPoss; poss.YPoss = LastPoss.YPoss - 1; break; case ConsoleKey.DownArrow: poss.XPoss = LastPoss.XPoss; poss.YPoss = LastPoss.YPoss + 1; break; } LastPoss = poss; Body.Add(poss); if (!hasEaten) { Body.ForEach(x => x.PrintPossision(' ')); Body.RemoveAt(0); Body.ForEach(x => x.PrintPossision('O', ConsoleColor.Green)); } else { Body.ForEach(x => x.PrintPossision('O', ConsoleColor.Green)); hasEaten = false; } }