象棋游戏编程——画棋盘
BingWay原创作品,转载请注明作者和出处。
主功能界面已划分完成,昨天写了一段代码画棋盘(也是象棋里面所说的九宫格)。画棋盘虽然是个不起眼的小功能,但是棋盘上论输赢,画棋盘也是非常重要的。
在panel1中画棋盘
public void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g=e.Graphics;
int startLeft,startTop;
if(this.displaySize==15)
{
startLeft=48;startTop=32;//标准屏幕
}
else
{
startLeft=60;startTop=35;//宽屏
}
int Index=0; //记录点的索引
width=panel1.Width;
height=panel1.Height;
width=width/9; height=height/10; //确定每个方格的宽和高
width-=4; height-=3;
Pen pen1=new Pen(Color.DarkBlue,3);
Pen pen2=new Pen(Color.Black,2);
int startX=startLeft,startY=startTop;
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<8;k++)
{//以下括号内所有语句为画一行方格
g.DrawRectangle(pen1,startX,startY,width,height);
point[Index]=new Point(startX,startY);
Index++;
startX+=width;
}
point[Index]=new Point(point[Index-1].X+width,point[Index-1].Y);
Index++;
startY+=height; //往下移动动坐标,画下一行方格
startX=startLeft;
}
//记录上面往下的九个点
for(int ii=Index-9,j=0;j<9;ii++,j++)
{
point[Index]=new Point(point[ii].X,point[ii].Y+height);
Index++;
}
int paoX,paoY; //声明画炮点的坐标
int x,y,temp; //画将/帅的辅助变量
if(i==0)
{
paoX=startLeft+width;
paoY=startTop+2*height;
x=startLeft+3*width;
y=startTop+2*height;
temp=0;
}
else
{
paoX=startLeft+width;
paoY=startTop+7*height+height/2;
x=startLeft+3*width;
y=startLeft+9*height+2;
temp=7*height+height/2+2;
}
//画横向的两个炮的图案
DrawAllSitePoint(sender,e,paoX,paoY,width);
paoX=startLeft+7*width;
DrawAllSitePoint(sender,e,paoX,paoY,width);
//画将/帅的皇宫
float[] dashValues={3,1,3,1,3,1};
pen2.DashPattern=dashValues;
g.DrawLine(pen2,x,startTop+temp,x+2*width,y);
x=startLeft+5*width;
g.DrawLine(pen2,x,startTop+temp,x-2*width,y);
//画中间的楚河汉界
Font font=new Font("华文行楷", 42F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
Brush brush=new SolidBrush(Color.Purple);
PointF tempPoint=new PointF(startLeft,startTop+4*height+10);
string tempStr;
if(this.displaySize==15)
{
tempStr=" 楚 河 汉 界 "; /************适于标准屏幕**************/
}
else
{
tempStr=" 楚 河 汉 界 "; /************适于宽屏**************/
tempPoint.Y=tempPoint.Y+8;
}
g.DrawString(tempStr,font,brush,tempPoint);
startX=startLeft;
startY=startTop+5*height+height/2; //移动坐标,画下方旗盘
}
for(int i=29;i<=33;i+=2)
DrawAllSitePoint(sender,e,this.point[i].X,this.point[i].Y,width);
for(int i=56;i<=60;i+=2)
DrawAllSitePoint(sender,e,this.point[i].X,this.point[i].Y,width);
this.DrawLeftHalfSitePoint(sender,e,this.point[27].X,this.point[27].Y,width);
this.DrawRightHalfSitePoint(sender,e,this.point[35].X,this.point[35].Y,width);
this.DrawLeftHalfSitePoint(sender,e,this.point[54].X,this.point[54].Y,width);
this.DrawRightHalfSitePoint(sender,e,this.point[62].X,this.point[62].Y,width);
pen1.Dispose();
g.Dispose();
MyInitializeComponent();
}
public void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g=e.Graphics;
int startLeft,startTop;
if(this.displaySize==15)
{
startLeft=48;startTop=32;//标准屏幕
}
else
{
startLeft=60;startTop=35;//宽屏
}
int Index=0; //记录点的索引
width=panel1.Width;
height=panel1.Height;
width=width/9; height=height/10; //确定每个方格的宽和高
width-=4; height-=3;
Pen pen1=new Pen(Color.DarkBlue,3);
Pen pen2=new Pen(Color.Black,2);
int startX=startLeft,startY=startTop;
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
for(int k=0;k<8;k++)
{//以下括号内所有语句为画一行方格
g.DrawRectangle(pen1,startX,startY,width,height);
point[Index]=new Point(startX,startY);
Index++;
startX+=width;
}
point[Index]=new Point(point[Index-1].X+width,point[Index-1].Y);
Index++;
startY+=height; //往下移动动坐标,画下一行方格
startX=startLeft;
}
//记录上面往下的九个点
for(int ii=Index-9,j=0;j<9;ii++,j++)
{
point[Index]=new Point(point[ii].X,point[ii].Y+height);
Index++;
}
int paoX,paoY; //声明画炮点的坐标
int x,y,temp; //画将/帅的辅助变量
if(i==0)
{
paoX=startLeft+width;
paoY=startTop+2*height;
x=startLeft+3*width;
y=startTop+2*height;
temp=0;
}
else
{
paoX=startLeft+width;
paoY=startTop+7*height+height/2;
x=startLeft+3*width;
y=startLeft+9*height+2;
temp=7*height+height/2+2;
}
//画横向的两个炮的图案
DrawAllSitePoint(sender,e,paoX,paoY,width);
paoX=startLeft+7*width;
DrawAllSitePoint(sender,e,paoX,paoY,width);
//画将/帅的皇宫
float[] dashValues={3,1,3,1,3,1};
pen2.DashPattern=dashValues;
g.DrawLine(pen2,x,startTop+temp,x+2*width,y);
x=startLeft+5*width;
g.DrawLine(pen2,x,startTop+temp,x-2*width,y);
//画中间的楚河汉界
Font font=new Font("华文行楷", 42F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic))), System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
Brush brush=new SolidBrush(Color.Purple);
PointF tempPoint=new PointF(startLeft,startTop+4*height+10);
string tempStr;
if(this.displaySize==15)
{
tempStr=" 楚 河 汉 界 "; /************适于标准屏幕**************/
}
else
{
tempStr=" 楚 河 汉 界 "; /************适于宽屏**************/
tempPoint.Y=tempPoint.Y+8;
}
g.DrawString(tempStr,font,brush,tempPoint);
startX=startLeft;
startY=startTop+5*height+height/2; //移动坐标,画下方旗盘
}
for(int i=29;i<=33;i+=2)
DrawAllSitePoint(sender,e,this.point[i].X,this.point[i].Y,width);
for(int i=56;i<=60;i+=2)
DrawAllSitePoint(sender,e,this.point[i].X,this.point[i].Y,width);
this.DrawLeftHalfSitePoint(sender,e,this.point[27].X,this.point[27].Y,width);
this.DrawRightHalfSitePoint(sender,e,this.point[35].X,this.point[35].Y,width);
this.DrawLeftHalfSitePoint(sender,e,this.point[54].X,this.point[54].Y,width);
this.DrawRightHalfSitePoint(sender,e,this.point[62].X,this.point[62].Y,width);
pen1.Dispose();
g.Dispose();
MyInitializeComponent();
}
下面是棋盘运行效果: