/// <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); } }