C # implement Chinese chess [board, chess piece ],

Source: Internet
Author: User

C # implement Chinese chess [board, chess piece ],

This article uses C # To draw the Chinese chess board and initialize the layout. It does not implement the playing logic of Chinese chess. For reference only.

Ideas:

Knowledge points:

As follows:(1)

(2)

Core code

The core code of the Board is as follows:

1 protected override void OnPaint (PaintEventArgs e) 2 {3 base. onPaint (e); 4 5 // initialize array 6 InitArrPieceInfo (); 7 8 Graphics g = e. graphics; 9 int width = this. width; 10 int height = this. height; 11 int padding = this. padding. all * 20; 12 int center = height/2; // vertical center position 13 int s_width = (width-2 * padding)/8; // The interval between each horizontal line is 14 int s_heigth = (height-2 * padding)/9; // The interval between each vertical line is 15 int start_x = padding; // start position 16 int start_y = padding; // start position 17 Pen pen = new Pen (Brushes. black, 1.5f); 18 Dictionary <string, string []> dicNums = new Dictionary <string, string []> (); 19 dicNums. add ("up", new string [9] {"1", "2", "3", "4", "5", "6", "7 ", "8", "9"}); 20 dicNums. add ("down", new string [9] {"Nine", "eight", "Seven", "Six", "five", "four", "three ", "2", "1"}); 21 Font font = new Font ("", 12, FontStyle. regular); 22 for (int I = 0; I <9; I ++) 23 {24 // nine bars in the vertical bar 25 Point p0 = new Point (start_x + I * s_width, start_y); 26 Point p1 = new Point (start_x + I * s_width, start_y + (s_heigth * 4); 27 Point p2 = new Point (start_x + I * s_width, start_y + (s_heigth * 5); 28 Point p3 = new Point (start_x + I * s_width, start_y + (s_heigth * 9); 29g. drawLine (pen, p0, p1); 30g. drawLine (pen, p2, p3); 31 // upper and lower texts 32 Point p_up = new Point (start_x + I * s_width-5, padding/2 ); 33 Point p_down = new Point (start_x + I * s_width-5, start_y + (s_heigth * 9) + padding/3); 34g. drawString (dicNums ["up"] [I], font, Brushes. black, p_up); 35g. drawString (dicNums ["down"] [I], font, Brushes. black, p_down); 36 // array value assignment 37 for (int j = 0; j <10; j ++) 38 {39 Point absLocation = ArrPiece [I, j]. absoluteLocation; 40 absLocation. X = start_x + I * s_width; 41 ArrPiece [I, j]. absoluteLocation = absLocation; 42} 43} 44 for (int I = 0; I <10; I ++) 45 {46 // 10x47 Point p0 = new Point (start_x, start_y + I * s_heigth); 48 Point p1 = new Point (start_x + s_width * 8, start_y + I * s_heigth); 49g. drawLine (pen, p0, p1); 50 // array value assignment 51 for (int j = 0; j <9; j ++) 52 {53 Point absLocation = ArrPiece [j, i]. absoluteLocation; 54 absLocation. Y = start_y + I * s_heigth; 55 ArrPiece [j, I]. absoluteLocation = absLocation; 56} 57} 58 // draw jiugong Ge 59 for (int I = 0; I <2; I ++) 60 {61 Point p0 = new Point (start_x + (3 + I * 2) * s_width, start_y); 62 Point p1 = new Point (start_x + (5-I * 2) * s_width, start_y + (s_heigth * 2); 63 Point p2 = new Point (start_x + (3 + I * 2) * s_width, start_y + (s_heigth * 7 )); 64 Point p3 = new Point (start_x + (5-I * 2) * s_width, start_y + (s_heigth * 9); 65g. drawLine (pen, p0, p1); 66g. drawLine (pen, p2, p3); 67} 68 69 // There is a corner between the soldier and the pawn, 70 from left to right for (int I = 0; I <5; I ++) 71 {72 int p_x = start_x + 2 * I * s_width; 73 int p_y = start_y + 3 * s_heigth; 74 DrawCorner (g, pen, p_x, p_y ); // 75 p_y = start_y + 6 * s_heigth; 76 DrawCorner (g, pen, p_x, p_y); // 77} 78 // corner of the gun, 79 for (int I = 0; I <2; I ++) 80 {81 int p_x = start_x + (1 + 6 * I) * s_width; 82 int p_y = start_y + 2 * s_heigth; 83 DrawCorner (g, pen, p_x, p_y); // gun 84 p_y = start_y + 7 * s_heigth; 85 DrawCorner (g, pen, p_x, p_y); // gun 86} 87 // draw the chuhehan 88 Point p_0 = new Point (2 * s_width, center-25 ); 89 Point p_1 = new Point (7 * s_width, center + 20); 90 font = new Font (" 正 ", 30, FontStyle. regular); 91g. drawString ("chuhe", font, Brushes. black, p_0); 92 Matrix mtxSave = g. transform; 93 Matrix mtxRotate = g. transform; 94 mtxRotate. rotateAt (180, p_1); 95g. transform = mtxRotate; 96g. drawString ("", font, Brushes. black, p_1); 97g. transform = mtxSave; 98 // draw the outer border 99g. drawRectangle (pen, 3, 3, width-6, height-6); 100g. drawRectangle (pen, 5, 5, width-10, height-10); 101g. drawRectangle (pen, 7, 7, width-14, height-14); 102}
View Code

The core code of the chess piece is as follows:

1 protected override void OnPaint (PaintEventArgs e) 2 {3 base. onPaint (e); 4 Graphics g = e. graphics; 5 GraphicsPath gPath = new GraphicsPath (); 6 // Set a new rectangle to the same size as the button's ClientRectangle property. 7 Rectangle rectangle = this. clientRectangle; 8g. drawEllipse (new Pen (this. flatAppearance. borderColor), rectangle); 9 gPath. addEllipse (rectangle); 10 11 // Set the button's Region property to the newly created circle region.12 this. region = new Region (gPath); 13 Rectangle inRect = new Rectangle (2, 2, this. width-4, this. height-3); 14g. fillEllipse (new SolidBrush (this. backColor), rectangle); 15 GB. drawEllipse (new Pen (Color. black, 2), inRect); 16 17 Font font = new Font ("", 25, FontStyle. regular); 18g. drawString (this. text, font, new SolidBrush (this. foreColor), 0, 5); 19}
View Code


Source code download link

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.