예제 #1
0
        private void DrawBlocks(TetrisGameState state)
        {
            if (state.Grid.Width > _canvas.Width - 2 * _borderWidth || state.Grid.Height > _canvas.Height - 2 * _borderWidth)
            {
                throw new Exception(
                          $"Game grid ({state.Grid.Width}x{state.Grid.Height}) does not fit inside " +
                          $"Canvas ({_canvas.Width}x{_canvas.Height}) with Border size ({_borderWidth})");
            }

            var grid = state.Grid.Blocks;

            if (state.ActiveBlock != null)
            {
                var block = state.ActiveBlock;
                grid.Imprint(block.Shape, block.Position);
            }

            for (var y = 0; y < grid.GetLength(1); y++)
            {
                for (var x = 0; x < grid.GetLength(0); x++)
                {
                    var type = grid[x, y];
                    if (type != 0)
                    {
                        var color = GetColor(type);
                        _canvas.SetPixel(x + _borderWidth, y + _borderWidth, color);
                    }
                }
            }
        }
예제 #2
0
        private void DrawScore(TetrisGameState state)
        {
            var xOffset = 1;
            var yOffset = state.Grid.Height + 2 * _borderWidth + 1;

            var digitRenderer = new DigitRenderer(_canvas, 4);

            digitRenderer.RenderNumber(xOffset, yOffset, state.Score);
        }
예제 #3
0
        public void Render(TetrisGameState state)
        {
            _canvas.Clear();

            DrawBorder(state);
            DrawBlocks(state);
            DrawScore(state);

            _matrix.SwapOnVsync(_canvas);
        }
예제 #4
0
        private void DrawBorder(TetrisGameState state)
        {
            if (_borderWidth == 0)
            {
                return;
            }

            var x2 = state.Grid.Width + 1;
            var y2 = state.Grid.Height + 1;

            var borderColor = new Color(50, 50, 50);

            _canvas.DrawLine(0, 0, 0, y2, borderColor);
            _canvas.DrawLine(0, 0, x2, 0, borderColor);
            _canvas.DrawLine(0, y2, x2, y2, borderColor);
            _canvas.DrawLine(x2, 0, x2, y2, borderColor);
        }
예제 #5
0
        private static void DrawBlocks(TetrisGameState state)
        {
            var grid = state.Grid.Blocks;

            if (state.ActiveBlock != null)
            {
                var block = state.ActiveBlock;
                grid.Imprint(block.Shape, block.Position);
            }

            var sb = new StringBuilder();

            for (var y = 0; y < grid.GetLength(1); y++)
            {
                sb.Append("| ");
                for (var x = 0; x < grid.GetLength(0); x++)
                {
                    var type = grid[x, y];
                    if (type != 0)
                    {
                        System.Console.ForegroundColor = GetColor(type);
                        sb.Append("\u2588\u2588");
                    }
                    else
                    {
                        sb.Append("  ");
                    }
                }
                System.Console.ResetColor();
                sb.AppendLine($"|{y}");
            }

            var hLine = "`" + new string(Enumerable.Repeat('-', grid.GetLength(0) * 2 + 1).ToArray()) + "`";

            sb.AppendLine(hLine);
            System.Console.SetCursorPosition(0, 0);
            System.Console.Write(sb.ToString());
        }
예제 #6
0
 private void DrawScore(TetrisGameState state)
 {
     var(x, y) = (_gridWidth + 2 + 5, 4);
     System.Console.SetCursorPosition(x, y);
     System.Console.Write("Score: " + state.Score);
 }
예제 #7
0
 public void Render(TetrisGameState state)
 {
     DrawBlocks(state);
     DrawScore(state);
 }