コード例 #1
0
        private static void PrintFrame(Ball ball, Board board)
        {
            int currBallRow = ball.Row;
            int currBallCol = ball.Col;
            int currBoardRow = board.Row;
            matrixForGame[currBallRow, currBallCol] = ball;
            for (int i = 0; i < board.Size; i++)
            {
                matrixForGame[board.Row, board.Col + i] = board;
            }
            for (int rows = 0; rows < matrixForGame.GetLength(0); rows++)
            {
                for (int cols = 0; cols < matrixForGame.GetLength(1); cols++)
                {
                    Console.Write("  {0}  ", matrixForGame[rows, cols].GetCharOfObject());
                }
                Console.WriteLine();
            }
            Console.WriteLine(new string('-', matrixForGame.GetLength(0)*5));
            Console.WriteLine("    {0," + ((Console.WindowWidth / 2)) + "}", "Hello " + user + ", Good Luck! :)");

            Console.WriteLine(@"                  _________________________
                 //                       \\
                //       Lives = {1}         \\
               //                           \\
              //         Score = {0:000}         \\
             //_______________________________\\ ", score, lives);
            for (int i = 0; i < board.Size; i++)
            {
                matrixForGame[board.Row, board.Col + i] = new EmptyBlock();
            }
            matrixForGame[ball.Row, ball.Col] = new EmptyBlock();
        }
コード例 #2
0
        private static void Update(Ball ball, Board board)
        {
            ball.Col += ball.UpdateCol;
            ball.Row += ball.UpdateRow;

            //Colliding with left or right wall
            #region
            if (ball.Col >= matrixForGame.GetLength(1) - 1 || ball.Col <= 0)
            {
                ball.UpdateCol *= (-1);
                ball.Col += ball.UpdateCol;

            }
            #endregion
            //Colliding with ceiling
            #region
            if (ball.Row < 1)
            {
                ball.UpdateRow *= -1;
                ball.Row += ball.UpdateRow;

            }
            #endregion
            //Colliding with bricks
            #region
            if (matrixForGame[ball.Row, ball.Col].IsDestroyable)
            {
                //TO DO.. Implement Destroy
                if (matrixForGame[ball.Row, ball.Col] is Brick)
                {
                    //Console.Beep(3000,200);
                    score += 5;
                }
                if (matrixForGame[ball.Row, ball.Col] is SpecialBonusBrick)
                {
                    //Console.Beep(3000, 300);
                    score += 10;
                }
                matrixForGame[ball.Row, ball.Col] = new EmptyBlock();
                ball.UpdateRow *= -1;
                ball.Row += ball.UpdateRow;
                return;
            }
            #endregion
            //Colliding with board
            #region
            if (ball.Row >= board.Row && (ball.Col >= board.Col && ball.Col <= board.Col + board.Size))
            {
                ball.UpdateRow *= -1;
                ball.Row += ball.UpdateRow;
                ballBoardHits++;
            }
            #endregion
            if (ball.Row >= matrixForGame.GetLength(0) - 1)
            {
                lives--;
                // Console.Beep(658, 1250);

                ball.Row = matrixForGame.GetLength(0) - 1;
                ball.Col = matrixForGame.GetLength(1) / 2;
                ball.UpdateRow = -1;
                ball.UpdateCol = -1;
                board.Col = matrixForGame.GetLength(1) / 2 - board.Size / 2;
                Console.Clear();
                Thread.Sleep(1000);
            }
        }
コード例 #3
0
        private static void PlayGame(int level)
        {
            //The method returns the score
            matrixForGame = LoadLevel(level);
            Ball ball = new Ball(matrixForGame.GetLength(0) - 1, matrixForGame.GetLength(1) / 2);
            int boardRow = matrixForGame.GetLength(0) - 1;
            int boardCol = matrixForGame.GetLength(1) / 2;
            bool allBricksCleared = false;
            Board board = new Board(boardRow, boardCol);
            while (true)
            {
                if (ballBoardHits >= MAX_BALL_BOARD_HITS_BEFORE_BRICKS_DOWN)
                {
                    for (int row = matrixForGame.GetLength(0) - 1; row > 1; row--)
                    {
                        for (int col = 1; col < matrixForGame.GetLength(1) - 1; col++)
                        {
                            matrixForGame[row, col] = matrixForGame[row - 1, col];
                        }
                    }
                    ballBoardHits = 0;
                }
                if (lives < 1)
                {
                    //Read all the users
                    List<User> users = new List<User>();
                    users.Add(new User(user, score));
                    using (StreamReader scoreReader = new StreamReader(@"..\..\HighScore.txt"))
                    {
                        string lineRead = scoreReader.ReadLine();
                        while (!string.IsNullOrEmpty(lineRead))
                        {
                            users.Add(User.ParseUser(lineRead));
                            lineRead = scoreReader.ReadLine();
                        }
                    }
                    //Write the current user
                    using (StreamWriter sr = new StreamWriter(@"..\..\HighScore.txt"))
                    {
                        foreach (var userToWrite in users)
                        {
                            sr.WriteLine(userToWrite.ToString());
                        }
                    }
                    users.Clear();

                    lives = 3;
                    score = 0;
                    break;

                }
                Console.Clear();
                PrintFrame(ball, board);
                Update(ball, board);
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    switch (key.Key)
                    {

                        case ConsoleKey.LeftArrow:
                            if (board.Col >= 2)
                            {
                                board.Col--;
                            }
                            break;
                        case ConsoleKey.RightArrow:
                            if (board.Col + board.Size < matrixForGame.GetLength(1) - 1)
                            {
                                board.Col++;
                            }
                            break;

                        case ConsoleKey.P:
                            {
                                while (true)
                                {
                                ConsoleKeyInfo isPause = Console.ReadKey(true);
                                    if (isPause.Key == ConsoleKey.P)
                                    {
                                        break;
                                    }
                                    Console.ReadKey(true);
                                }
                            }
                            break;

                        case ConsoleKey.Escape:
                            {
                                Console.Clear();
                                DrawMenu();
                            }
                            break;
                    }
                }

                allBricksCleared = true;
                for (int row = 0; row < matrixForGame.GetLength(0); row++)
                {
                    for (int col = 0; col < matrixForGame.GetLength(1); col++)
                    {
                        if (matrixForGame[row, col].IsDestroyable)
                        {
                            allBricksCleared = false;
                            break;
                        }
                    }
                }
                if (allBricksCleared)
                {
                    break;
                }
                Thread.Sleep(150);
            }
            if (allBricksCleared)
            {
                PlayGame(level + 1);
            }
            else
            {
                AskRetry();
            }
        }