/// <summary>Erases snake to facilitate moving through playing field</summary> private static void EraseSnake(SnakeType[] snake, SnakeBody[,] snakeBod, int snakeNum) { for (int c = 0; c < 9; c++) for (int b = snake[snakeNum].length - c; b >= 0; b -= 10) { var tail = (snake[snakeNum].head + MAXSNAKELENGTH - b) % MAXSNAKELENGTH; Set(snakeBod[tail, snakeNum].row, snakeBod[tail, snakeNum].col, colorTable[3]); Thread.Sleep(2); } }
/// <summary>Main routine that controls game play</summary> private static void PlayNibbles(int NumPlayers, int speed, string diff) { // Initialize Snakes SnakeBody[,] sammyBody = new SnakeBody[MAXSNAKELENGTH, 2]; SnakeType[] sammy = new SnakeType[2]; sammy[0] = new SnakeType { lives = 5, score = 0, scolor = colorTable[0] }; sammy[1] = new SnakeType { lives = 5, score = 0, scolor = colorTable[1] }; Level(STARTOVER, sammy); var startRow1 = sammy[0].row; var startCol1 = sammy[0].col; var startRow2 = sammy[1].row; var startCol2 = sammy[1].col; var curSpeed = speed; // play Nibbles until finished SpacePause(" Level " + curLevel + ", Push Space"); do { if (NumPlayers == 1) sammy[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, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); // PLAY "T160O1>L20CDEDCDL10ECC" 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, colorTable[3]) || PointIsThere(sisterRow, NumberCol, colorTable[3])); numberRow = arena[numberRow, NumberCol].realRow; nonum = false; Console.ForegroundColor = (ConsoleColor)colorTable[0]; Console.BackgroundColor = (ConsoleColor)colorTable[3]; ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(number.ToString().Last()); } // Delay game Thread.Sleep(curSpeed); // Get keyboard input & Change direction accordingly if (Console.KeyAvailable) { var kbd = Console.ReadKey(true).Key; switch (kbd) { case ConsoleKey.W: if (sammy[1].direction != 2) sammy[1].direction = 1; break; case ConsoleKey.S: if (sammy[1].direction != 1) sammy[1].direction = 2; break; case ConsoleKey.A: if (sammy[1].direction != 4) sammy[1].direction = 3; break; case ConsoleKey.D: if (sammy[1].direction != 3) sammy[1].direction = 4; break; case ConsoleKey.UpArrow: if (sammy[0].direction != 2) sammy[0].direction = 1; break; case ConsoleKey.DownArrow: if (sammy[0].direction != 1) sammy[0].direction = 2; break; case ConsoleKey.LeftArrow: if (sammy[0].direction != 4) sammy[0].direction = 3; break; case ConsoleKey.RightArrow: if (sammy[0].direction != 3) sammy[0].direction = 4; break; case ConsoleKey.Spacebar: case ConsoleKey.P: SpacePause(" Game Paused ... Push Space "); break; default: break; } } for (int a = 0; a < NumPlayers; a++) { // Move Snake switch (sammy[a].direction) { case 1: sammy[a].row = sammy[a].row - 1; break; case 2: sammy[a].row = sammy[a].row + 1; break; case 3: sammy[a].col = sammy[a].col - 1; break; case 4: sammy[a].col = sammy[a].col + 1; break; } // If snake hits number, respond accordingly if (numberRow == (sammy[a].row + 1) / 2 && NumberCol == sammy[a].col) { // PLAY "MBO0L16>CCCE" if (sammy[a].length < MAXSNAKELENGTH - 30) sammy[a].length = sammy[a].length + number * 4; sammy[a].score = sammy[a].score + number; PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); number = number + 1; if (number == 10) { EraseSnake(sammy, sammyBody, 0); EraseSnake(sammy, sammyBody, 1); ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(" "); Level(NEXTLEVEL, sammy); PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); SpacePause(" Level " + curLevel + ", Push Space"); if (NumPlayers == 1) sammy[1].row = 0; number = 1; if (diff == "P") { speed = speed - 10; curSpeed = speed; } } nonum = true; if (curSpeed < 1) curSpeed = 1; } } for (int a = 0; a < NumPlayers; a++) { // If player runs into any point, or the head of the other snake, it dies. if (PointIsThere(sammy[a].row, sammy[a].col, colorTable[3]) || (sammy[0].row == sammy[1].row && sammy[0].col == sammy[1].col)) { // PLAY "MBO0L32EFGEFDC" Console.BackgroundColor = (ConsoleColor)colorTable[3]; ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(" "); playerDied = true; sammy[a].alive = false; sammy[a].lives = sammy[a].lives - 1; } // Otherwise, move the snake, and erase the tail else { sammy[a].head = (sammy[a].head + 1) % MAXSNAKELENGTH; sammyBody[sammy[a].head, a].row = sammy[a].row; sammyBody[sammy[a].head, a].col = sammy[a].col; var tail = (sammy[a].head + MAXSNAKELENGTH - sammy[a].length) % MAXSNAKELENGTH; Set(sammyBody[tail, a].row, sammyBody[tail, a].col, colorTable[3]); sammyBody[tail, a].row = 0; Set(sammy[a].row, sammy[a].col, sammy[a].scolor); } } } while (!playerDied); // reset speed to initial value curSpeed = speed; for (int a = 0; a < NumPlayers; a++) { EraseSnake(sammy, sammyBody, a); // If dead, then erase snake in really cool way if (!sammy[a].alive) { // Update score sammy[a].score = sammy[a].score - 10; PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); if (a == 0) SpacePause(" Sammy Dies! Push Space! --->"); else SpacePause(" <---- Jake Dies! Push Space "); } } Level(SAMELEVEL, sammy); PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); // Play next round, until either of snake's lives have run out. } while (sammy[0].lives != 0 && sammy[1].lives != 0); }
/// <summary>Main routine that controls game play</summary> private static void PlayNibbles(int NumPlayers, int speed, string diff) { // Initialize Snakes SnakeBody[,] sammyBody = new SnakeBody[MAXSNAKELENGTH, 2]; SnakeType[] sammy = new SnakeType[2]; sammy[0] = new SnakeType { lives = 5, score = 0, scolor = colorTable[0] }; sammy[1] = new SnakeType { lives = 5, score = 0, scolor = colorTable[1] }; Level(STARTOVER, sammy); var startRow1 = sammy[0].row; var startCol1 = sammy[0].col; var startRow2 = sammy[1].row; var startCol2 = sammy[1].col; var curSpeed = speed; // play Nibbles until finished SpacePause(" Level " + curLevel + ", Push Space"); do { if (NumPlayers == 1) { sammy[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, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); // PLAY "T160O1>L20CDEDCDL10ECC" 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, colorTable[3]) || PointIsThere(sisterRow, NumberCol, colorTable[3])); numberRow = arena[numberRow, NumberCol].realRow; nonum = false; Console.ForegroundColor = (ConsoleColor)colorTable[0]; Console.BackgroundColor = (ConsoleColor)colorTable[3]; ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(number.ToString().Last()); } // Delay game Thread.Sleep(curSpeed); // Get keyboard input & Change direction accordingly if (Console.KeyAvailable) { var kbd = Console.ReadKey(true).Key; switch (kbd) { case ConsoleKey.W: if (sammy[1].direction != 2) { sammy[1].direction = 1; } break; case ConsoleKey.S: if (sammy[1].direction != 1) { sammy[1].direction = 2; } break; case ConsoleKey.A: if (sammy[1].direction != 4) { sammy[1].direction = 3; } break; case ConsoleKey.D: if (sammy[1].direction != 3) { sammy[1].direction = 4; } break; case ConsoleKey.UpArrow: if (sammy[0].direction != 2) { sammy[0].direction = 1; } break; case ConsoleKey.DownArrow: if (sammy[0].direction != 1) { sammy[0].direction = 2; } break; case ConsoleKey.LeftArrow: if (sammy[0].direction != 4) { sammy[0].direction = 3; } break; case ConsoleKey.RightArrow: if (sammy[0].direction != 3) { sammy[0].direction = 4; } break; case ConsoleKey.Spacebar: case ConsoleKey.P: SpacePause(" Game Paused ... Push Space "); break; default: break; } } for (int a = 0; a < NumPlayers; a++) { // Move Snake switch (sammy[a].direction) { case 1: sammy[a].row = sammy[a].row - 1; break; case 2: sammy[a].row = sammy[a].row + 1; break; case 3: sammy[a].col = sammy[a].col - 1; break; case 4: sammy[a].col = sammy[a].col + 1; break; } // If snake hits number, respond accordingly if (numberRow == (sammy[a].row + 1) / 2 && NumberCol == sammy[a].col) { // PLAY "MBO0L16>CCCE" if (sammy[a].length < MAXSNAKELENGTH - 30) { sammy[a].length = sammy[a].length + number * 4; } sammy[a].score = sammy[a].score + number; PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); number = number + 1; if (number == 10) { EraseSnake(sammy, sammyBody, 0); EraseSnake(sammy, sammyBody, 1); ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(" "); Level(NEXTLEVEL, sammy); PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); SpacePause(" Level " + curLevel + ", Push Space"); if (NumPlayers == 1) { sammy[1].row = 0; } number = 1; if (diff == "P") { speed = speed - 10; curSpeed = speed; } } nonum = true; if (curSpeed < 1) { curSpeed = 1; } } } for (int a = 0; a < NumPlayers; a++) { // If player runs into any point, or the head of the other snake, it dies. if (PointIsThere(sammy[a].row, sammy[a].col, colorTable[3]) || (sammy[0].row == sammy[1].row && sammy[0].col == sammy[1].col)) { // PLAY "MBO0L32EFGEFDC" Console.BackgroundColor = (ConsoleColor)colorTable[3]; ConsoleSetCursorPosition(NumberCol, numberRow); Console.Write(" "); playerDied = true; sammy[a].alive = false; sammy[a].lives = sammy[a].lives - 1; } // Otherwise, move the snake, and erase the tail else { sammy[a].head = (sammy[a].head + 1) % MAXSNAKELENGTH; sammyBody[sammy[a].head, a].row = sammy[a].row; sammyBody[sammy[a].head, a].col = sammy[a].col; var tail = (sammy[a].head + MAXSNAKELENGTH - sammy[a].length) % MAXSNAKELENGTH; Set(sammyBody[tail, a].row, sammyBody[tail, a].col, colorTable[3]); sammyBody[tail, a].row = 0; Set(sammy[a].row, sammy[a].col, sammy[a].scolor); } } }while (!playerDied); // reset speed to initial value curSpeed = speed; for (int a = 0; a < NumPlayers; a++) { EraseSnake(sammy, sammyBody, a); // If dead, then erase snake in really cool way if (!sammy[a].alive) { // Update score sammy[a].score = sammy[a].score - 10; PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); if (a == 0) { SpacePause(" Sammy Dies! Push Space! --->"); } else { SpacePause(" <---- Jake Dies! Push Space "); } } } Level(SAMELEVEL, sammy); PrintScore(NumPlayers, sammy[0].score, sammy[1].score, sammy[0].lives, sammy[1].lives); // Play next round, until either of snake's lives have run out. }while (sammy[0].lives != 0 && sammy[1].lives != 0); }