private static bool TryMoveShape(ref Game g, GameView view, int horizontalShift, int verticalShift) { bool result = false; if (BL.IsPositionPossible(g.Field, g.CurrentShape, horizontalShift, verticalShift)) { MoveShape(ref g, view, horizontalShift, verticalShift); result = true; } return(result); }
private static void ExecuteCommand(GameCommand command, ref Game g, GameView view) { switch (command) { case GameCommand.MoveLeft: TryMoveShape(ref g, view, -1, 0); break; case GameCommand.MoveRight: TryMoveShape(ref g, view, 1, 0); break; case GameCommand.MoveDown: TryMoveShape(ref g, view, 0, 1); break; case GameCommand.Land: while (TryMoveShape(ref g, view, 0, 1)) { System.Threading.Thread.Sleep(GAME_QUANTUM); } break; case GameCommand.Rotate: Shape rotated = BL.RotateShape(g.CurrentShape); if (BL.IsPositionPossible(g.Field, rotated)) { Point2D[] makeEmpty; Point2D[] makeFilled; BL.CalcChangedPoints(g.CurrentShape, rotated, out makeEmpty, out makeFilled); g.CurrentShape = rotated; UI.DrawPoints(makeEmpty, ShapeKind.Empty, view); UI.DrawPoints(makeFilled, g.CurrentShape.Kind, view); } break; } }
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); }