Silverlight+WCF 新手实例 象棋 棋盘(二)

Silverlight+WCF 新手实例 象棋 棋盘(二),第1张

概述1.先新建一个和棋子相关的类库 打开VS2010后->新建->项目->Silverlight类库,名称就定为"ChessLib" 新建一个类名为Board.cs,棋盘类     棋盘类 using  System; using  System.Net; using  System.Windows; using  System.Windows.Controls; using  System.Wind

1.先新建一个和棋子相关的类库

打开VS2010后->新建->项目->Silverlight类库,名称就定为"Chesslib"

新建一个类名为Board.cs,棋盘类

 

 

棋盘类 using  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
    {
    }
}

 

乱七八糟:

由于继承自VS2005的想法,要画棋盘就是画线了,画线就要找GDI+了。于是在类里找什么Drawline之类的,

结果什么Draw都找不到,找到WPF有的,Silverlight里也是没有的。

 

最后,画线还是得用line控件,然后找个画布Add进去。

画线的控件后台生成的代码就是赋几个属性了:

代码 line line  =   new  line()
            {
                X1 
=  x1,
                Y1 
=  y1,
                X2 
=  x2,
                Y2 
=  y2,
                stroke 
=   new  SolIDcolorBrush(colors.Black),//线的颜色
                strokeThickness 
=   1//线的粗细
            };

 

 

 

按原始人冲动的想法,封闭个line函数出来,传入几个点,生成一条线,还得加上一个画布。

原始代码就变成了

 

public   class  Board
    {
        Panel container;
        
private   voID  Drawline( double  x1,  double  y1,  double  x2,  double  y2)
        {
            line line 
=   new  line()
            {
                X1 
=  x1,
                strokeThickness 
=   1
            };
            container.Children.Add(line);
        }
    }

 

 

有了画线函数,我们要想想棋盘里有多少条线了。

于是我们再看看象棋的图片:

@H_541_301@

数一数了,10条横线,9条直线。还有两个X线。如果棋子移走后,还能发现有一些点的修饰线(_| |_)像这种的。

于是冲动的就又要弄个函数来画线了

 

代码   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 );
            }
        }

 

由于棋盘中间有河界分隔,直线就不能直接从上到下了,于是分开两个for来循环了。左右两边两条是链接着的,我们另外补上了。

其实,看了棋盘,就知道棋盘的每条线都有间隔的,而且间隔后的宽和高都是一样的。这样我们就会想加多一个变量来表示了: public  int gap = 50;

我们的for里,传的都是点1.2.3.4.5,生成后,线都靠在一起的,所以还得加上个间隔隔开一下。

一开始呢Drawline(k*gap, 5*gap, k*gap, 9*gap);的传,于是重复的多了,发现直接把gap都拉到函数里面就行了。

于是目前的代码就变成了下面的了[这里加了一个DrawIn方法,用于外面调用,同时为棋盘增加宽和高属性]:

 

@H_994_502@

两边还没有线,我们接着补上两边的线,然后是画两个XX,还有加上一些修饰线,棋盘就差不多完成了。

我们在Draw函数里加上以下代码:

 

   // 补上两边两条直线
            Drawline( 0 , 0 , 9 );
            Drawline(
8 , 8 , 9 );
            
// 画交叉线
            Drawline( 3 , 5 , 2 );
            Drawline(
3 , 2 , 0 );
            Drawline(
3 , 7 , 9 );
            Drawline(
3 , 9 , 7 );

 

接下来麻烦一点的就是画修饰线了,像“炮”的位置四边都有8条修饰线。而兵的位置有两条

我仔细研究了下,两边的兵的修饰线+起来就是一个“炮”的修饰线,

于是多了三个函数:

private voID Drawlineleft2(double x,double y) //一个点,左边两个修饰线[4条]

private voID DrawlineRight2(double x,double y)//一个点,右边两个修饰线[4条]

private voID Drawline4(double x,double y)//一个点,上下左右四个修饰线,里面就是左+右了

函数实现如下:

 

代码   private   voID  Drawline4( double  x,  double  y)
        {
            Drawlineleft2(x, y);
            DrawlineRight2(x, y);
        }
        
private   voID  DrawlineRight2( double  x,  double  y)
        {
            x 
=  x  *  gap;
            y 
=  y  *  gap;
            
// 右两条直接
            Drawline(x  +  minGap, y  -  minGap  *   2 , x  +  minGap, y  -  minGap);
            Drawline(x 
+  minGap, y  +  minGap  *   2 , y  +  minGap);

            
// 右两条横线
            Drawline(x  +  minGap, y  -  minGap, x  +  minGap  *   2 , y  +  minGap, y  +  minGap);
        }
        
private   voID  Drawlineleft2( double  x,  double  y)
        {
            x 
=  x  *  gap;
            y 
=  y  *  gap;
            
// 左两条直接
            Drawline(x  -  minGap, x  -  minGap, y  -  minGap);
            Drawline(x 
-  minGap, y  +  minGap);
            
// 左两条横线
            Drawline(x  -  minGap  *   2 , y  -  minGap);
            Drawline(x 
-  minGap  *   2 , y  +  minGap);
        }

 

 

看到没有,代码里多了一个minGap,想想啦,那几个修饰线离那个点是有点距离的,所以多定义了这个小间隔。

