本文是利用C# 实现中国象棋的棋盘绘制,以及初始化布局,并不实现中国象棋的对弈逻辑。仅供学习参考使用。
思路:
- 绘制中国象棋棋盘,竖线九条,横线十条。再中间绘制‘楚河’,‘汉界’ 。
- 绘制棋子,然后将棋子布局在棋盘上即可。
涉及知识点:
- 用户控件:用于实现棋盘的绘制,重写 OnPaint(PaintEventArgs e) 方法。
- Matrix:封装表示几何变换的 3x3 仿射矩阵。本例中主要用于旋转绘制反方的‘汉界’。
- GraphicsPath:表示一系列相互连接的直线和曲线。本例中主要用于绘制圆形棋子。
效果图如下:
(一)
(二)
核心代码
棋盘核心代码如下:
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 * ;
int center = height / ;//垂直中心位置
int s_width = (width - * padding) / ;//每一条横线的间距
int s_heigth = (height - * padding) / ;//每一条竖线的间距
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[] { "", "", "", "", "", "", "", "", "" });
dicNums.Add("down", new string[] { "九", "八", "七", "六", "五", "四", "三", "二", "一" });
Font font = new Font("宋体", , FontStyle.Regular);
for (int i = ; i < ; 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 * ));
Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * ));
Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * ));
g.DrawLine(pen, p0, p1);
g.DrawLine(pen, p2, p3);
//上下的文字
Point p_up = new Point(start_x + i * s_width - , padding / );
Point p_down = new Point(start_x + i * s_width - , start_y + (s_heigth * ) + padding / );
g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);
g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);
//数组赋值
for (int j = ; j < ; j++)
{
Point absLocation = ArrPiece[i, j].AbsoluteLocation;
absLocation.X = start_x + i * s_width;
ArrPiece[i, j].AbsoluteLocation = absLocation;
}
}
for (int i = ; i < ; i++)
{
//横线十条
Point p0 = new Point(start_x, start_y + i * s_heigth);
Point p1 = new Point(start_x + s_width * , start_y + i * s_heigth);
g.DrawLine(pen, p0, p1);
//数组赋值
for (int j = ; j < ; j++)
{
Point absLocation = ArrPiece[j, i].AbsoluteLocation;
absLocation.Y = start_y + i * s_heigth;
ArrPiece[j, i].AbsoluteLocation = absLocation;
}
}
//绘制九宫格
for (int i = ; i < ; 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 * ));
Point p2 = new Point(start_x + ( + i * ) * s_width, start_y + (s_heigth * ));
Point p3 = new Point(start_x + ( - i * ) * s_width, start_y + (s_heigth * ));
g.DrawLine(pen, p0, p1);
g.DrawLine(pen, p2, p3);
} //兵和卒处有拐角,从左往右
for (int i = ; i < ; i++)
{
int p_x = start_x + * i * s_width;
int p_y = start_y + * s_heigth;
DrawCorner(g, pen, p_x, p_y);//兵
p_y = start_y + * s_heigth;
DrawCorner(g, pen, p_x, p_y);//卒
}
//炮处的拐角,从左往右
for (int i = ; i < ; i++)
{
int p_x = start_x + ( + * i) * s_width;
int p_y = start_y + * s_heigth;
DrawCorner(g, pen, p_x, p_y);//炮
p_y = start_y + * s_heigth;
DrawCorner(g, pen, p_x, p_y);//炮
}
//绘制楚河汉界
Point p_0 = new Point( * s_width, center - );
Point p_1 = new Point( * s_width, center + );
font = new Font("方正隶二繁体", , FontStyle.Regular);
g.DrawString("楚河", font, Brushes.Black, p_0);
Matrix mtxSave = g.Transform;
Matrix mtxRotate = g.Transform;
mtxRotate.RotateAt(, p_1);
g.Transform = mtxRotate;
g.DrawString("汉界", font, Brushes.Black, p_1);
g.Transform = mtxSave;
//绘制外边框
g.DrawRectangle(pen, , , width - , height - );
g.DrawRectangle(pen, , , width - , height - );
g.DrawRectangle(pen, , , width - , height - );
}
棋子核心代码如下:
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(, , this.Width - , this.Height - );
g.FillEllipse(new SolidBrush(this.BackColor), rectangle);
g.DrawEllipse(new Pen(Color.Black,), inRect); Font font = new Font("楷体", , FontStyle.Regular);
g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), ,);
}
源码下载链接