There are pieces, the pieces should be some property on board, according to the original impulse to create a new pawn class.
What the google translation piece E under article called, he found a called Chessman, then add a right-front project Chessman.cs
Mess:
Thought, still looking for pieces to draw a good picture, the picture is not easy to find, online search is still there, but the picture ah, forget, or painted pieces, anyway, the board is painted. So how thought painting, first draw a circle, which then draw a word. Circle to say, there is a class Ellipse, you have to set about the property. Painting the word, so he GDI + in the direction of thought, to find what DrawString, fly for a long time could not find the direction, no way, to finally settled on a TextBlock.
What does this piece should have, and thought, basically you have to have is:
Name, has a name, called "soldiers", "gun", the name of the class
Color, red is not a black
RADIUS, the radius of pieces, which pieces have a total size.
InitPoint, pieces of coordinate positions
MovePoint, pieces gotta move, moved position.
Well, in the class typing "prop" double-click the Tab key, type-written, press Tab, play a good name, a carriage return, a finished property will continue to repeat, so with the following code:
Double the Radius
{
GET ;
SET ;
}
/// <Summary> /// piece array coordinate /// </ Summary> public Point InitPoint { GET ; SET ; } /// <Summary> /// mobile array after coordinate / // </ Summary> public Point MovePoint { GET ; SET ; } }
We add a constructor to initialize the data about the property
{
InitPoint = new Point(x, y);
MovePoint = InitPoint;
Radius = radius;
Color = color;
Name = name;
}
Well, we want to draw the pieces
Refer to the drawing board, we added two new functions, a Panel of the container to. DrawIn for external calls.
public void DRAWIN (Control Panel) { Container = Control; the Draw (); } Private void the Draw () { // Here it Videos achieved }
How to paint how to paint, anxious Lala.
Think it would like to draw a circle, and also draw a word, the two have combined into one. Thus a new Canvas, Canvas then placed inside two. In this way the Canvas as a pawn. Because sooner or later you want to move the pieces, you can not just move a circle, or move a word of it.
Well, look at the circle of the code, is assigned some basic properties of the trouble:
{
Width = Radius * 2 ,
Height = Radius * 2 ,
Stroke = new SolidColorBrush(Color),
Fill = new SolidColorBrush(Color),
Opacity = 15
};
Well, look at the text of the code, is assigned a bunch of property values trouble, the margin value is really hard to tune it, the pair is not very homogeneous, it will point:
{
TextAlignment = TextAlignment.Center,
Foreground = new SolidColorBrush(Colors.White),
Text = Name,
FontFamily = new FontFamily( " 宋体 " ),
FontSize = Radius,
FontWeight = FontWeights.Bold,
Margin = new Thickness(Radius / 2 - 2 , Radius / 2 - 2 , 0 , 0 )
};
Next to a New Canvas, Talia added to the list, and finally put the pieces in a container Panel
chessman.Children.Add(elp);
chessman.Children.Add(text);
container.Children.Add(chessman);
Front chessman ye not Canvas, is written less? Not, here is the definition of it to go outside, and the back should be used.
public Canvas chessman; // pieces, this definition outside.
This, almost like a pawn, but now two less emphasis.
1. Is the location of a pawn, the position of the pieces can be adjusted by the Margin property, but let's not write, because the original coordinates should turn into pixel coordinates,
Separate pieces classes can not complete such a thing, so keep until the next Chess Chess we add a new class to control.
2. Is clicked event pawn, chess pawn your total first selected, you can not select the next not.
We stay focused on these two important qualification to the next section.
We went to the local chapter of the New Board again below New Chessman a piece, to see the effect.
In Silverlight application interface code:
{
public MainPage()
{
InitializeComponent();
Board board = new Board();
board.DrawIn(canvas1);
Chessman chessman = new Chessman( 0 , 0 , 15 , Colors.Red, " 车 " );
chessman.DrawIn(canvas1);
}
}
Run, see renderings, a piece came out:
Well, call it a day kick. The next section focuses pieces and then complete the location and click event.
Reproduced in: https: //my.oschina.net/secyaher/blog/274085