silverlight   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

上节创建显示了一颗棋子,由于没有margin属性,所以只能靠边站。 现在,我们创建一个象棋类,让它包括棋盘和棋子,同时附加几个常用函数。 还是对着Silverlight类库项目右键添加类:Chess.cs ///   <sumMary>      ///  象棋 by 路过秋天      ///   </sumMary>      public   class  Chess     {     }

上节创建显示了一颗棋子,由于没有margin属性,所以只能靠边站。

现在,我们创一个象棋类,让它包括棋盘和棋子,同时附加几个常用函数

还是对着Silverlight类库项目右键添加类:Chess.cs

///   <sumMary>
    
///  象棋 by 路过秋天
    
///   </sumMary>
     public   class  Chess
    {

    }

 

既然要包括象棋当然是要包括棋盘和棋子了,于是我们为之加两个属性,棋子有很多颗,所以用List列表。

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

  ///   <sumMary>
    
///  象棋 by 路过秋天
    
///   </sumMary>
     public   class  Chess
    {
        
///   <sumMary>
        
///  棋子列表
        
///   </sumMary>
         public  List < Chessman >  ChessmanList
        {
            
get ;
            
set ;
        }
        
///   <sumMary>
        
///  棋盘
        
///   </sumMary>
         public  Board Board
        {
            
get ;
            
set ;
        }
    }

 

我们再将初始化棋盘的行为放到象棋里来,于是增加一个初始棋盘的方法

里面有个container呢,就是Panel容器了

Panel container;//归属容器
///   <sumMary>
        
///  初始化棋盘
        
///   </sumMary>
         public   void  InitBoard()
        {
            Board 
=   new  Board();
            Board.DrawIn(container);
        }

 

至于棋子嘛,太多了,不好弄啊。。。36颗棋子,颜色又不一样,想for一下,没规律啊,咋整。。。

难道直接复制36条了。。。-_-...

经过研究发现,棋子的数量有1,2,5三种,颜色只有黑和红,于是折腾出了这么个创建的函数

一个参数传数量了。分支里,如果是2个的,位置对称,所以[8-x]就OK,5个的,隔两个位置一颗,于是就这么出来了。

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

private   void  CreateChessman( int  count, double  x, double  y,Color color, String  Name)
        {
            
if  (count  ==   1 )
            {
                Chessman chessmane 
=   new  Chessman(x, y, radius, color, NAME, this );
                ChessmanList.Add(chessmanE);
            }
            
if  (count  ==   2 )
            {
                Chessman chessmane1 
=   new  Chessman(x,  this );
                Chessman chessmane2 
=   new  Chessman( 8   -  x,  this );
                ChessmanList.Add(chessmane1);
                ChessmanList.Add(chessmane2);
            }
            
else   if  (count  ==   5 )
            {
                
for  ( int  i  =   0 ; i  <   5 ; i ++ )
                {
                    Chessman chessmane 
=   new  Chessman(i  *   2 this );
                    ChessmanList.Add(chessmanE);
                }
            }
        }

 

 

注意:

1.参数里还有棋子的半径呢,所以呢,记得在外面再定义一个:

privatE int radius=15;//棋子半径

2.在Chessman的构造函数里,传进了this自身对象,这是为了在棋子函数里可以调用Chess的方法

所以我们需要@L_772_29@下上节的棋子的构造函数,新增加一个传参:

 

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

代码
// 这是棋子类Chessman改动的代码。 
public  Chess Parent
        {
            
get ;
            
set ;
        }
        
public  Chessman( double  x,  double  y,  double  radius, Color color,  String  NAME,Chess parent)
        {
            Parent 
=  parent;
            initPoint 
=   new  Point(x, y);
            MovePoint 
=  InitPoint;
            radius 
=  radius;
            Color 
=  color;
            NAME 
=  NAME;
        }

 

 

 

好了,现在开始创建黑棋的函数代码相对简洁了一点,这里多了一个int[] y,用来干什么的?

本来啊,一开始是直接写数字的,后来比较了下黑棋和红棋的区别,就是这里的不同,可以交换棋子是在上面还是在下面。

棋子弄好时,只有兵 炮 上面一排或下面一排,其实就是只有6条线上有棋子,而且总是对称的。

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

private   void  CreateBlackChessman( int [] y)
        {
            
// 黑色棋 将一个,士、象、车、马、炮各两个,卒五个
            CreateChessman( 2 0 Y[ 0 ], Colors.black,  " " );
            CreateChessman(
2 1 " " );
            CreateChessman(
2 2 " " );
            CreateChessman(
2 3 " " );
            CreateChessman(
2 Y[ 1 ],  " " );
            CreateChessman(
5 Y[ 2 ],  " " );
            CreateChessman(
1 4 " " );
        }

 

再看一下红棋的创建函数,其实就一个样,名字不同而已

