/// <summary> /// Will show/display the score and a game over message when game is over. /// </summary> private void showGameOverMessage() { ChangeConsoleColors.to_info_BackgroundColor(); ChangeConsoleColors.to_info_ForegroundColor(); Thread.Sleep(500); Console.Clear(); Console.SetCursorPosition((Console.WindowWidth / 2) - 4, Console.WindowTop + 5); Console.WriteLine("Game Over!"); Console.SetCursorPosition((Console.WindowWidth / 2) - 6, Console.WindowTop + 7); Console.Write("Your Score: {0}", score); Console.SetCursorPosition((Console.WindowWidth / 2) - 12, Console.WindowTop + 9); Console.Write("Press Esc key to get back...."); ChangeConsoleColors.to_Bucket_BackgroundColor(); ChangeConsoleColors.to_Bucket_ForegroundColor(); while (Console.ReadKey(true).Key != ConsoleKey.Escape) { ; } }
/// <summary> /// Manage the Pausing operation. /// Paused when user press P. /// Resume when user press P again. /// </summary> private void PausingOperation() { gamePaused = !gamePaused; if (gamePaused) { ChangeConsoleColors.to_info_BackgroundColor(); ChangeConsoleColors.to_info_ForegroundColor(); Console.SetCursorPosition(((Bucket.rightEnd + Bucket.leftEnd) / 2) - 3, Bucket.topEnd - 1); Console.Write("Paused!"); Console.SetCursorPosition(((sidebarLeft + sidebarRight) / 2) - 8, sidebarBottom - 2); Console.Write("Press P to Resume."); ChangeConsoleColors.to_Bucket_BackgroundColor(); ChangeConsoleColors.to_Bucket_ForegroundColor(); } else { ChangeConsoleColors.to_info_BackgroundColor(); ChangeConsoleColors.to_info_ForegroundColor(); Console.SetCursorPosition(((Bucket.rightEnd + Bucket.leftEnd) / 2) - 3, Bucket.topEnd - 1); Console.Write(" "); Console.SetCursorPosition(((sidebarLeft + sidebarRight) / 2) - 8, sidebarBottom - 2); Console.Write("Press P to Pause. "); ChangeConsoleColors.to_Bucket_BackgroundColor(); ChangeConsoleColors.to_Bucket_ForegroundColor(); } }
/// <summary> /// startGame() a function that will build bucket and sidebar, /// change the console foreground background color by calling the respective function. /// Then let user play the game by calling play() function. /// After playing it changes the score, level and console color to their initial value. /// </summary> public void startGame() { Bucket.buildBucket(); score = 0; level = 1; buildSidebar(); ChangeConsoleColors.to_Bucket_BackgroundColor(); ChangeConsoleColors.to_Bucket_ForegroundColor(); playGame(); ChangeConsoleColors.to_Console_Default_BackgroundColor(); ChangeConsoleColors.to_Console_Default_ForegroundColor(); }
/// <summary> /// Just coloring the bucket Area. /// </summary> private static void colorBucketArea() { ChangeConsoleColors.to_Bucket_BackgroundColor(); for (int i = 1; i < bottomEnd - topEnd; i++) { Console.SetCursorPosition(leftEnd, topEnd + i); for (int j = 1; j <= rightEnd - leftEnd + 1; j++) { Console.Write(" "); } } ChangeConsoleColors.to_Console_Default_BackgroundColor(); }
/// <summary> /// buildSidebar() building the sidbar which will show info about /// next shape, score, level. /// </summary> private void buildSidebar() { ChangeConsoleColors.to_info_ForegroundColor(); ChangeConsoleColors.to_info_BackgroundColor(); Console.SetCursorPosition(sidebarLeft, sidebarTop); for (int i = sidebarLeft; i <= sidebarRight; i++) { Console.Write('_'); } for (int i = sidebarTop + 1; i <= sidebarBottom; i++) { Console.SetCursorPosition(sidebarLeft - 1, i); Console.Write('|'); } for (int i = sidebarTop + 1; i <= sidebarBottom; i++) { Console.SetCursorPosition(sidebarRight + 1, i); Console.Write('|'); } Console.SetCursorPosition(sidebarLeft, sidebarBottom); for (int i = sidebarLeft; i <= sidebarRight; i++) { Console.Write('_'); } Console.SetCursorPosition(((sidebarLeft + sidebarRight) / 2) - 2, sidebarTop + 1); Console.Write("Next"); Console.SetCursorPosition(sidebarLeft + 2, sidebarTop + 9); Console.Write("Score: {0}", score); Console.SetCursorPosition(sidebarLeft + 2, sidebarTop + 11); Console.Write("Level: {0}", level); Console.SetCursorPosition(((sidebarLeft + sidebarRight) / 2) - 8, sidebarBottom - 2); Console.Write("Press P to Pause."); Console.SetCursorPosition(((sidebarLeft + sidebarRight) / 2) - 8, sidebarBottom - 1); Console.Write("Press Esc to Quit."); ChangeConsoleColors.to_Bucket_ForegroundColor(); ChangeConsoleColors.to_Bucket_BackgroundColor(); }
/// <summary> /// updateSidebar(TetrisShapes) update the sidebar info (next shape, score, level) /// after a shape completely fall down. /// /// parameters: /// TetrisShapes shape: indicating the shape which need to be removed from sidebar info panel. /// </summary> private void updateSidebar(TetrisShapes shape) { ChangeConsoleColors.to_info_ForegroundColor(); ChangeConsoleColors.to_info_BackgroundColor(); shape.removeShape(shape.getShape(0), ((sidebarLeft + sidebarRight) / 2) - 2, sidebarTop + 3); Console.SetCursorPosition(sidebarLeft, sidebarTop + 9); Console.Write(new string(' ', sidebarRight - sidebarLeft + 1)); Console.SetCursorPosition(sidebarLeft, sidebarTop + 11); Console.Write(new string(' ', sidebarRight - sidebarLeft + 1)); Console.SetCursorPosition(sidebarLeft + 2, sidebarTop + 9); Console.Write("Score: {0}", score); Console.SetCursorPosition(sidebarLeft + 2, sidebarTop + 11); Console.Write("Level: {0}", level); ChangeConsoleColors.to_Bucket_ForegroundColor(); ChangeConsoleColors.to_Bucket_BackgroundColor(); }
/// <summary> /// playGame() this function will control the entire game playing cases. /// This function will generate shape, print it in console screen, move the shape according /// to user inputs, move down the shape by one step after every time interval, update the sidebar /// info, check for game over and end the game if it is over. /// /// The game will continue until game is not over and uset doesn't quit. (see Lines from 146 to 226). /// In this loop it will generate shape, display it in console if game is not over and start the stopwatch /// to measure the time interval of falling shape down and also... /// there is another loop in it which manage shape falling (see Lines from 169 to 224). /// This loop will continue until the shape can move down without facing any obstacle or reach the bottom boundary. /// while falling it will check for any available user's input (Console.KeyAvailable) in input stream and if find any /// then do the respective job by calling respective function. /// Also it will check for time interval in stopwacth to move shape downwards or not. /// If stop watch time reach or pass the time inteval it will move the shape one step down by calling the movedown() /// function then reset and restart the stopwatch. /// If a shape can't move down any more step it will exit the loop then update grid by calling updateGrid(), /// update sidebar by calling updateSidebar() /// /// The first loop will exit when game over condition is reached or user quit it. /// Then this Function end the game and return to it's Caller function. /// </summary> private void playGame() { gameOver = false; gamePaused = false; userQuit = false; TetrisShapes nextShape = generateShape(randomShape.Next(7)); while (!gameOver && !userQuit) { fallingShape = true; currentShape = nextShape; currentShape.PrintShape(currentShape.getShape(0), currentShape.left, currentShape.top); nextShape = generateShape(randomShape.Next(7)); if (!isGameOver(currentShape.getShape(0), currentShape.left, currentShape.top)) { ChangeConsoleColors.to_info_ForegroundColor(); ChangeConsoleColors.to_info_BackgroundColor(); nextShape.PrintShape(nextShape.getShape(0), ((sidebarLeft + sidebarRight) / 2) - 2, sidebarTop + 3); ChangeConsoleColors.to_Bucket_ForegroundColor(); ChangeConsoleColors.to_Bucket_BackgroundColor(); stopwatch.Start(); } else { showGameOverMessage(); fallingShape = false; gameOver = true; } while (fallingShape && !userQuit) { if (Console.KeyAvailable) { pressedKey = Console.ReadKey(true); if (!gamePaused && pressedKey.Key == ConsoleKey.UpArrow && currentShape.canRotate()) { currentShape.RotateShape(); } else if (!gamePaused && pressedKey.Key == ConsoleKey.LeftArrow && currentShape.canMoveLeft()) { currentShape.moveLeft(); } else if (!gamePaused && pressedKey.Key == ConsoleKey.RightArrow && currentShape.canMoveRight()) { currentShape.moveRight(); } else if (!gamePaused && pressedKey.Key == ConsoleKey.DownArrow) { if (currentShape.canMoveDown(3)) { currentShape.moveDown(); } else { fallingShape = false; Bucket.updateGrid(currentShape.getShape(currentShape.currentRotation), currentShape.left, currentShape.top, TetrisShapes.shapeChar); } } else if (pressedKey.Key == ConsoleKey.P) { PausingOperation(); } else if (pressedKey.Key == ConsoleKey.Escape) { fallingShape = false; userQuit = true; } } if (!gamePaused && stopwatch.ElapsedMilliseconds >= fallingTimeInterval - ((level - 1) * 100)) { if (currentShape.canMoveDown(1)) { currentShape.moveDown(); } else { fallingShape = false; Bucket.updateGrid(currentShape.getShape(currentShape.currentRotation), currentShape.left, currentShape.top, TetrisShapes.shapeChar); } stopwatch.Reset(); stopwatch.Start(); } } updateSidebar(nextShape); } }