예제 #1
0
        public Bitmap CreateBoardImage(GameStateDTO gs)
        {
            var adjustPoint = new Func<Contracts.PointDTO, Point>(p => new Point(p.X, gs.BoardSize.Height - 1 - p.Y));
            var bitmap = new Bitmap(gs.BoardSize.Width*_cellOuterWidth + 1, gs.BoardSize.Height*_cellOuterWidth + 1);
            using (var g = Graphics.FromImage(bitmap))
            {
                // background
                g.FillRectangle(BackgroundBrush, 0, 0, bitmap.Width, bitmap.Height);
                Enumerable.Range(0, gs.BoardSize.Width + 1)
                    .ToList()
                    .ForEach(x => g.DrawLine(_gridPen, x*_cellOuterWidth, 0, x*_cellOuterWidth, bitmap.Height));
                Enumerable.Range(0, gs.BoardSize.Height + 1)
                    .ToList()
                    .ForEach(y => g.DrawLine(_gridPen, 0, y*_cellOuterWidth, bitmap.Width, y*_cellOuterWidth));

                gs.Food.Select(adjustPoint).ToList().ForEach(location =>
                {
                    var p = PointToGridCellCenter(location);
                    g.DrawLine(_foodPen, new PointF(p.X - 0.1f, p.Y), new PointF(p.X + 0.1f, p.Y));
                });

                gs.Walls.Select(adjustPoint).ToList()
                    .ForEach(
                        m =>
                        {
                            g.FillRectangle(_wallBrush, m.X*_cellOuterWidth + 1, m.Y*_cellOuterWidth + 1, _cellWidth,
                                _cellWidth);
                        });

                var snakes = gs.Snakes.ToList();
                for (var i = 0; i < snakes.Count; i++)
                {
                    var snake = snakes[i];
                    var cells = snake.Cells.Select(adjustPoint).ToList();
                    var head = adjustPoint(snake.Head);

                    if (!snake.IsAlive)
                    {
                        DrawSnakeLine(cells, g, _deadSnakeOutlinePen);
                    }
                    var tailPens = _snakeTailPens[i%_snakeTailPens.Length];
                    // If snake weigth is less than half of it maximum weight it is drown with thinner body
                    var tailPen =
                        tailPens[Math.Min(tailPens.Length - 1, 2*tailPens.Length*snake.Weight/snake.MaxWeight)];
                    DrawSnakeLine(cells, g, tailPen);

                    // head
                    var p = PointToGridCellCenter(cells.Last());
                    g.DrawLine(tailPens.Last(), new PointF(p.X - 0.1f, p.Y), new PointF(p.X + 0.1f, p.Y));
                    g.DrawLine(_snakeHeadOverlayPen, new PointF(p.X - 0.1f, p.Y), new PointF(p.X + 0.1f, p.Y));

                    g.DrawString(snake.Id, _snakeHeadFont, HeadFontBrush,
                        new RectangleF(head.X*_cellOuterWidth + 1, head.Y*_cellOuterWidth + 1, _cellWidth,
                            _cellWidth),_headLabelStringFormat);
                }
            }
            return bitmap;
        }
예제 #2
0
 private void DrawBoard(GameStateDTO gs)
 {
     pbGameView.Image?.Dispose();
     if (gs == null)
     {
         pbGameView.Image = null;
     }
     else
     {
         pbGameView.Image = _visualiser.CreateBoardImage(gs);
     }
 }