@H_311_801@

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

  private   void  CreateRedChessman( int [] y)
        {
            
// 红色棋 帅一个,仕、相、车、马、炮各两个,兵五个
            CreateChessman( 2 , Colors.Red,  " " );
            CreateChessman(
2 " " );
            CreateChessman(
2 " " );
            CreateChessman(
1 " " );
        }

 

好了,现在可以写出棋子初始化的函数

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

///   <sumMary>
        
///  初始化棋子
        
///   </sumMary>
         public   void  InitChessman()
        {
            
int [] up  =   new [] {  0 3  }; // 棋子在上面
             int [] down  =   new [] {  9 7 6  };  // 棋子在下面
            
            CreateBlackChessman(up);
            CreateRedChessman(down);
            ShowChessman();
        }
        
private   void  ShowChessman()
        {
            
foreach  (Chessman chessmane  in  ChessmanList)
            {
                chessmane.DrawIn(container);
            }
        }

 

这里就定义了棋子在上面还是下面了,换up和down换一下,棋子上下的位置也会发现变化,这里我们以后再利用。

多了一个ShowChessman(),一个循环而已,抽出来放到函数里了,不喜欢就把循环的代码弄上去,再不喜欢就到

ChessmanList.Add(chessmanE);的地方直接DrawIn一下,这样就少了这个函数了。

OK,棋盘和棋子都有了。

增加个构造函数传进入Panel先,还有实例化一个棋子列表,不然就报错啦。

 

   public  Chess(Panel control)
        {
            container 
=  control;
            ChessmanList 
=   new  List < Chessman > ( 32 );
        }

 

 

到界面里调用一下看看效果,silghtlight应用程序的调用代码要变一变了

 

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

public   partial   class  MainPage : UserControl
    {
        
public  MainPage()
        {
            initializeComponent();
            Chess chess 
=   new  Chess(canvas1);
            chess.InitBoard();
            chess.InitChessman();
        }
    }

 

F5运行了,出来了。。

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

 

yo~~只看到一个帅,还不错,挺帅的。。。

依呀哟,由于我们的原始坐标没有和像素坐标进行转换呢。

好了,在Chess类里增加一个坐标双向转换的函数

高级用法本来是加多一个参数true或false来进行是像素-》坐标,还是坐标->像素的。

后来发现坐标x+y怎么也比像素小,所以里面加个判断就搞定了。

里面还要用到棋盘的一些属性,所以方法放到这里来,是比较合适的。

@H_878_1197@

  ///   <sumMary>
        
///  棋子坐标和实际像素交换
        
///   </sumMary>
         public  Point SwitchPixelArray(Point point)
        {
            
if  (point.X  +  point.Y  >   17 ) // 最大值x=8,y=9,大于17则为像素 ,转为数组
            {
                
return   new  Point(((point.X  -  Board.marginLeft)  /  Board.gap), ((point.Y  -  Board.marginTop)  /  Board.gap));
            }
            
// 转为坐标
             return   new  Point(point.X  *  Board.gap  +  Board.marginLeft, point.Y  *  Board.gap  +  Board.marginTop);
        }

 

 

好了,回到之前的棋子类的Draw函数里,为Canvas增加@L_925_2@margin属性了。

 

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

  private   void  Draw()
        {
            
// 这里实现画啦
            Ellipse elp  =   new  Ellipse()
            {
                Width 
=  radius  *   2 ,
                Height 
=  radius  *   2 ,
                stroke 
=   new  SolidColorBrush(Color),
                Fill 
=   new  SolidColorBrush(Color),
                Opacity 
=   15
            };
            textBlock tex
=   new  textBlock()
            {
                textAlignment 
=  textAlignment.Center,
                Foreground 
=   new  SolidColorBrush(Colors.WhitE),
                tex
=  NAME,
                FontFamily 
=   new  FontFamily( " 宋体 " ),
                FontSize 
=  radius,
                FontWeight 
=  FontWeights.bold,
                Margin 
=   new  Thickness(Radius  /   2   -   2 , radius  /   2   -   2 0 )
            };
            chessma
=   new  Canvas();
            Point pixel 
=  Parent.SwitchPixelArray(InitPoint); // 新加的一行
            chessman.Margin  =   new  Thickness(pixel. -  radius, pixel. -  radius,  0 ); // 新加的一行
            chessman.Children.Add(elp);
            chessman.Children.Add(text);
            container.Children.Add(chessman);
        }

 

好了,现在重新运行,F5,看到效果图如下:

Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)

好了,棋子终于全部尘埃落地了。

到此,提交第一部分的源码:点击下载

作者博客http://cyq1162.cnblogs.com

大佬总结

以上是大佬教程为你收集整理的Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)全部内容,希望文章能够帮你解决Silverlight+WCF 新手实例 象棋 棋子定位与象棋类(四)所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。