コード例 #1
0
ファイル: MainForm.cs プロジェクト: ClassTerr/Game2048
        /// <summary>
        ///     Здесь происходит отрисовка всего в игре.
        /// </summary>
        /// <param name="g"></param>
        private void Draw(Graphics g)
        {
            lock (_lockObject)
            {
                g.Clear(_settingsPack.BackCol);

                g.FillRectangle(new SolidBrush(_settingsPack.FieldCol), _fieldLeft, _fieldTop,
                                _settingsPack.FieldSize * _cellSize, _settingsPack.FieldSize * _cellSize);
                for (int i = 0; i <= _settingsPack.FieldSize; i++)
                {
                    g.DrawLine(new Pen(_settingsPack.GridCol), _fieldLeft + i * _cellSize, _fieldTop,
                               _fieldLeft + i * _cellSize, _fieldTop + _settingsPack.FieldSize * _cellSize);
                }

                for (int i = 0; i <= _settingsPack.FieldSize; i++)
                {
                    g.DrawLine(new Pen(_settingsPack.GridCol), _fieldLeft, _fieldTop + i * _cellSize,
                               _fieldLeft + _cellSize * _settingsPack.FieldSize, _fieldTop + i * _cellSize);
                }

                float l, t, renderSize, centerX, centerY;
                foreach (Block b in _settingsPack.Blocks)
                {
                    int opacity = 255;
                    renderSize = _cellSize * b.ScaleCoefficient;
                    centerX    = b.X + _cellSize / 2;
                    centerY    = b.Y + _cellSize / 2;
                    l          = centerX - renderSize / 2;
                    t          = centerY - renderSize / 2;

                    Color col;
                    if (!b.IsMain)
                    {
                        double dist =
                            Math.Sqrt(Math.Pow(b.X - b.TargetBlock.X, 2) + Math.Pow(b.Y - b.TargetBlock.Y, 2));
                        double targ = dist / _cellSize + 0.5f;
                        b.ScaleCoefficient = (float)Math.Min(1, targ);
                        opacity            = (int)Math.Min(255, targ * 255);
                        col = Color.FromArgb(opacity, b.Color);
                    }
                    else
                    {
                        col = b.Color;
                    }

                    g.FillPath(new SolidBrush(col), b.GetShape(l, t, renderSize));
                    FontInfo fi = Fun.GetFontInfo(b.Text, renderSize, _settingsPack.FontName, g);
                    Font     f  = new Font(_settingsPack.FontName, fi.Size);

                    if (fi.Width > fi.Height)
                    {
                        t += (renderSize - fi.Height) / 2;
                    }
                    else
                    {
                        l += (renderSize - fi.Width) / 2;
                    }


                    g.DrawString(b.Text, f, new SolidBrush(Color.FromArgb(opacity, Color.Black)), l, t);
                }

                foreach (Block b in _disappearingBlocks)
                {
                    renderSize = _cellSize * b.ScaleCoefficient;
                    centerX    = b.X + _cellSize / 2;
                    centerY    = b.Y + _cellSize / 2;
                    l          = centerX - renderSize / 2;
                    t          = centerY - renderSize / 2;
                    g.FillPath(new SolidBrush(b.Color), b.GetShape(l, t, renderSize));
                    FontInfo fi = Fun.GetFontInfo(b.Text, renderSize, _settingsPack.FontName, g);
                    Font     f  = new Font(_settingsPack.FontName, fi.Size);

                    if (fi.Width > fi.Height)
                    {
                        t += (renderSize - fi.Height) / 2;
                    }
                    else
                    {
                        l += (renderSize - fi.Width) / 2;
                    }


                    g.DrawString(b.Text, f, Brushes.Black, l, t);
                }
            }
        }