c#-中国象棋:画棋盘

 先画出了棋盘,效果类似QQ游戏里面的,程序如下:

 

namespace ChineseChess
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }       

        //棋盘参数
        private const int _outterStartLeft = 25;//棋盘的外框距离panel的左边距
        private const int _outterStartTop = 25;//棋盘的外框距离panel的上边距
        private const int _distance = 6;//棋盘的内框与外框的距离
        private const int _riverFontFromBorder = 40;//“楚河”或“汉界”距离边框的长度
        private const int _locationLength=10;//标示“炮”“卒”位置的线长度
        private const int _locationFromBorder = 2;//标示“炮”“卒”位置的线

        //画笔
        Pen drawOutterRecPen = new Pen(Color.Black, 4);//棋盘外边框用到的画笔黑色4pt
        Pen drawInnerRecPen = new Pen(Color.Black, 2);//棋盘内边框用黑色2pt画笔
        Pen drawBasicLine = new Pen(Color.Black, 1);//棋盘基线用黑色1pt画笔

        //字体
        Font drawRiverFont = new Font("幼圆", 25);

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics gh = e.Graphics;//获取绘制图形对象

            //画棋盘边框

            int _chessBoardWidth = panel1.Width - 2 * _outterStartLeft - 2 * _distance;//棋盘的宽度
            int _chessBoardHeight = panel1.Height - 2 * _outterStartTop - 2 * _distance;//棋盘的高度
            //画棋盘外边框
            gh.DrawRectangle(drawOutterRecPen, new Rectangle(new Point(_outterStartLeft, _outterStartTop),new Size(_chessBoardWidth+2*_distance,_chessBoardHeight+2*_distance)));
            //画棋盘内边框
            gh.DrawRectangle(drawInnerRecPen, new Rectangle(new Point(_outterStartLeft + _distance, _outterStartTop + _distance), new Size(_chessBoardWidth,_chessBoardHeight)));


            //画棋盘

            //画棋盘线
            int _xStart = _outterStartLeft + _distance;//线的起始x坐标
            int _yStart = _outterStartTop + _distance;//线的起始x坐标
            int gridWidth=(panel1.Width-2*_outterStartLeft-2*_distance)/8;
            int gridHeight=(panel1.Height-2*_outterStartTop-2*_distance)/9;
            //画竖线(8列7条线)
            for (int i = 1; i <= 7; i++)
            {
                //分开画,避开“楚河汉界”一行的竖线
                gh.DrawLine(drawBasicLine, new Point(_xStart + i * gridWidth, _yStart), new Point(_xStart + i * gridWidth, _yStart + 4*gridHeight));
                gh.DrawLine(drawBasicLine,new Point(_xStart + i * gridWidth,_yStart + 5*gridHeight),new Point(_xStart + i * gridWidth,_yStart+_chessBoardHeight));
            }
            //画横线(9行8条线)
            for (int i = 1; i <= 8; i++)
            {
                gh.DrawLine(drawBasicLine, new Point(_xStart, _yStart + i * gridHeight), new Point(_xStart + _chessBoardWidth, _yStart + i * gridHeight));
            }
            //画“楚河汉界”
            SizeF fRiver = gh.MeasureString("楚 河", drawRiverFont);
            SizeF fBound = gh.MeasureString("汉 界", drawRiverFont);
            gh.DrawString("楚 河", drawRiverFont, Brushes.Black, new PointF(_xStart + _chessBoardWidth - _riverFontFromBorder - fRiver.Width, _yStart + 4 * gridHeight + (float)(gridHeight - fRiver.Height) / 2));
            gh.DrawString("汉 界", drawRiverFont, Brushes.Black, new PointF(_xStart + _riverFontFromBorder, _yStart + 4 * gridHeight + (float)(gridHeight - fBound.Height) / 2));
            //画将/帅的皇宫
            gh.DrawLine(drawBasicLine, new Point(_xStart + 3 * gridWidth, _yStart), new Point(_xStart + 5 * gridWidth, _yStart + 2 * gridHeight));
            gh.DrawLine(drawBasicLine, new Point(_xStart + 3 * gridWidth, _yStart + 2 * gridHeight), new Point(_xStart + 5 * gridWidth, _yStart));

            gh.DrawLine(drawBasicLine, new Point(_xStart + 3 * gridWidth, _yStart+7*gridHeight), new Point(_xStart + 5 * gridWidth, _yStart + _chessBoardHeight));
            gh.DrawLine(drawBasicLine, new Point(_xStart + 3 * gridWidth, _yStart + _chessBoardHeight), new Point(_xStart + 5 * gridWidth, _yStart + 7 * gridHeight));
            //画“炮”的位置
            for (int i = 0; i <= 1; i++)
            {
                for (int j = 0; j <= 1; j++)
                {
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + gridWidth - _locationLength - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationLength - _locationFromBorder+i*5*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + gridWidth - _locationLength - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + 2 * _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + 2 * _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth - _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + _locationLength + 2 * _locationFromBorder+i*5*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + gridWidth + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + _locationLength + 2 * _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + 2 * _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth + _locationLength + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight + 2 * _locationFromBorder+i*5*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + gridWidth + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationLength - _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationFromBorder+i*5*gridHeight),
                        new Point(_xStart + gridWidth + _locationLength + 2 * _locationFromBorder + j * 6 * gridWidth, _yStart + 2 * gridHeight - _locationFromBorder+i*5*gridHeight) });

                }
            }
            //画“卒”的位置
            for (int i = 0; i <= 1; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + 2*gridWidth - _locationLength - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationLength - _locationFromBorder+i*3*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + 2*gridWidth - _locationLength - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth - _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + _locationLength + 2 * _locationFromBorder+i*3*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + 2*gridWidth + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + _locationLength + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth + _locationLength + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight) });
                    gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + 2*gridWidth + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationLength - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart + 2*gridWidth + _locationLength + 2 * _locationFromBorder + j *2 * gridWidth, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight) });

                }
            }
            for (int i = 0; i <= 1; i++)
            {
                gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + 2 * _locationFromBorder, _yStart + 3 * gridHeight + _locationLength + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart  + 2 * _locationFromBorder, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart  + _locationLength + 2 * _locationFromBorder, _yStart + 3 * gridHeight + 2 * _locationFromBorder+i*3*gridHeight) });
                gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart  + 2 * _locationFromBorder, _yStart + 3 * gridHeight - _locationLength - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart  + 2 * _locationFromBorder, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight),
                        new Point(_xStart  + _locationLength + 2 * _locationFromBorder, _yStart + 3 * gridHeight - _locationFromBorder+i*3*gridHeight) });
            }
            for (int j = 0; j <= 1; j++)
            {
                gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + _chessBoardWidth - _locationLength - 2*_locationFromBorder, _yStart + 3 * gridHeight - _locationFromBorder+j*3*gridHeight),
                        new Point(_xStart + _chessBoardWidth - 2*_locationFromBorder , _yStart + 3 * gridHeight - _locationFromBorder+j*3*gridHeight),
                        new Point(_xStart + _chessBoardWidth - 2*_locationFromBorder, _yStart + 3 * gridHeight - _locationLength - _locationFromBorder+j*3*gridHeight) });
                gh.DrawLines(drawInnerRecPen, new Point[] {
                        new Point(_xStart + _chessBoardWidth - _locationLength - 2*_locationFromBorder , _yStart + 3 * gridHeight + 2 * _locationFromBorder+j*3*gridHeight),
                        new Point(_xStart + _chessBoardWidth - 2*_locationFromBorder, _yStart + 3 * gridHeight + 2 * _locationFromBorder+j*3*gridHeight),
                        new Point(_xStart + _chessBoardWidth - 2*_locationFromBorder, _yStart + 3 * gridHeight + _locationLength + 2 * _locationFromBorder+j*3*gridHeight) });
            }
           
            gh.Flush();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.Size = new System.Drawing.Size(462, 512);
            panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(205)))), ((int)(((byte)(170)))));
        }
    }
}

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值