善良的死神

 

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

大家不知道发现没有,我们已经完成了象棋与棋盘的结合,现在看到这里大家一定很Hight吧,首先我们已经卖出了一大步,但是在这里存在一些问题,不知道大家想了没想,我是在上一章弄了一个陷阱,怎么说呢?

1、如果我们提取chessman_name这个值,是不是看到了就是我们编写的最好一个坐标的NAME。

这个是为什么呢?

因为我在这里忽略了一点,就是应该在这些棋子放置在一个LIST里这样,才能使每个棋子都有自己的NAME。

所以在这里我修改了一下代码:

chessman类里的代码应该这样

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;
using System.Collections.Generic;

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 qizi(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;
         Draw();
        }
      
 
    }

}

这里只是把圆与中间的字表示出来,而且我通过实践算出了棋子的位置,就是能在棋盘十字的中间。

关于棋子的我就不多说了,不懂的看第二章。

在这里我重新命名了一个类

chess

先看看代码

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;
using chessman;
using System.Collections.Generic;
namespace chess

    public class qiziyupan
    {
        Panel qipanandzi;
        public qiziyupan(Panel zipan)
        {
            qipanandzi = zipan;
            chessmanlist = new List<qizi>(32);//初始化LIST
        }
        public List<qizi> chessmanlist
        {
            get;
            set;
        }
        private void chessman1(double x,double y,double radius, Color color,string name)
        {
            qizi chessman = new qizi(x, y, radius, color, name);
            chessmanlist.Add(chessman);
        }
        private void showchess()
        {
            foreach (qizi chessman in chessmanlist)
                chessman.DrawIn(qipanandzi);
        }
        public void chessman2()
        {
            chessman1(50, 50, 15, Colors.Red, "莗");

            chessman1(100, 50, 15, Colors.Red, "馬");

            chessman1(150, 50, 15, Colors.Red, "相");

            chessman1(200, 50, 15, Colors.Red, "仕");

            chessman1(250, 50, 15, Colors.Red, "帅");

            chessman1(450, 50, 15, Colors.Red, "莗");

            chessman1(400, 50, 15, Colors.Red, "馬");

            chessman1(350, 50, 15, Colors.Red, "相");

            chessman1(300, 50, 15, Colors.Red, "仕");

            chessman1(100, 150, 15, Colors.Red, "炮");

            chessman1(400, 150, 15, Colors.Red, "炮");

            chessman1(50, 200, 15, Colors.Red, "兵");

            chessman1(150, 200, 15, Colors.Red, "兵");

            chessman1(250, 200, 15, Colors.Red, "兵");

            chessman1(350, 200, 15, Colors.Red, "兵");

            chessman1(450, 200, 15, Colors.Red, "兵");
       
            //黑方
            chessman1(50, 500, 15, Colors.Black, "车");

            chessman1(100, 500, 15, Colors.Black, "马");

            chessman1(150, 500, 15, Colors.Black, "象");

            chessman1(200, 500, 15, Colors.Black, "士");

            chessman1(250, 500, 15, Colors.Black, "帅");

            chessman1(450, 500, 15, Colors.Black, "车");

            chessman1(400, 500, 15, Colors.Black, "马");

            chessman1(350, 500, 15, Colors.Black, "相");

            chessman1(300, 500, 15, Colors.Black, "士");

            chessman1(100, 400, 15, Colors.Black, "炮");

            chessman1(50, 350, 15, Colors.Black, "卒");

            chessman1(150, 350, 15, Colors.Black, "卒");

            chessman1(250, 350, 15, Colors.Black, "卒");

            chessman1(350, 350, 15, Colors.Black, "卒");

            chessman1(450, 350, 15, Colors.Black, "卒");

            chessman1(400, 400, 15, Colors.Black, "炮");
            showchess();
        }

        }
}
在了我们用到了LIST

把所有的棋子都归纳在LIST中,通过showchess函数来画出所有的棋子。

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

导航