private void showHelp() { Console.Clear(); ChangeConsoleColors.to_info_ForegroundColor(); ChangeConsoleColors.to_info_BackgroundColor(); Console.SetCursorPosition(0, 5); Console.Write("\tControls:\n"); Console.Write("\tUp: Rotate\n"); Console.Write("\tLeft: Move Left\n"); Console.Write("\tRight: Move Right\n"); Console.Write("\tDown: Move Down by three lines\n\n"); Console.Write("\tRules:\n"); Console.Write("\tJust try to fill up every row.\n" + "\tPoints are earned one by one by filling up every row.\n" + "\tLevel increased by earning every 5 Points.\n" + "\tThe falling Down speed of every shape will be increased\n" + "\twith the increment of Level\n"); Console.SetCursorPosition((Console.WindowWidth / 2) - 12, Console.CursorTop + 2); Console.Write("press any key to get back ......"); Console.ReadKey(); ChangeConsoleColors.to_Console_Default_BackgroundColor(); ChangeConsoleColors.to_Console_Default_ForegroundColor(); }
/// <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) { ; } }
private void showCredits() { Console.Clear(); ChangeConsoleColors.to_info_BackgroundColor(); ChangeConsoleColors.to_info_ForegroundColor(); Console.SetCursorPosition(10, 5); Console.Write("Mohaimin Morsalin"); Console.SetCursorPosition(10, Console.CursorTop + 1); Console.Write("United International University (CSE 121)"); Console.SetCursorPosition(10, Console.CursorTop + 1); Console.Write("Dhaka, Bangladesh."); Console.SetCursorPosition(10, Console.CursorTop + 1); Console.Write("Email: [email protected]"); Console.SetCursorPosition((Console.WindowWidth / 2) - 12, Console.CursorTop + 2); Console.Write("press any key to get back ......"); Console.ReadKey(); ChangeConsoleColors.to_Console_Default_BackgroundColor(); ChangeConsoleColors.to_Console_Default_ForegroundColor(); }
/// <summary> /// moveNavigator(string) a function to manage the movement of the navigator('->') through menu. /// /// parameters: /// direction: Indicating the direction (Up or Down) where the navigator will move. /// </summary> private void moveNavigator(string direction) { clearNavigator(27, Console.CursorTop); ChangeConsoleColors.change_LineColor(30, Console.CursorTop, menuNames[Console.CursorTop - 2], ChangeConsoleColors.defaultForeground); if (direction == "Down") { Console.CursorTop++; if (Console.CursorTop > 5) { Console.CursorTop = 2; } } else if (direction == "Up") { Console.CursorTop--; if (Console.CursorTop < 2) { Console.CursorTop = 5; } } ChangeConsoleColors.change_LineColor(30, Console.CursorTop, menuNames[Console.CursorTop - 2], ChangeConsoleColors.navigatorForeground); showNavigator(27, Console.CursorTop); }
/// <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> /// invalidInputMessage(int, int) a function to show/display the warning after invalid input in console screen. /// /// parameters: /// left: indicating the left posotion of the console window. /// top: indicating the top posotion of the console window. /// </summary> private void invalidInputMessage(int left, int top) { Console.Clear(); ChangeConsoleColors.to_error_ForegroundColor(); Console.SetCursorPosition(left, top); Console.WriteLine("{0} is not a valid input!!!", input.KeyChar); ChangeConsoleColors.to_Console_Default_ForegroundColor(); }
/// <summary> /// showNavigator(int, int) a function to show/display the navigator in console screen. /// /// parameters: /// left: indicating the left posotion of the console window. /// top: indicating the top posotion of the console window. /// </summary> private void showNavigator(int left, int top) { ChangeConsoleColors.to_Navigator_ForegroundColor(); Console.SetCursorPosition(left, top); Console.Write("->"); Console.SetCursorPosition(left - 1, top); ChangeConsoleColors.to_Console_Default_ForegroundColor(); }
/// <summary> /// adjustConsole() function will adjust and manage the console screen /// such as foreground background color, window size, cursor visibility /// </summary> private static void adjustConsole() { Console.Title = "Tetris Game"; ChangeConsoleColors.to_Console_Default_BackgroundColor(); ChangeConsoleColors.to_Console_Default_ForegroundColor(); Console.SetWindowSize(80, 45); Console.CursorVisible = false; }
/// <summary> /// showMenu() a function to print/display the main menu in Console screen. /// </summary> public void showMenu() { Console.Clear(); Console.SetCursorPosition(30, 0); Console.WriteLine("TETRIS GAME"); Console.SetCursorPosition(30, 2); Console.WriteLine(menuNames[0]); Console.SetCursorPosition(30, 3); Console.WriteLine(menuNames[1]); Console.SetCursorPosition(30, 4); Console.WriteLine(menuNames[2]); Console.SetCursorPosition(30, 5); Console.WriteLine(menuNames[3]); Console.SetCursorPosition(20, 8); Console.WriteLine("Press Arrow Keys: Navigate Options."); Console.SetCursorPosition(20, 9); Console.WriteLine("Press Enter: Select Menu."); showNavigator(27, 2); ChangeConsoleColors.change_LineColor(30, 2, menuNames[0], ChangeConsoleColors.navigatorForeground); }
/// <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); } }