编写网络象棋案例————Silverlight(棋盘篇)
这节我们来说怎么只做象棋的棋盘,其实很简单用到 LINE类,首先对象棋有个初步映像。如下图
我们也要知道计算机的图形是如何的:

通过线把我们想要的棋盘构造好,开始写代码:
sing System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace chesslib
{
/// <summary>
/// 棋盘
/// </summary>
public class Board
{
Panel qipan;
private void Drawline(double x1,double y1,double x2, double y2)
{
double tempgab = (x1 + y1) > 18 ? 1 : gab;//修饰的原因:因为把gap移到了函数内部,而在画修饰的时候又传了*gap的值
Line line = new Line()
{
X1 = x1 * tempgab + maginleft,
Y1 = y1 * tempgab + magintop,
X2 = x2 * tempgab + maginleft,
Y2 = y2 * tempgab + magintop,
Stroke=new SolidColorBrush(Colors.Black),//线的颜色
StrokeThickness=1,
};
qipan.Children.Add(line);
}
/// <summary>
/// 间距
/// </summary>
public int gab = 50;
public int maginleft = 50;//离页面左右的距离
public int magintop = 50;//
public void DrawIn(Panel qipan1)
{
width = 9 * gab+maginleft;
height = 10 * gab+magintop;
qipan = qipan1;
Draw();
}
public int width
{
set;
get;
}
public int height
{
set;
get;
}
private int magin = 5;//炮、兵所在位置的小线
private void Drawlineleft(double x, double y)
{
x = x * gab;
y = y * gab;
Drawline(x - magin, y - magin * 2, x - magin, y - magin);//竖线
Drawline(x - magin, y + magin * 2, x - magin, y + magin);
Drawline(x-magin*2,y-magin,x-magin,y-magin);//横线
Drawline(x-magin*2,y+magin,x-magin,y+magin);
}
private void Drawlineright(double x, double y)
{
x = x * gab;
y = y * gab;
Drawline(x+magin,y-magin*2,x+magin,y-magin);//竖线
Drawline(x+magin,y+magin*2,x+magin,y+magin);//
Drawline(x+magin*2,y-magin,x+magin,y-magin);
Drawline(x+magin*2,y+magin,x+magin,y+magin);
}
private void DrawlineLow(double x, double y)
{
Drawlineleft(x, y);
Drawlineright(x, y);
}
private void Draw()
{
//横线
for (int i = 0; i < 10; i++)
{
Drawline(0, i, 8, i);
}
//7个竖直线
for (int j = 1; j < 8; j++)
{
Drawline(j,0,j,4);
}
for (int k = 1; k < 8; k++)
{
Drawline(k, 5, k, 9);
}
Drawline(0, 0, 0, 9);
Drawline(8,0,8,9);
Drawline(3,0,5,2);
Drawline(5,0,3,2);
Drawline(3,7,5,9);
Drawline(5,7,3,9);
//炮
DrawlineLow(1,2);
DrawlineLow(7,2);
DrawlineLow(1,7);
DrawlineLow(7,7);
//兵
Drawlineright(0,3);
Drawlineleft(8, 3);
Drawlineright(0,6);
Drawlineleft(8,6);
DrawlineLow(2,3);
DrawlineLow(4,3);
DrawlineLow(6,3);
DrawlineLow(2,6);
DrawlineLow(4,6);
DrawlineLow(6,6);
}
}
}
这里我们通过画线来够着棋盘的,如果有什么疑问跟帖或者与我联系QQ(361121687)
浙公网安备 33010602011771号