亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

c#繪制中國象棋棋盤與棋子的方法

發布時間:2020-07-10 10:04:38 來源:億速云 閱讀:373 作者:清晨 欄目:開發技術

這篇文章將為大家詳細講解有關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<string, string[]> dicNums = new Dictionary<string, string[]>();
   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);
  }

棋子核心代碼如下:

protected override void OnPaint(PaintEventArgs e)
  {
   base.OnPaint(e);
   Graphics g = e.Graphics;
   GraphicsPath gPath = new GraphicsPath();
   // Set a new rectangle to the same size as the button's ClientRectangle property.
   Rectangle rectangle = this.ClientRectangle;
   g.DrawEllipse(new Pen(this.FlatAppearance.BorderColor), rectangle);
   gPath.AddEllipse(rectangle);

   // Set the button's Region property to the newly created circle region.
   this.Region = new Region(gPath);
   Rectangle inRect = new Rectangle(2, 2, this.Width - 4, this.Height - 3);
   g.FillEllipse(new SolidBrush(this.BackColor), rectangle);
   g.DrawEllipse(new Pen(Color.Black,2), inRect);

   Font font = new Font("楷體", 25, FontStyle.Regular);
   g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 0,5);
  }

關于c#繪制中國象棋棋盤與棋子的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

安宁市| 壤塘县| 龙井市| 宽甸| 周至县| 阿图什市| 洞口县| 正阳县| 临邑县| 林甸县| 景谷| 临洮县| 龙门县| 迁西县| 德令哈市| 平原县| 拉孜县| 宁海县| 贺兰县| 石景山区| 萨嘎县| 延安市| 合阳县| 沙雅县| 盈江县| 曲松县| 普格县| 宜宾市| 全州县| 大丰市| 牙克石市| 丰县| 灌云县| 子洲县| 金门县| 甘南县| 沙田区| 肥城市| 大同市| 霍城县| 太谷县|