private int minGap = 5;//修饰隔宽

于是我们还在Draw函数里加上这几个调用

 

// 画修饰线[炮]
            Drawline4( 1 , 2 );
            Drawline4(
1 , 7 );
            Drawline4(
7 , 2 );
            Drawline4(
7 , 7 );
            
// 画修饰线[兵]
            Drawline4( 2 , 3 );
            Drawline4(
4 , 3 );
            Drawline4(
6 , 3 );

            Drawline4(
2 , 6 );
            Drawline4(
4 , 6 );
            Drawline4(
6 , 6 );

            Drawlineleft2(
8 , 3 );
            Drawlineleft2(
8 , 6 );
            DrawlineRight2(
0 , 3 );
            DrawlineRight2(
0 , 6 );

 

 

OK,到此,棋盘代码就完成了。运行一下。看下效果:

 

 

我们发现,这棋盘太靠边了,于是加上两个margin让它宽出来一下,完整类代码如下:

 

棋盘 ///   <summary>
    
///  棋盘
    
///   </summary>
     public   class  Board
    {
        
///   <summary>
        
///  棋盘left偏移量
        
///   </summary>
         public    int  marginleft  =   50 ;
        
///   <summary>
        
///  棋盘top偏移量
        
///   </summary>
         public    int  margintop  = 50 ;
        
///   <summary>
        
///  棋盘隔宽
        
///   </summary>
         public    int  gap  =   50 ;
        
private   int  minGap  =   5 ; // 修饰隔宽
        Panel container;
        
public   double  WIDth
        {
            
get ;
            
set ;
        }
        
public   double  Height
        {
            
get ;
            
set ;
        }
        
public   voID  DrawIn(Panel control)
        {
            WIDth 
=  gap  *   9 + marginleft;
            Height 
=  gap  *   10 + margintop;
            container 
=  control;
            Draw();
        }
        
private   voID  Draw()
        {
            
// 画横线
             for  ( int  i  =   0 ; i  <   10 ; i ++ )
            {
                Drawline(
0 , 4 );
            }
            
for  ( int  k  =   1 ; k <   8 ; k ++ )
            {
                Drawline(k,
9 );
            }
            
// 补上两边两条直线
            Drawline( 0 , 7 );
            
// 画修饰线[炮]
            Drawline4( 1 , 6 );
        }
        
private   voID  Drawline( double  x1, double  y1, double  x2, double  y2)
        {
            line line 
=   new  line()
            {
                X1 
=  x1 * gap + marginleft,
                Y1 
=  y1  *  gap  +  margintop,
                X2 
=  x2  *  gap  +  marginleft,
                Y2 
=  y2  *  gap  +  margintop,
                strokeThickness 
=   1
            };
            container.Children.Add(line);
        }

        
private   voID  Drawline4( double  x, y  +  minGap);
        }
    }

 

刚刚发现,几个修饰点没出来,发现是因为把gap移到了函数内部,而在画修饰的时候又传了*gap的值。

于是,我们简单修改一下Draw函数就可以了。判断传进点的数字来决定是不是*gap就可以了。

 

private   voID  Drawline( double  x1,  double  y2)
        {
            
double  tempGap  =  (x1 + y1)  >   19   ?   1  : gap;
            line line 
=   new  line()
            {
                X1 
=  x1  *  tempGap  +  marginleft,
                Y1 
=  y1  *  tempGap  +  margintop,
                X2 
=  x2  *  tempGap  +  marginleft,
                Y2 
=  y2  *  tempGap  +  margintop,
                strokeThickness 
=   1
            };
            container.Children.Add(line);
        }

 

最后上传一张完整的图:

好了,本文到此结果,打完收工。

这里提示一下。那个窗口为什么用的Panel,因为我发现GrID还是Canvas都是继承自Panel,所以用了这个。

发现写的已经很新手了。

  public   class  Board
    { 
        
///   <summary>
        
///  棋盘隔宽
        
///   </summary>
        
///  
         public   int  gap  =   50 ;
        
public   double  WIDth
        {
            
get ;
            
set ;
        }
        
public   double  Height
        {
            
get ;
            
set ;
        }
        Panel container;
        
public   voID  DrawIn(Panel control)
        {
            WIDth 
=  gap  *   9 ;
            Height 
=  gap  *   10 ;
            container 
=  control;
            Draw();
        }
        
private   voID  Drawline( double  x1,  double  y2)
        {
            line line 
=   new  line()
            {
                X1 
=  x1  *  gap,
                Y1 
=  y1  *  gap,
                X2 
=  x2  *  gap,
                Y2 
=  y2  *  gap,
                strokeThickness 
=   1
            };
            container.Children.Add(line);
        }
        
private   voID  Draw()
        {
            
// 画横线
             for  ( int  i  =   0 ; i  <   10 ; i ++ )
            {
                Drawline(
0 9 );
            }
        }

 


好了,新建一个Silverlight应用程序,添加引用这个类库,实例Board board=new Board(); board.DrawIn(Canvas画布的ID);

可以看到界面线已有几条线了来了。

总结

以上是内存溢出为你收集整理的Silverlight+WCF 新手实例 象棋 棋盘(二)全部内容,希望文章能够帮你解决Silverlight+WCF 新手实例 象棋 棋盘(二)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1025435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存