编写网络象棋案例————Silverlight(棋子篇)
在上一节我们讲解了棋盘的制作,在这一节我们来讲解一下关于棋子的制作,首先我们要知道制作棋子有2种方法(个人见解,学疏才浅)
1、自己画图:用Ellipse类(使用方法请参照Silverlight 3.0文档)
2、加载图片的方式
一般用 IMAGE中的URI进行加载,定位好坐标即可。
在这里体现我们的动手能力,就使用第一种吧(有特别需要的可以发帖,我有第二种方法)
说了那么多废话,现在开始
问:制作棋子需要什么信息呢?
答:当然是棋子名,颜色,坐标,棋子大小,棋子起始坐标,移动坐标
好了又了这些需求我们开始编写程序:
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;
}
/// <summary>
/// 初始化构造函数
/// </summary>
public qizi(double x,double y,double raduis,Color color,string name)
{
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.Green),
Text=chessman_name,
Margin=new Thickness(chessman_radius/2-1,chessman_radius/2-1,0,0),
};
chessman_qizi = new Canvas();
chessman_qizi.Children.Add(ell);
chessman_qizi.Children.Add(chessman_text);
qizi1.Children.Add(chessman_qizi);
}
public void DrawIn(Panel qizi2)
{
qizi1 = qizi2;
Draw();
}
}
}
存在一个问题,就是不能更好的把字完全居中有更好办法的朋友请与我联系。
浙公网安备 33010602011771号