资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c#绘制中国象棋棋盘与棋子的方法-创新互联

创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

站在用户的角度思考问题,与客户深入沟通,找到漳州网站设计与漳州网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站建设、网站制作、企业官网、英文网站、手机端网站、网站推广、域名注册虚拟主机、企业邮箱。业务覆盖漳州地区。

这篇文章将为大家详细讲解有关c#绘制中国象棋棋盘与棋子的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

思路:

  1. 绘制中国象棋棋盘,竖线九条,横线十条。再中间绘制‘楚河',‘汉界' 。
  2. 绘制棋子,然后将棋子布局在棋盘上即可。
     

涉及知识点:

  1. 用户控件:用于实现棋盘的绘制,重写 OnPaint(PaintEventArgs e) 方法。
  2. Matrix:封装表示几何变换的 3x3 仿射矩阵。本例中主要用于旋转绘制反方的‘汉界'。
  3. GraphicsPath:表示一系列相互连接的直线和曲线。本例中主要用于绘制圆形棋子。
     

效果图如下:

(一)

c#绘制中国象棋棋盘与棋子的方法

(二)

c#绘制中国象棋棋盘与棋子的方法

核心代码

棋盘核心代码如下:

protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint(e);

   //初始化数组
   InitArrPieceInfo();

   Graphics g = e.Graphics;
   int width = this.Width;
   int height = this.Height;
   int padding = this.Padding.All * 20;
   int center = height / 2;//垂直中心位置
   int s_width = (width - 2 * padding) / 8;//每一条横线的间距
   int s_heigth = (height - 2 * padding) / 9;//每一条竖线的间距
   int start_x = padding;//起始位置
   int start_y = padding;//起始位置
   Pen pen = new Pen(Brushes.Black, 1.5f);
   Dictionary dicNums = new Dictionary();
   dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
   dicNums.Add("down", new string[9] { "九", "八", "七", "六", "五", "四", "三", "二", "一" });
   Font font = new Font("宋体", 12, FontStyle.Regular);
   for (int i = 0; i < 9; i++)
   {
    //竖线九条
    Point p0 = new Point(start_x + i * s_width, start_y);
    Point p1 = new Point(start_x + i * s_width, start_y + (s_heigth * 4));
    Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * 5));
    Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * 9));
    g.DrawLine(pen, p0, p1);
    g.DrawLine(pen, p2, p3);
    //上下的文字
    Point p_up = new Point(start_x + i * s_width - 5, padding / 2);
    Point p_down = new Point(start_x + i * s_width - 5, start_y + (s_heigth * 9) + padding / 3);
    g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);
    g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);
    //数组赋值
    for (int j = 0; j < 10; j++)
    {
     Point absLocation = ArrPiece[i, j].AbsoluteLocation;
     absLocation.X = start_x + i * s_width;
     ArrPiece[i, j].AbsoluteLocation = absLocation;
    }
   }
   for (int i = 0; i < 10; i++)
   {
    //横线十条
    Point p0 = new Point(start_x, start_y + i * s_heigth);
    Point p1 = new Point(start_x + s_width * 8, start_y + i * s_heigth);
    g.DrawLine(pen, p0, p1);
    //数组赋值
    for (int j = 0; j < 9; j++)
    {
     Point absLocation = ArrPiece[j, i].AbsoluteLocation;
     absLocation.Y = start_y + i * s_heigth;
     ArrPiece[j, i].AbsoluteLocation = absLocation;
    }
   }
   //绘制九宫格
   for (int i = 0; i < 2; i++)
   {
    Point p0 = new Point(start_x + (3 + i * 2) * s_width, start_y);
    Point p1 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 2));
    Point p2 = new Point(start_x + (3 + i * 2) * s_width, start_y + (s_heigth * 7));
    Point p3 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 9));
    g.DrawLine(pen, p0, p1);
    g.DrawLine(pen, p2, p3);
   }

   //兵和卒处有拐角,从左往右
   for (int i = 0; i < 5; i++)
   {
    int p_x = start_x + 2 * i * s_width;
    int p_y = start_y + 3 * s_heigth;
    DrawCorner(g, pen, p_x, p_y);//兵
    p_y = start_y + 6 * s_heigth;
    DrawCorner(g, pen, p_x, p_y);//卒
   }
   //炮处的拐角,从左往右
   for (int i = 0; i < 2; i++)
   {
    int p_x = start_x + (1 + 6 * i) * s_width;
    int p_y = start_y + 2 * s_heigth;
    DrawCorner(g, pen, p_x, p_y);//炮
    p_y = start_y + 7 * s_heigth;
    DrawCorner(g, pen, p_x, p_y);//炮
   }
   //绘制楚河汉界
   Point p_0 = new Point(2 * s_width, center - 25);
   Point p_1 = new Point(7 * s_width, center + 20);
   font = new Font("方正隶二繁体", 30, FontStyle.Regular);
   g.DrawString("楚河", font, Brushes.Black, p_0);
   Matrix mtxSave = g.Transform;
   Matrix mtxRotate = g.Transform;
   mtxRotate.RotateAt(180, p_1);
   g.Transform = mtxRotate;
   g.DrawString("汉界", font, Brushes.Black, p_1);
   g.Transform = mtxSave;
   //绘制外边框
   g.DrawRectangle(pen, 3, 3, width - 6, height - 6);
   g.DrawRectangle(pen, 5, 5, width - 10, height - 10);
   g.DrawRectangle(pen, 7, 7, width - 14, height - 14);
  }

名称栏目:c#绘制中国象棋棋盘与棋子的方法-创新互联
本文来源:http://cdkjz.cn/article/ghigo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220