兵棋---棋盤繪制算法(六邊形陣列算法)


  最近玩一些手機游戲、ipad游戲、或者pc戰棋游戲,感覺在很多游戲中融入了正六邊形,作為地圖模塊中的最小單元,打破了常規的方塊模式,不僅提升了趣味性,也刺激了我們開發者的味覺。其實,我早先就想過做一種新棋,有着自己特殊的規則和棋盤模式,最好能像機器人大戰戰棋游戲一樣(我還是太愛機器人大戰了,打小就開始玩),想法就是建立在正六邊形或者三角形為最小單元的棋盤上(跳棋最小單元是三角模式、象棋最小單元是方塊模式),今天這個算法,或許能在我以后閑的沒事的時候,為做一些自己喜歡的東西開個引子。

 

 

 

   代碼是下午閑的無聊的時候,亂敲的,比較簡單,可能不是那么規范,觀者自查,見諒。

   計算的方式:根據中心點,計算出下三邊的位置然后繪制下三邊,每一個六邊形只繪制了下三邊啊,六邊繪制純屬浪費性能。

   本算法極其簡單,只繪制正六邊形的陣列,不做尋路算法,那個誰喜歡做就去試試吧,應該也不是很難。可以用BFS算法,個人覺得,有步數限制的使用這種算法比較好。

 


 

  1 using System;
  2 using System.Drawing.Drawing2D;
  3 using System.Drawing;
  4 using System.Windows.Forms;
  5 using System.Collections.Generic;
  6 
  7 namespace SixSidesMap
  8 {
  9     public class SixSidesControl : Control
 10     {
 11         double G3 = Math.Sin(60 * Math.PI / 180);//二分之根號三
 12         private int m_sideLength = 20;
 13 
 14         public int SideLength
 15         {
 16             get { return m_sideLength; }
 17             set
 18             {
 19                 m_sideLength = value;
 20                 Invalidate();
 21             }
 22         }
 23 
 24 
 25         private float m_lineThickness = 1;
 26 
 27         public float LineThickness
 28         {
 29             get { return m_lineThickness; }
 30             set
 31             {
 32                 m_lineThickness = value;
 33                 Invalidate();
 34             }
 35         }
 36 
 37 
 38         private Color m_lineColor = Color.Black;
 39 
 40         public Color LineColor
 41         {
 42             get { return m_lineColor; }
 43             set
 44             {
 45                 m_lineColor = value;
 46                 Invalidate();
 47             }
 48         }
 49 
 50         public SixSidesControl()
 51         {
 52             SetStyle(ControlStyles.UserPaint, true);
 53             SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 54             SetStyle(ControlStyles.DoubleBuffer, true); 
 55         }
 56 
 57         protected override void OnPaint(PaintEventArgs pe)
 58         {
 59             //橫線,三被的邊長
 60             //縱線,根號三倍的邊長
 61             List<float> xList = new List<float>();
 62             List<float> yList = new List<float>();
 63 
 64             int maxx = this.Width / (3 * m_sideLength);
 65             int maxy = (int)(this.Height / (G3 * m_sideLength));
 66 
 67             for (int y = 0; y <= maxy; y++)
 68             {
 69                 float curHeight =(float)( y * G3 * m_sideLength);
 70                 for (int x = 0; x <= maxx; x++)
 71                 {
 72                     float curWidth;
 73                     if (y % 2 == 0)
 74                         curWidth = (float)(x * 3 * m_sideLength);
 75                     else
 76                         curWidth = (float)((x * 3 + 1.5) * m_sideLength);
 77 
 78                     yList.Add(curHeight);
 79                     xList.Add(curWidth);
 80                 }
 81             }
 82 
 83             OnPaint(pe, xList.ToArray(), yList.ToArray());
 84 
 85             base.OnPaint(pe);
 86         }
 87 
 88         private void OnPaint(PaintEventArgs pe, float[] x, float[] y)
 89         {
 90             pe.Graphics.SmoothingMode = SmoothingMode.HighQuality;
 91             using (Pen pen = new Pen(new SolidBrush(m_lineColor), m_lineThickness))
 92             {
 93                 pen.StartCap = LineCap.Round;
 94                 pen.EndCap = LineCap.Round;
 95 
 96                 for (int i = 0; i < x.Length; i++)
 97                 {
 98                     //9點方向的點
 99                     float px1 = (float)(x[i] - m_sideLength);
100                     float py1 = (float)(y[i]);
101 
102                     ////11點方向的點
103                     //float px2 = (float)(x[i] - 0.5 * m_sideLength);
104                     //float py2 = (float)(y[i] - G3 * m_sideLength);
105 
106                     ////1點方向的點
107                     //float px3 = (float)(x[i] + 0.5 * m_sideLength);
108                     //float py3 = (float)(y[i] - G3 * m_sideLength);
109 
110                     //3點方向的點
111                     float px4 = (float)(x[i] + m_sideLength);
112                     float py4 = (float)(y[i]);
113 
114                     //5點方向的點
115                     float px5 = (float)(x[i] + 0.5 * m_sideLength);
116                     float py5 = (float)(y[i] + G3 * m_sideLength);
117 
118                     //7點方向的點
119                     float px6 = (float)(x[i] - 0.5 * m_sideLength);
120                     float py6 = (float)(y[i] + G3 * m_sideLength);
121 
122                     //pe.Graphics.DrawLine(pen, px1, py1, px2, py2);
123                     //pe.Graphics.DrawLine(pen, px2, py2, px3, py3);
124                     //pe.Graphics.DrawLine(pen, px3, py3, px4, py4);
125                     //pe.Graphics.DrawLine(pen, px4, py4, px5, py5);
126                     //pe.Graphics.DrawLine(pen, px5, py5, px6, py6);
127                     //pe.Graphics.DrawLine(pen, px6, py6, px1, py1);
128 
129                     pe.Graphics.DrawLines(pen, new PointF[]
130                     {   
131                         new PointF(px4, py4),
132                         new PointF(px5, py5), 
133                         new PointF(px6, py6),
134                         new PointF(px1, py1)
135                     });
136                 }
137             }
138         }
139     }
140 }
自繪制六邊形陣列

 

 

  這只是一個Control,請在C#下自建一個窗體,然后將此控件拖入即可使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM