c# 繪制中國(guó)象棋棋盤(pán)與棋子
本文是利用C# 實(shí)現(xiàn)中國(guó)象棋的棋盤(pán)繪制,以及初始化布局,并不實(shí)現(xiàn)中國(guó)象棋的對(duì)弈邏輯。僅供學(xué)習(xí)參考使用。
思路:
- 繪制中國(guó)象棋棋盤(pán),豎線(xiàn)九條,橫線(xiàn)十條。再中間繪制‘楚河',‘漢界' 。
- 繪制棋子,然后將棋子布局在棋盤(pán)上即可。
涉及知識(shí)點(diǎn):
- 用戶(hù)控件:用于實(shí)現(xiàn)棋盤(pán)的繪制,重寫(xiě) OnPaint(PaintEventArgs e) 方法。
- Matrix:封裝表示幾何變換的 3x3 仿射矩陣。本例中主要用于旋轉(zhuǎn)繪制反方的‘漢界'。
- GraphicsPath:表示一系列相互連接的直線(xiàn)和曲線(xiàn)。本例中主要用于繪制圓形棋子。
效果圖如下:
(一)
(二)
核心代碼
棋盤(pán)核心代碼如下:
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); //初始化數(shù)組 InitArrPieceInfo(); Graphics g = e.Graphics; int width = this.Width; int height = this.Height; int padding = this.Padding.All * 20; int center = height / 2;//垂直中心位置 int s_width = (width - 2 * padding) / 8;//每一條橫線(xiàn)的間距 int s_heigth = (height - 2 * padding) / 9;//每一條豎線(xiàn)的間距 int start_x = padding;//起始位置 int start_y = padding;//起始位置 Pen pen = new Pen(Brushes.Black, 1.5f); Dictionary<string, string[]> dicNums = new Dictionary<string, string[]>(); dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" }); dicNums.Add("down", new string[9] { "九", "八", "七", "六", "五", "四", "三", "二", "一" }); Font font = new Font("宋體", 12, FontStyle.Regular); for (int i = 0; i < 9; i++) { //豎線(xiàn)九條 Point p0 = new Point(start_x + i * s_width, start_y); Point p1 = new Point(start_x + i * s_width, start_y + (s_heigth * 4)); Point p2 = new Point(start_x + i * s_width, start_y + (s_heigth * 5)); Point p3 = new Point(start_x + i * s_width, start_y + (s_heigth * 9)); g.DrawLine(pen, p0, p1); g.DrawLine(pen, p2, p3); //上下的文字 Point p_up = new Point(start_x + i * s_width - 5, padding / 2); Point p_down = new Point(start_x + i * s_width - 5, start_y + (s_heigth * 9) + padding / 3); g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up); g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down); //數(shù)組賦值 for (int j = 0; j < 10; j++) { Point absLocation = ArrPiece[i, j].AbsoluteLocation; absLocation.X = start_x + i * s_width; ArrPiece[i, j].AbsoluteLocation = absLocation; } } for (int i = 0; i < 10; i++) { //橫線(xiàn)十條 Point p0 = new Point(start_x, start_y + i * s_heigth); Point p1 = new Point(start_x + s_width * 8, start_y + i * s_heigth); g.DrawLine(pen, p0, p1); //數(shù)組賦值 for (int j = 0; j < 9; j++) { Point absLocation = ArrPiece[j, i].AbsoluteLocation; absLocation.Y = start_y + i * s_heigth; ArrPiece[j, i].AbsoluteLocation = absLocation; } } //繪制九宮格 for (int i = 0; i < 2; i++) { Point p0 = new Point(start_x + (3 + i * 2) * s_width, start_y); Point p1 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 2)); Point p2 = new Point(start_x + (3 + i * 2) * s_width, start_y + (s_heigth * 7)); Point p3 = new Point(start_x + (5 - i * 2) * s_width, start_y + (s_heigth * 9)); g.DrawLine(pen, p0, p1); g.DrawLine(pen, p2, p3); } //兵和卒處有拐角,從左往右 for (int i = 0; i < 5; i++) { int p_x = start_x + 2 * i * s_width; int p_y = start_y + 3 * s_heigth; DrawCorner(g, pen, p_x, p_y);//兵 p_y = start_y + 6 * s_heigth; DrawCorner(g, pen, p_x, p_y);//卒 } //炮處的拐角,從左往右 for (int i = 0; i < 2; i++) { int p_x = start_x + (1 + 6 * i) * s_width; int p_y = start_y + 2 * s_heigth; DrawCorner(g, pen, p_x, p_y);//炮 p_y = start_y + 7 * s_heigth; DrawCorner(g, pen, p_x, p_y);//炮 } //繪制楚河漢界 Point p_0 = new Point(2 * s_width, center - 25); Point p_1 = new Point(7 * s_width, center + 20); font = new Font("方正隸二繁體", 30, FontStyle.Regular); g.DrawString("楚河", font, Brushes.Black, p_0); Matrix mtxSave = g.Transform; Matrix mtxRotate = g.Transform; mtxRotate.RotateAt(180, p_1); g.Transform = mtxRotate; g.DrawString("漢界", font, Brushes.Black, p_1); g.Transform = mtxSave; //繪制外邊框 g.DrawRectangle(pen, 3, 3, width - 6, height - 6); g.DrawRectangle(pen, 5, 5, width - 10, height - 10); g.DrawRectangle(pen, 7, 7, width - 14, height - 14); }
棋子核心代碼如下:
protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics g = e.Graphics; GraphicsPath gPath = new GraphicsPath(); // Set a new rectangle to the same size as the button's ClientRectangle property. Rectangle rectangle = this.ClientRectangle; g.DrawEllipse(new Pen(this.FlatAppearance.BorderColor), rectangle); gPath.AddEllipse(rectangle); // Set the button's Region property to the newly created circle region. this.Region = new Region(gPath); Rectangle inRect = new Rectangle(2, 2, this.Width - 4, this.Height - 3); g.FillEllipse(new SolidBrush(this.BackColor), rectangle); g.DrawEllipse(new Pen(Color.Black,2), inRect); Font font = new Font("楷體", 25, FontStyle.Regular); g.DrawString(this.Text, font, new SolidBrush(this.ForeColor), 0,5); }
以上就是c# 繪制中國(guó)象棋棋盤(pán)與棋子的詳細(xì)內(nèi)容,更多關(guān)于c# 繪制棋盤(pán)與棋子的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并)
C#歸并排序的實(shí)現(xiàn)方法(遞歸,非遞歸,自然歸并),需要的朋友可以參考一下2013-04-04C# FileStream實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)續(xù)傳
這篇文章主要為大家詳細(xì)介紹了C# FileStream實(shí)現(xiàn)多線(xiàn)程斷點(diǎn)續(xù)傳,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03C#使用RabbitMq隊(duì)列(Sample,Work,Fanout,Direct等模式的簡(jiǎn)單使用)
這篇文章主要介紹了C#使用RabbitMq隊(duì)列(Sample,Work,Fanout,Direct等模式的簡(jiǎn)單使用),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10C#中前臺(tái)線(xiàn)程和后臺(tái)線(xiàn)程的區(qū)別與聯(lián)系
這篇文章主要介紹了C#中前臺(tái)線(xiàn)程和后臺(tái)線(xiàn)程的區(qū)別與聯(lián)系,本文先講解了它們的區(qū)別,然后給出了一個(gè)例子來(lái)驗(yàn)證這些區(qū)別,需要的朋友可以參考下2015-06-06微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建
這篇文章主要介紹了微信公眾平臺(tái)開(kāi)發(fā)教程(三) 基礎(chǔ)框架搭建,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12C#實(shí)現(xiàn)SMTP服務(wù)發(fā)送郵件的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)SMTP服務(wù)發(fā)送郵件的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12c#實(shí)現(xiàn)適配器模式的項(xiàng)目實(shí)踐
適配器模式將一個(gè)類(lèi)的接口轉(zhuǎn)換成客戶(hù)希望的另一個(gè)接口,使得原本由于接口不兼容而不能一起工作的那些類(lèi)可以一起工作,本文主要介紹了c#實(shí)現(xiàn)適配器模式的項(xiàng)目實(shí)踐,感興趣的可以一起來(lái)了解一下2023-08-08