Beispiel #1
0
        /// <summary>
        /// Prepares next brick to be active brick.
        /// </summary>
        private void MakeNextBrickActive()
        {
            NextBrick.Erase();
            ActiveBrick = NextBrick;
            var pair = GameBoard.Labels.ElementAt(3);
            var x    = pair.Value[0] + pair.Key.Length / 2;
            var y    = pair.Value[1] + 1;

            ActiveBrick.Move(-x / GameBoard.BoardScaling[0], -y / GameBoard.BoardScaling[1]); // move to original position
            ActiveBrick.Move(GameBoard.TheoreticalSize[0] / 2 - 1, 0);                        // move to middle top of the board

            ActiveBrick.Write();
        }
Beispiel #2
0
        /// <summary>
        /// Handles key pressed by the player.
        /// </summary>
        /// <param name="key">Key pressed by the player.</param>
        private void ProcessUserInput(ConsoleKey key)
        {
            var    newPixels = new int[ActiveBrick.Pixels.GetLength(0)][];
            Action action    = null;

            switch (key)
            {
            case ConsoleKey.DownArrow:
                action    = new Action(() => ActiveBrick.Move(0, 1));
                newPixels = ActiveBrick.GetMovedPixels(0, 1);
                break;

            case ConsoleKey.LeftArrow:
                action    = new Action(() => ActiveBrick.Move(-1, 0));
                newPixels = ActiveBrick.GetMovedPixels(-1, 0);
                break;

            case ConsoleKey.RightArrow:
                action    = new Action(() => ActiveBrick.Move(1, 0));
                newPixels = ActiveBrick.GetMovedPixels(1, 0);
                break;

            case ConsoleKey.Spacebar:
                action    = new Action(() => ActiveBrick.Rotate());
                newPixels = ActiveBrick.GetRotatedPixels();
                break;

            case ConsoleKey.P:
                IsPaused = !IsPaused;
                break;

            default:
                break;
            }

            if (action != null && !IsPaused)
            {
                if (IsNewPositionValid(newPixels))
                {
                    ActiveBrick.Erase();
                    action.Invoke();
                    ActiveBrick.Write();
                    GameBoard.DrawFrame();
                }
                else if (key == ConsoleKey.Spacebar)
                {
                    var vector = new int[2];
                    if (newPixels.Any(p => p[0] < 0))
                    {
                        vector = new int[] { -(newPixels.Min(x => x[0]) - 1) / GameBoard.BoardScaling[0], 0 }
                    }
                    ;
                    if (newPixels.Any(p => p[0] > GameBoard.BoardScaling[0] * GameBoard.TheoreticalSize[0]))
                    {
                        vector = new int[] { (GameBoard.BoardScaling[0] * GameBoard.TheoreticalSize[0] - (newPixels.Max(x => x[0]) + 1)) / GameBoard.BoardScaling[0], 0 }
                    }
                    ;
                    if (newPixels.Any(p => p[1] < 1))
                    {
                        vector = new int[] { 0, 1 }
                    }
                    ;
                    foreach (var px in newPixels)
                    {
                        px[0] += vector[0] * GameBoard.BoardScaling[0];
                        px[1] += vector[1] * GameBoard.BoardScaling[1];
                    }
                    if (IsNewPositionValid(newPixels))
                    {
                        ActiveBrick.Erase();
                        action.Invoke();
                        ActiveBrick.Move(vector[0], vector[1]);
                        ActiveBrick.Write();
                        GameBoard.DrawFrame();
                    }
                }
            }
        }