善良的死神

 

编写网络象棋案例————Silverlight(盘与棋双剑合并)

最近一段时间工作原因比较忙,所以没又继续更新,乘着今晚有点时间,我继续说一下,象棋嘛有棋盘没有棋子是不能说得过去的,所以这章我们来学习一下(盘与棋双剑合并),第一节我们说了棋盘的制作,第二节我们说了棋子的制作,这节我们如何把棋谱加载在棋盘上面,其实本人做的方法很笨,但是我觉得这样做不是不可以38个棋子,按照坐标放置在棋谱上,首先我们来说代码,代码如下

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 chessman
{
   
    public class qizi
    {
        Panel qizi1;
        public Canvas chessman_qizi;
        /// <summary>
        /// 棋子名
        /// </summary>
        public string chessman_name
        {
            get;
            set;
        }
        /// <summary>
        /// 颜色
        /// </summary>
        public Color chessman_color
        {
            get;
            set;
        }
        /// <summary>
        /// 半径
        /// </summary>
        public double chessman_radius
        {
            get;
            set;
        }
        /// <summary>
        /// 当前坐标
        /// </summary>
        public Point chessman_dangqianzuobiao
        {
            get;
            set;
        }
        /// <summary>
        /// 移动坐标
        /// </summary>
        public Point chessman_move
        {
            get;
            set;
        }
        public double qizi_x
        {
            get;
            set;
        }
        public double qizi_y
        {
            get;
            set;
        }
       
     
       public void  qizi_p(double x,double y,double raduis,Color color,string name)
        {
            qizi_x = x;
            qizi_y = y;
            chessman_dangqianzuobiao = new Point(x, y);
            chessman_move = chessman_dangqianzuobiao;
            chessman_radius = raduis;
            chessman_color = color;
            chessman_name = name;
        }

        private void Draw()
        {
            //圆的制作
            Ellipse ell = new Ellipse()
            {
                Width=chessman_radius*2,
                Height=chessman_radius*2,
                Stroke=new SolidColorBrush(chessman_color),
                Fill=new SolidColorBrush(chessman_color),
                Opacity=15
            };
            //圆中的内容
            TextBlock chessman_text = new TextBlock()
            {
                TextAlignment=TextAlignment.Center,
                FontFamily=new FontFamily("宋体"),
                FontSize=chessman_radius,
                FontWeight=FontWeights.Bold,
                Foreground=new SolidColorBrush(Colors.White),
                Text=chessman_name,
                Margin=new Thickness(chessman_radius/2,chessman_radius/3-2.1,0,0),

            };
           
            chessman_qizi = new Canvas();
         chessman_qizi.Children.Add(ell);
            chessman_qizi.Children.Add(chessman_text);
            chessman_qizi.Margin = new Thickness(qizi_x -15 , qizi_y -15, 0, 0);//棋子的偏移量;
          qizi1.Children.Add(chessman_qizi);

        }
   
        public void DrawIn(Panel qizi2)
        {
         qizi1 = qizi2;
           //红方
         qizi_p(50, 50, 15, Colors.Red, "莗");
         Draw();
         qizi_p(100, 50, 15, Colors.Red, "馬");
         Draw();
         qizi_p(150, 50, 15, Colors.Red, "相");
         Draw();
         qizi_p(200, 50, 15, Colors.Red, "仕");
         Draw();
         qizi_p(250, 50, 15, Colors.Red, "帅");
         Draw();
         qizi_p(450, 50, 15, Colors.Red, "莗");
         Draw();
         qizi_p(400, 50, 15, Colors.Red, "馬");
         Draw();
         qizi_p(350, 50, 15, Colors.Red, "相");
         Draw();
         qizi_p(300, 50, 15, Colors.Red, "仕");
         Draw();
         qizi_p(100, 150, 15, Colors.Red, "炮");
         Draw();
         qizi_p(400, 150, 15, Colors.Red, "炮");
         Draw();
         qizi_p(50, 200, 15, Colors.Red, "兵");
         Draw();
         qizi_p(150, 200, 15, Colors.Red, "兵");
         Draw();
         qizi_p(250, 200, 15, Colors.Red, "兵");
         Draw();
         qizi_p(350, 200, 15, Colors.Red, "兵");
         Draw();
         qizi_p(450, 200, 15, Colors.Red, "兵");
         Draw();
         //黑方
         qizi_p(50, 500, 15, Colors.Black, "车");
         Draw();
         qizi_p(100, 500, 15, Colors.Black, "马");
         Draw();
         qizi_p(150, 500, 15, Colors.Black, "象");
         Draw();
         qizi_p(200, 500, 15, Colors.Black, "士");
         Draw();
         qizi_p(250, 500, 15, Colors.Black, "帅");
         Draw();
         qizi_p(450, 500, 15, Colors.Black, "莗");
         Draw();
         qizi_p(400, 500, 15, Colors.Black, "馬");
         Draw();
         qizi_p(350, 500, 15, Colors.Black, "相");
         Draw();
         qizi_p(300, 500, 15, Colors.Black, "仕");
         Draw();
         qizi_p(100, 400, 15, Colors.Black, "炮");
         Draw();
         qizi_p(400, 400, 15, Colors.Black, "炮");
         Draw();
         qizi_p(50, 350, 15, Colors.Black, "卒");
         Draw();
         qizi_p(150, 350, 15, Colors.Black, "卒");
         Draw();
         qizi_p(250, 350, 15, Colors.Black, "卒");
         Draw();
         qizi_p(350, 350, 15, Colors.Black, "卒");
         Draw();
         qizi_p(450, 350, 15, Colors.Black, "卒");
         Draw();
        }
    }

}

在上一章中,我做了细小的改动,为什么我们的棋子能在棋盘上放置呢,在上一章过程中,是否还记得

不管怎么设置坐标都无法放置棋子,主要的原因在于    chessman_qizi.Margin = new Thickness(qizi_x -15 , qizi_y -15, 0, 0);这个

的添加,没有这句话棋子始终在网页的左上角。

看了这个知道MARGIN的使用了吧!

这我们用了直接坐标法放置所有的棋子,其实我想一个问题,可不可以通过数组来放置

我总结了一下,觉得是行的,大家不知道发现没有有些坐标X轴相同,有些Y轴相同

我们可以通过相同的坐标建立一个FOR循环完成坐标的放置。

代码我不公布了,其实就是一个思路毕竟我事随笔写的。以上代码有些凌乱,

我没时间优化与整理,只能说运行成功,有兴趣的朋友自己修改与优化。

最后看看我们的成果

posted on 2011-05-25 15:53  善良的死神  阅读(197)  评论(0)    收藏  举报

导航