Exemple #1
0
 public void PrintBoard(Scoreboard scoreBoard, Board board)
 {
     Console.CursorVisible = false;
     for (int y = 0; y < Board.boardHeight; y++)
     {
         Console.SetCursorPosition(0, y);
         Console.Write("*");
         for (int x = 0; x < Board.boardWidth; x++)
         {
             if (board.wholeBoard[y][x].val == 1)
             {
                 Console.BackgroundColor = board.wholeBoard[y][x].color;
                 Console.Write("  ");
                 Console.ResetColor();
             }
             else if (x >= board.fallingPoint.x && x < (board.fallingPoint.x + 4) && y >= board.fallingPoint.y && y < (board.fallingPoint.y + 4))
             {
                 if (board.currentPiece.pieceStore[y - board.fallingPoint.y, x - board.fallingPoint.x] == 1)
                 {
                     Console.BackgroundColor = board.currentPiece.GetShapeColor(board.currentBlock);
                     Console.Write("  ");
                     Console.ResetColor();
                 }
                 else
                 {
                     Console.BackgroundColor = ConsoleColor.Black;
                     Console.Write("  ");
                     Console.ResetColor();
                 }
             }
             else
             {
                 Console.BackgroundColor = ConsoleColor.Black;
                 Console.Write("  ");
                 Console.ResetColor();
             }
         }
         Console.SetCursorPosition(Board.boardWidth * 2 + 1, y);
         Console.Write("*");
         Console.WriteLine();
     }
     Console.SetCursorPosition(0, Board.boardHeight);
     Console.WriteLine("##########################################");
 }
Exemple #2
0
        private static void Main(string[] args)
        {
            //background music;
            WMPLib.WindowsMediaPlayer WinMediaPlayer = new WMPLib.WindowsMediaPlayer();
            (WinMediaPlayer.settings as WMPLib.IWMPSettings).setMode("loop", true);
            WinMediaPlayer.URL = "start.mp3";
            WinMediaPlayer.controls.play();

            Console.Title = "Tetris";
            string title = @"
 ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄
▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
 ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀█░▌ ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀
     ▐░▌     ▐░▌               ▐░▌     ▐░▌       ▐░▌     ▐░▌     ▐░▌
     ▐░▌     ▐░█▄▄▄▄▄▄▄▄▄      ▐░▌     ▐░█▄▄▄▄▄▄▄█░▌     ▐░▌     ▐░█▄▄▄▄▄▄▄▄▄
     ▐░▌     ▐░░░░░░░░░░░▌     ▐░▌     ▐░░░░░░░░░░░▌     ▐░▌     ▐░░░░░░░░░░░▌
     ▐░▌     ▐░█▀▀▀▀▀▀▀▀▀      ▐░▌     ▐░█▀▀▀▀█░█▀▀      ▐░▌      ▀▀▀▀▀▀▀▀▀█░▌
     ▐░▌     ▐░▌               ▐░▌     ▐░▌     ▐░▌       ▐░▌               ▐░▌
     ▐░▌     ▐░█▄▄▄▄▄▄▄▄▄      ▐░▌     ▐░▌      ▐░▌  ▄▄▄▄█░█▄▄▄▄  ▄▄▄▄▄▄▄▄▄█░▌
     ▐░▌     ▐░░░░░░░░░░░▌     ▐░▌     ▐░▌       ▐░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌
      ▀       ▀▀▀▀▀▀▀▀▀▀▀       ▀       ▀         ▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀

";

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(title, Console.ForegroundColor);
            Console.WriteLine();
            Console.WriteLine("                            Awesome    Tetris   Game\n");
            Console.ResetColor();
            Console.WriteLine("                              Press ENTER to start\n\n");
            Console.WriteLine($"Arrow Keys to Move    LRotate: A    RRotate: D    Pause: Spacebar    Exit: Esc\n\n");
            int savedScore = 0;

            try
            {
                string str = File.ReadAllText("score.txt");
                savedScore = Convert.ToInt32(str);
            }
            catch (Exception)
            {
                savedScore = 0;
            }
            Console.WriteLine("Highest score: " + savedScore);

            //start the game
            while (true)
            {
                Console.Read();
                Console.Clear();
                WinMediaPlayer.URL = "Tetris.mp3";
                WinMediaPlayer.controls.play();
                Console.SetWindowSize(100, 50);
                Console.SetBufferSize(100, 80);
                board = new Board();
                board.PlaceBlock();
                displayBoard = new Display();
                scoreBoard   = new Scoreboard(board);
                displayBoard.PrintBoard(scoreBoard, board);
                scoreBoard.UpdateLevel(board);
                SetTimer();
                while (true)
                {
                    // when Game is over
                    if (!board.isInGame)
                    {
                        (WinMediaPlayer.settings as WMPLib.IWMPSettings).setMode("loop", false);
                        WinMediaPlayer.URL = "gameover.mp3";
                        WinMediaPlayer.controls.play();
                        break;
                    }
                    ;

                    //check to see if the current timer interval matches the level changes
                    //increase the dropping speed when level goes up
                    if (scoreBoard.UpdateInterval(scoreBoard.Levels) != aTimer.Interval)
                    {
                        aTimer.Interval = scoreBoard.UpdateInterval(scoreBoard.Levels);
                    }

                    // two consoleLocks in the main class
                    // the second thread will wait untill the first one is complete
                    lock (consoleLock)
                    {
                        displayBoard.PrintBoard(scoreBoard, board);
                        displayBoard.PrintScoreBoard(scoreBoard, board);
                    }

                    Thread.Sleep(200); //allow main thread delay for 0.2 second
                    while (Console.KeyAvailable)
                    {
                        switch (Console.ReadKey(true).Key)
                        {
                        case ConsoleKey.LeftArrow:
                            board.keyPress(Board.Key.Left);
                            break;

                        case ConsoleKey.RightArrow:
                            board.keyPress(Board.Key.Right);
                            break;

                        case ConsoleKey.DownArrow:
                            board.keyPress(Board.Key.Down);
                            break;

                        case ConsoleKey.A:
                            board.keyPress(Board.Key.rLeft);
                            break;

                        case ConsoleKey.D:
                            board.keyPress(Board.Key.rRight);
                            break;

                        case ConsoleKey.Spacebar:
                            aTimer.Enabled = aTimer.Enabled ? false : true;     // Pause key, when aTimer.Enabled is true ==> false (Pause the game)
                            break;

                        case ConsoleKey.Escape:
                            Environment.Exit(0);
                            break;

                        default:
                            break;
                        }
                    }
                }
                //aTimer.Dispose();
            }
        }
