/// <summary>Sets game level</summary> private static void Level(LevelChoice whatToDo, Snake[] sammy) { switch (whatToDo) { case LevelChoice.StartOver: CurLevel = 1; break; case LevelChoice.NextLevel: CurLevel = CurLevel + 1; break; } // Initialize Snakes sammy[0].Head = 1; sammy[0].Length = 2; sammy[0].Alive = true; sammy[1].Head = 1; sammy[1].Length = 2; sammy[1].Alive = true; Inputs[0].Clear(); Inputs[1].Clear(); InitColors(); switch (CurLevel) { case 1: sammy[0].Row = 25; sammy[1].Row = 25; sammy[0].Col = 50; sammy[1].Col = 30; sammy[0].Direction = Direction.Right; sammy[1].Direction = Direction.Left; break; case 2: for (int i = 20; i <= 60; i++) Set(25, i, Walls); sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 60; sammy[1].Col = 20; sammy[0].Direction = Direction.Left; sammy[1].Direction = Direction.Right; break; case 3: for (int i = 10; i <= 40; i++) { Set(i, 20, Walls); Set(i, 60, Walls); } sammy[0].Row = 25; sammy[1].Row = 25; sammy[0].Col = 50; sammy[1].Col = 30; sammy[0].Direction = Direction.Up; sammy[1].Direction = Direction.Down; break; case 4: for (int i = 4; i <= 30; i++) { Set(i, 20, Walls); Set(53 - i, 60, Walls); } for (int i = 2; i <= 40; i++) { Set(38, i, Walls); Set(15, 81 - i, Walls); } sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 60; sammy[1].Col = 20; sammy[0].Direction = Direction.Left; sammy[1].Direction = Direction.Right; break; case 5: for (int i = 13; i <= 39; i++) { Set(i, 21, Walls); Set(i, 59, Walls); } for (int i = 23; i <= 57; i++) { Set(11, i, Walls); Set(41, i, Walls); } sammy[0].Row = 25; sammy[1].Row = 25; sammy[0].Col = 50; sammy[1].Col = 30; sammy[0].Direction = Direction.Up; sammy[1].Direction = Direction.Down; break; case 6: for (int i = 4; i <= 49; i++) { if (i > 30 || i < 23) { Set(i, 10, Walls); Set(i, 20, Walls); Set(i, 30, Walls); Set(i, 40, Walls); Set(i, 50, Walls); Set(i, 60, Walls); Set(i, 70, Walls); } } sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 65; sammy[1].Col = 15; sammy[0].Direction = Direction.Down; sammy[1].Direction = Direction.Up; break; case 7: for (int i = 4; i <= 49; i += 2) Set(i, 40, Walls); sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 65; sammy[1].Col = 15; sammy[0].Direction = Direction.Down; sammy[1].Direction = Direction.Up; break; case 8: for (int i = 4; i <= 40; i++) { Set(i, 10, Walls); Set(53 - i, 20, Walls); Set(i, 30, Walls); Set(53 - i, 40, Walls); Set(i, 50, Walls); Set(53 - i, 60, Walls); Set(i, 70, Walls); } sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 65; sammy[1].Col = 15; sammy[0].Direction = Direction.Down; sammy[1].Direction = Direction.Up; break; case 9: for (int i = 6; i <= 47; i++) { Set(i, i, Walls); Set(i, i + 28, Walls); } sammy[0].Row = 40; sammy[1].Row = 15; sammy[0].Col = 75; sammy[1].Col = 5; sammy[0].Direction = Direction.Up; sammy[1].Direction = Direction.Down; break; default: for (int i = 4; i <= 49; i += 2) { Set(i, 10, Walls); Set(i + 1, 20, Walls); Set(i, 30, Walls); Set(i + 1, 40, Walls); Set(i, 50, Walls); Set(i + 1, 60, Walls); Set(i, 70, Walls); } sammy[0].Row = 7; sammy[1].Row = 43; sammy[0].Col = 65; sammy[1].Col = 15; sammy[0].Direction = Direction.Down; sammy[1].Direction = Direction.Up; break; } }
/// <summary>Main routine that controls game play</summary> private static void PlayNibbles() { // Initialize Snakes Snakes[0] = new Snake { Lives = 5, Score = 0, Color = Sammy, Body = new SnakePiece[MAXSNAKELENGTH] }; Snakes[1] = new Snake { Lives = 5, Score = 0, Color = Jake, Body = new SnakePiece[MAXSNAKELENGTH] }; Level(LevelChoice.StartOver, Snakes); var startRow1 = Snakes[0].Row; var startCol1 = Snakes[0].Col; var startRow2 = Snakes[1].Row; var startCol2 = Snakes[1].Col; var curSpeed = Speed; // play Nibbles until finished SpacePause(" Level " + CurLevel + ", Push Space"); do { if (NumPlayers == 1) Snakes[1].Row = 0; var number = 1; // Current number that snakes are trying to run into var nonum = true; // nonum = TRUE if a number is not on the screen int numberRow = 0, numberCol = 0, sisterRow = 0; var playerDied = false; PrintScore(NumPlayers, Snakes[0].Score, Snakes[1].Score, Snakes[0].Lives, Snakes[1].Lives); Music.Play("T160O1>L20CDEDCDL10ECC", true); do { // Print number if no number exists if (nonum) { do { numberRow = (int) (Rnd.NextDouble() * 47 + 3); numberCol = (int) (Rnd.NextDouble() * 78 + 2); sisterRow = numberRow + Arena[numberRow, numberCol].Sister; } while (PointIsThere(numberRow, numberCol, Background) || PointIsThere(sisterRow, numberCol, Background)); numberRow = Arena[numberRow, numberCol].RealRow; nonum = false; Console.ForegroundColor = Sammy; Console.BackgroundColor = Background; ConsoleSetCursorPosition(numberCol, numberRow); Console.Write(number.ToString().Last()); } // Delay game Thread.Sleep(curSpeed); // Get keyboard input & Change direction accordingly while (Console.KeyAvailable) { var kbd = Console.ReadKey(true).Key; switch (kbd) { case ConsoleKey.W: if (Inputs[1].Count == 0 || Inputs[1].Last() != Direction.Up) Inputs[1].Add(Direction.Up); break; case ConsoleKey.S: if (Inputs[1].Count == 0 || Inputs[1].Last() != Direction.Down) Inputs[1].Add(Direction.Down); break; case ConsoleKey.A: if (Inputs[1].Count == 0 || Inputs[1].Last() != Direction.Left) Inputs[1].Add(Direction.Left); break; case ConsoleKey.D: if (Inputs[1].Count == 0 || Inputs[1].Last() != Direction.Right) Inputs[1].Add(Direction.Right); break; case ConsoleKey.UpArrow: if (Inputs[0].Count == 0 || Inputs[0].Last() != Direction.Up) Inputs[0].Add(Direction.Up); break; case ConsoleKey.DownArrow: if (Inputs[0].Count == 0 || Inputs[0].Last() != Direction.Down) Inputs[0].Add(Direction.Down); break; case ConsoleKey.LeftArrow: if (Inputs[0].Count == 0 || Inputs[0].Last() != Direction.Left) Inputs[0].Add(Direction.Left); break; case ConsoleKey.RightArrow: if (Inputs[0].Count == 0 || Inputs[0].Last() != Direction.Right) Inputs[0].Add(Direction.Right); break; case ConsoleKey.P: SpacePause(" Game Paused ... Push Space "); nonum = true; break; default: break; } } for (int a = 0; a < NumPlayers; a++) { if (Inputs[a].Count > 0) { var input = Inputs[a][0]; Inputs[a].RemoveAt(0); switch (input) { case Direction.Up: if (Snakes[a].Direction != Direction.Down) Snakes[a].Direction = Direction.Up; break; case Direction.Down: if (Snakes[a].Direction != Direction.Up) Snakes[a].Direction = Direction.Down; break; case Direction.Left: if (Snakes[a].Direction != Direction.Right) Snakes[a].Direction = Direction.Left; break; case Direction.Right: if (Snakes[a].Direction != Direction.Left) Snakes[a].Direction = Direction.Right; break; } } // Move Snake switch (Snakes[a].Direction) { case Direction.Up: Snakes[a].Row = Snakes[a].Row - 1; break; case Direction.Down: Snakes[a].Row = Snakes[a].Row + 1; break; case Direction.Left: Snakes[a].Col = Snakes[a].Col - 1; break; case Direction.Right: Snakes[a].Col = Snakes[a].Col + 1; break; } // If snake hits number, respond accordingly if (numberRow == (Snakes[a].Row + 1) / 2 && numberCol == Snakes[a].Col) { Music.Play("O0L16>CCCE", true); if (Snakes[a].Length < MAXSNAKELENGTH - 30) Snakes[a].Length = Snakes[a].Length + number * 4; Snakes[a].Score = Snakes[a].Score + number; PrintScore(NumPlayers, Snakes[0].Score, Snakes[1].Score, Snakes[0].Lives, Snakes[1].Lives); number = number + 1; if (number == 10) { EraseSnake(Snakes[0]); EraseSnake(Snakes[1]); ConsoleSetCursorPosition(numberCol, numberRow); Console.Write(" "); Level(LevelChoice.NextLevel, Snakes); PrintScore(NumPlayers, Snakes[0].Score, Snakes[1].Score, Snakes[0].Lives, Snakes[1].Lives); SpacePause(" Level " + CurLevel + ", Push Space"); if (NumPlayers == 1) Snakes[1].Row = 0; number = 1; if (IncreaseSpeedDuringPlay) curSpeed = Math.Max(10, curSpeed - 10); } nonum = true; } } for (int a = 0; a < NumPlayers; a++) { // If player runs into any point, or the head of the other snake, it dies. if (PointIsThere(Snakes[a].Row, Snakes[a].Col, Background) || (Snakes[0].Row == Snakes[1].Row && Snakes[0].Col == Snakes[1].Col)) { Music.Play("O0L32EFGEFDC", true); Console.BackgroundColor = Background; ConsoleSetCursorPosition(numberCol, numberRow); Console.Write(" "); playerDied = true; Snakes[a].Alive = false; Snakes[a].Lives = Snakes[a].Lives - 1; } // Otherwise, move the snake, and erase the tail else { Snakes[a].Head = (Snakes[a].Head + 1) % MAXSNAKELENGTH; Snakes[a].Body[Snakes[a].Head].Row = Snakes[a].Row; Snakes[a].Body[Snakes[a].Head].Col = Snakes[a].Col; var tail = (Snakes[a].Head + MAXSNAKELENGTH - Snakes[a].Length) % MAXSNAKELENGTH; Set(Snakes[a].Body[tail].Row, Snakes[a].Body[tail].Col, Background); Snakes[a].Body[tail].Row = 0; Set(Snakes[a].Row, Snakes[a].Col, Snakes[a].Color); } } } while (!playerDied); // reset speed to initial value curSpeed = Speed; for (int a = 0; a < NumPlayers; a++) { // If dead, then erase snake in really cool way EraseSnake(Snakes[a]); if (!Snakes[a].Alive) { // Update score Snakes[a].Score = Snakes[a].Score - 10; PrintScore(NumPlayers, Snakes[0].Score, Snakes[1].Score, Snakes[0].Lives, Snakes[1].Lives); if (a == 0) SpacePause(" Sammy Dies! Push Space! --->"); else SpacePause(" <--- Jake Dies! Push Space! "); } } Level(LevelChoice.SameLevel, Snakes); PrintScore(NumPlayers, Snakes[0].Score, Snakes[1].Score, Snakes[0].Lives, Snakes[1].Lives); // Play next round, until either of snake's lives have run out. } while (Snakes[0].Lives != 0 && Snakes[1].Lives != 0); }
/// <summary>Erases snake to facilitate moving through playing field</summary> private static void EraseSnake(Snake snake) { for (int c = 0; c <= 9; c++) for (int b = snake.Length - c; b >= 0; b -= 10) { var tail = (snake.Head + MAXSNAKELENGTH - b) % MAXSNAKELENGTH; Set(snake.Body[tail].Row, snake.Body[tail].Col, Background); Thread.Sleep(2); } }