最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c# 繪制中國象棋棋盤與棋子

 更新時間:2020年07月09日 15:19:16   作者:Alan.hsiang  
這篇文章主要介紹了c# 繪制中國象棋棋盤與棋子,文中實例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下

本文是利用C# 實現(xiàn)中國象棋的棋盤繪制,以及初始化布局,并不實現(xiàn)中國象棋的對弈邏輯。僅供學習參考使用。

思路:

  1. 繪制中國象棋棋盤,豎線九條,橫線十條。再中間繪制‘楚河',‘漢界' 。
  2. 繪制棋子,然后將棋子布局在棋盤上即可。

涉及知識點:

  1. 用戶控件:用于實現(xiàn)棋盤的繪制,重寫 OnPaint(PaintEventArgs e) 方法。
  2. Matrix:封裝表示幾何變換的 3x3 仿射矩陣。本例中主要用于旋轉(zhuǎn)繪制反方的‘漢界'。
  3. GraphicsPath:表示一系列相互連接的直線和曲線。本例中主要用于繪制圓形棋子。

效果圖如下:

(一)

(二)

核心代碼

棋盤核心代碼如下:

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

   //初始化數(shù)組
   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);
    //數(shù)組賦值
    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);
    //數(shù)組賦值
    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# 繪制中國象棋棋盤與棋子的詳細內(nèi)容,更多關(guān)于c# 繪制棋盤與棋子的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:

相關(guān)文章

  • 在WPF中使用多線程更新UI

    在WPF中使用多線程更新UI

    這篇文章介紹了在WPF中使用多線程更新UI的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C#登入實例

    C#登入實例

    本篇文章通過截圖的方式向大家展示C#程序登陸實現(xiàn)的全過程,利用了C#三層架構(gòu)的編寫方法,希望對大家今后編寫代碼有所幫助
    2016-11-11
  • 如何讓C#、VB.NET實現(xiàn)復雜的二進制操作

    如何讓C#、VB.NET實現(xiàn)復雜的二進制操作

    VB.NET和C#屬于高級語言,對二進制位操作的支持不是很好,比如沒有了移位運算等,用的時候確實很不方便,所以在閑暇之余我重新封裝了一個用于C#、VB.NET的位操作類庫,通過該類庫可以實現(xiàn)數(shù)據(jù)移位、循環(huán)移位、轉(zhuǎn)換為二進制、將二進制轉(zhuǎn)換為數(shù)據(jù)等
    2013-07-07
  • C#代碼實現(xiàn)短信驗證碼接口示例

    C#代碼實現(xiàn)短信驗證碼接口示例

    這篇文章主要為大家詳細介紹了C#實現(xiàn)短信驗證碼接口示例代碼,感興趣的小伙伴們可以參考一下
    2016-08-08
  • asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼

    asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼

    tensorflowjs,在該項目中使用了ml5js這個封裝過的機器學習JavaScript類庫, 使用起來更簡單,本文給大家分享asp.net core 使用 tensorflowjs實現(xiàn) face recognition的源代碼,需要的朋友參考下吧
    2021-06-06
  • C#編程高并發(fā)的幾種處理方法詳解

    C#編程高并發(fā)的幾種處理方法詳解

    這篇文章主要為大家詳細介紹了C#編程高并發(fā)的幾種處理方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C#訪問及調(diào)用類中私有成員與方法示例代碼

    C#訪問及調(diào)用類中私有成員與方法示例代碼

    訪問一個類的私有成員不是什么好做法,大家也都知道私有成員在外部是不能被訪問的,這篇文章主要給大家介紹了關(guān)于C#訪問及調(diào)用類中私有成員與方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2018-06-06
  • WPF實現(xiàn)輪播圖效果(圖片、視屏)

    WPF實現(xiàn)輪播圖效果(圖片、視屏)

    這篇文章主要介紹了WPF實現(xiàn)輪播圖效果,以下是一個使用WPF技術(shù)實現(xiàn)圖片和視屏輪播的簡單案例代碼示例,文中有詳細的代碼示例,具有一定的參考價值,感興趣的小伙伴可以自己動手試試
    2023-10-10
  • C#實現(xiàn)單件模式的三種常用方法

    C#實現(xiàn)單件模式的三種常用方法

    這篇文章主要介紹了C#實現(xiàn)單件模式的三種常用方法,分析了單件模式的原理、功能與常用的三種實現(xiàn)方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • C#畫筆使用復合數(shù)組繪制單個矩形的方法

    C#畫筆使用復合數(shù)組繪制單個矩形的方法

    這篇文章主要介紹了C#畫筆使用復合數(shù)組繪制單個矩形的方法,涉及C#使用畫筆繪制圖形的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評論

东城区| 扎兰屯市| 利津县| 武定县| 桃源县| 襄汾县| 克什克腾旗| 浦城县| 拉孜县| 柘荣县| 中方县| 尼玛县| 怀来县| 河北区| 定日县| 深圳市| 贵州省| 竹山县| 邛崃市| 芜湖县| 诸城市| 高邑县| 忻城县| 商洛市| 台中县| 澎湖县| 宁夏| 三亚市| 金坛市| 台中市| 色达县| 南木林县| 蒲城县| 呼伦贝尔市| 兴宁市| 泸水县| 茂名市| 班玛县| 独山县| 东丽区| 苍南县|