예제 #1
0
        private static bool PlayGame(ref Game game, GameView view)
        {
            bool gameOver = false;

            Console.Clear();
            UI.DrawScore(view, game);
            UI.DrawNextShapeField(view, game);
            UI.DrawPlayField(view, game);
            UI.DrawCurrentShape(view, game);
            UI.DrawNextShape(view, game);

            // Game Loop
            GameCommand command;
            DateTime    previousFall = DateTime.Now; // время предыдущего автоматического опускания фигуры

            bool quit = false;

            do
            {
                if (BL.IsShapeLanded(game.CurrentShape, game.Field))
                {
                    int   minShapeRow;
                    int   maxShapeRow;
                    int[] filledRows;

                    BL.AppendShape(game.CurrentShape, game.Field, out minShapeRow, out maxShapeRow);

                    if (BL.HasFilledRows(minShapeRow, maxShapeRow, game.Field, out filledRows))
                    {
                        game.Score.Score += BL.CalcScore(filledRows.Length);
                        if (game.Score.Level < (LEVEL_SCORE.Length - 1) && game.Score.Score >= LEVEL_SCORE[game.Score.Level + 1])
                        {
                            ++game.Score.Level;
                        }

                        UI.DrawScore(view, game);

                        int firstRowToFall = BL.FirstEmptyRow(game.Field, filledRows[0] == 0 ? filledRows[0] : filledRows[0] - 1) + 1;

                        for (int i = 0; i < filledRows.Length; i++)
                        {
                            BL.RemoveFilledRow(firstRowToFall, filledRows[i], game.Field);
                            UI.DrawRows(firstRowToFall, filledRows[i], game.Field, view);

                            firstRowToFall++;
                            System.Threading.Thread.Sleep(FALL_ANIMATE_QUANTUM);
                        }
                    }

                    UI.ClearNextShape(view, game);
                    BL.SetNextShape(ref game);

                    if (BL.IsPositionPossible(game.Field, game.CurrentShape))
                    {
                        UI.DrawCurrentShape(view, game);
                        UI.DrawNextShape(view, game);
                    }
                    else
                    {
                        gameOver = true;
                    }
                }
                else
                {
                    if (IsTimeToFall(previousFall, LEVEL_DELAY[game.Score.Level]))
                    {
                        //автоматическое падение текущей фигуры
                        MoveShape(ref game, view, 0, 1);
                        previousFall = DateTime.Now;
                    }

                    if (UI.HasUserInput(out command))
                    {
                        if (command == GameCommand.QuitToMenu)
                        {
                            quit = true;
                        }
                        else
                        {
                            ExecuteCommand(command, ref game, view);
                        }
                    }
                }

                System.Threading.Thread.Sleep(GAME_QUANTUM);
            } while (!quit && !gameOver);

            return(gameOver);
        }