Exemple #3
0
        public void PrintScoreBoard(Scoreboard scoreBoard, Board board)
        {
            Console.CursorVisible = false;
            Console.SetCursorPosition(50, 1);
            Console.WriteLine("#############################");
            Console.Title = "Timer";
            string title1 = @"             ___     ___";
            string title2 = @"       |\ | |__  \_/  |";
            string title3 = @"       | \| |___ / \  |";

            Console.SetCursorPosition(50, 2);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(title1, Console.ForegroundColor);
            Console.SetCursorPosition(50, 3);
            Console.WriteLine(title2, Console.ForegroundColor);
            Console.SetCursorPosition(50, 4);
            Console.WriteLine(title3, Console.ForegroundColor);
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Red;
            PrintBlock(board);
            Console.ResetColor();
            Console.Title = "Score";
            string score1 = @"     __   __   __   __   ___";
            string score2 = @"    /__` /  ` /  \ |__) |__";
            string score3 = @"    .__/ \__, \__/ |  \ |___";

            Console.SetCursorPosition(50, 10);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(score1, Console.ForegroundColor);
            Console.SetCursorPosition(50, 11);
            Console.WriteLine(score2, Console.ForegroundColor);
            Console.SetCursorPosition(50, 12);
            Console.WriteLine(score3, Console.ForegroundColor);
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(50, 15);
            int currentLevel = scoreBoard.UpdateLevel(board);

            Console.WriteLine($"               {scoreBoard.UpdateScore(currentLevel, board)}");
            Console.ResetColor();
            Console.Title = "Level";
            string level1 = @"         ___       ___   ";
            string level2 = @"    |    |__  \  / |__  | ";
            string level3 = @"    |___ |___  \/  |___ |___";

            Console.SetCursorPosition(50, 17);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(level1, Console.ForegroundColor);
            Console.SetCursorPosition(50, 18);
            Console.WriteLine(level2, Console.ForegroundColor);
            Console.SetCursorPosition(50, 19);
            Console.WriteLine(level3, Console.ForegroundColor);
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(50, 22);
            Console.WriteLine($"               {scoreBoard.UpdateLevel(board)}");
            Console.ResetColor();
            Console.Title = "Lines";
            string lines1 = @"                  ___  __";
            string lines2 = @"     |    | |\ | |__  /__` ";
            string lines3 = @"     |___ | | \| |___ .__/ ";

            Console.SetCursorPosition(50, 23);
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine(lines1, Console.ForegroundColor);
            Console.SetCursorPosition(50, 24);
            Console.WriteLine(lines2, Console.ForegroundColor);
            Console.SetCursorPosition(50, 25);
            Console.WriteLine(lines3, Console.ForegroundColor);
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(50, 28);
            Console.WriteLine($"               {board.removedRows}");
            Console.ResetColor();
            Console.SetCursorPosition(50, 30);
            Console.WriteLine("#############################");
        }