本文是利用C# 实现中国象棋的棋盘绘制,以及初始化布局,并不实现中国象棋的对弈逻辑。仅供学习参考使用。
思路:
绘制中国象棋棋盘,竖线九条,横线十条。再中间绘制‘楚河’,‘汉界’ 。绘制棋子,然后将棋子布局在棋盘上即可。涉及知识点:
用户控件:用于实现棋盘的绘制,重写 OnPaint(PaintEventArgs e) 方法。Matrix:封装表示几何变换的 3x3 仿射矩阵。本例中主要用于旋转绘制反方的‘汉界’。GraphicsPath:表示一系列相互连接的直线和曲线。本例中主要用于绘制圆形棋子。效果图如下:
(一)
(二)
核心代码
棋盘核心代码如下:
1 protected overrIDe voID OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 5 //初始化数组 6 InitArrPIEceInfo(); 7 8 Graphics g = e.Graphics; 9 int wIDth = this.WIDth; 10 int height = .Height; 11 int padding = this.padding.All * 20; 12 int center = height / 2;垂直中心位置 13 int s_wIDth = (wIDth - 2 * padding) / 8;每一条横线的间距 14 int s_heigth = (height - 9;每一条竖线的间距 15 int start_x = padding;起始位置 16 int start_y = padding; 17 Pen pen = new Pen(Brushes.Black,1.5f); 18 Dictionary<string,string[]> dicNums = new Dictionary<string[]>(); 19 dicNums.Add("up",1)">new string[9] { 123456789" }); 20 dicNums.Add(down九八七六五四三二一 21 Font Font = new Font(宋体12,FontStyle.Regular); 22 for (int i = 0; i < 9; i++) 23 { 24 竖线九条 25 Point p0 = new Point(start_x + i * s_wIDth,start_y); 26 Point p1 = new Point(start_x + i * s_wIDth,start_y + (s_heigth * 4)); 27 Point p2 = 5 28 Point p3 = 9 29 g.Drawline(pen,p0,p1); 30 31 上下的文字 32 Point p_up = new Point(start_x + i * s_wIDth - 5,padding / 2 33 Point p_down = 9) + padding / 3 34 g.DrawString(dicNums[][i],Font,Brushes.Black,p_up); 35 g.DrawString(dicNums[ 36 数组赋值 37 int j = 0; j < 10; j++ 38 { 39 Point absLocation = ArrPIEce[i,j].absoluteLocation; 40 absLocation.X = start_x + i * s_wIDth; 41 ArrPIEce[i,j].absoluteLocation = absLocation; 42 } 43 } 44 10; i++ 45 46 横线十条 47 Point p0 = new Point(start_x,start_y + i * s_heigth); 48 Point p1 = new Point(start_x + s_wIDth * 8,1)"> 49 50 51 9; j++ 52 53 Point absLocation = ArrPIEce[j,i].absoluteLocation; 54 absLocation.Y = start_y + i * s_heigth; 55 ArrPIEce[j,i].absoluteLocation = 56 57 58 绘制九宫格 59 2; i++ 60 61 Point p0 = new Point(start_x + (3 + i * 2) * 62 Point p1 = 5 - i * 2) * s_wIDth,1)"> 63 Point p2 = 7 64 Point p3 = 65 66 67 68 69 兵和卒处有拐角,从左往右 70 5; i++ 71 72 int p_x = start_x + 2 * i * 73 int p_y = start_y + 3 * 74 DrawCorner(g,pen,p_x,p_y);兵 75 p_y = start_y + 6 * 76 DrawCorner(g,1)">卒 77 78 炮处的拐角,从左往右 79 80 81 int p_x = start_x + (1 + 6 * i) * 82 2 * 83 DrawCorner(g,1)">炮 84 p_y = start_y + 7 * 85 DrawCorner(g,1)"> 86 87 绘制楚河汉界 88 Point p_0 = new Point(2 * s_wIDth,center - 25 89 Point p_1 = 7 * s_wIDth,center + 90 Font = 方正隶二繁体30 91 g.DrawString(楚河 92 Matrix mtxSave = g.transform; 93 Matrix mtxRotate = 94 mtxRotate.RotateAt(180 95 g.transform = mtxRotate; 96 g.DrawString(汉界 97 g.transform = mtxSave; 98 绘制外边框 99 g.DrawRectangle(pen,1)">3,wIDth - 6,height - 6100 g.DrawRectangle(pen,1)">10,1)">10101 g.DrawRectangle(pen,1)">7,1)">14,1)">14102 }VIEw Code
棋子核心代码如下:
1 2 3 4 Graphics g = 5 GraphicsPath gPath = new GraphicsPath(); 6 Set a new rectangle to the same size as the button's ClIEntRectangle property. 7 Rectangle rectangle = .ClIEntRectangle; 8 g.DrawEllipse(new Pen(.FlatAppearance.bordercolor),rectangle); 9 gPath.AddEllipse(rectangle);10 11 Set the button's Region property to the newly created circle region.12 this.Region = Region(gPath);13 Rectangle inRect = new Rectangle(2,1)">this.WIDth - 4,1)">this.Height - 14 g.FillEllipse(new SolIDBrush(.Backcolor),1)">15 g.DrawEllipse(new Pen(color.Black,1)">),inRect);16 17 Font Font = 楷体18 g.DrawString(this.Text,1)">this.Forecolor),1)">0,1)">19 }VIEw Code
源码下载链接
总结
以上是内存溢出为你收集整理的C# 实现中国象棋【棋盘,棋子】全部内容,希望文章能够帮你解决C# 实现中国象棋【棋盘,棋子】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)