首页 > 学院 > 开发设计 > 正文

兵棋系列2----兵棋游戏中地图滑动和委托消息

2019-11-17 02:44:30
字体:
来源:转载
供稿:网友

兵棋系列2----兵棋游戏中地图滑动和委托消息

  前几天写了一个六边形阵列的算法,今天周末比较闲,下午没事就做了做兵棋的地图操作,一点一点的做吧,总会做好,毕竟我也经常玩各种棋,对做一个这类型的小游戏非常感兴趣。

首先来解释下,下面要出现代码的操作。如上图,当鼠标指针移动到地图的四个边时,地图会自动左右上下滑动(地图比这个from要大很多,不这么做地图显示不完整了,不要跟我说用滚动条,那个给人感觉不好,这也是兵棋里不会缺少的操作吧),同时下面的消息框会记录鼠标的位置,这个消息框前期为我开发时显示一些测试信息用的,后期应该会把它改成一个功能区(部队参数、将领参数、环境参数、消息显示等等吧)

  下面把源代码放出来,懂得大大们可以指点下怎么做。


  C#开发中,控件和控件之间的消息传递有多种方式,我一般选择委托,毕竟灵活方便。以下是两种传递消息的模式,我选择了第一个,放弃了第二个;其实个人觉得第二个方法更优秀,它作为一个继承基类,可以很安全的把消息传递给它的上层类;第一种采用静态,优势是灵活方便,弊端是一个委托几乎只在一个功能上使用。

选择的传递消息模式

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5   6 namespace UI  7 {  8     public enum MsgType  9     { 10         /// <summary> 11         /// 常规消息显示(黑色) 12         /// </summary> 13         Show, 14         /// <summary> 15         /// 提示信息(绿色) 16         /// </summary> 17         Info, 18         /// <summary> 19         /// 状态栏显示 20         /// </summary> 21         State, 22         /// <summary> 23         /// 错误消息,可识别,可控类型(蓝色) 24         /// </summary> 25         Error, 26         /// <summary> 27         /// 警告消息,程序异常,不可处理(红色) 28         /// </summary> 29         Warn, 30         /// <summary> 31         /// 他人发送消息的颜色 32         /// </summary> 33         OtherMessage, 34         /// <summary> 35         /// 本人发送消息的颜色 36         /// </summary> 37         OwnerMessage 38     } 39  40     class MsgEventArgs 41     { 42         PRivate string _message; 43  44         public string Message 45         { 46             get { return _message; } 47             set { _message = value; } 48         } 49  50         private MsgType _type; 51  52         public MsgType Type 53         { 54             get { return _type; } 55             set { _type = value; } 56         } 57  58         public MsgEventArgs(string msg) 59         { 60             this._message = msg; 61  62             this._type = MsgType.Show; 63         } 64         public MsgEventArgs(string msg, MsgType type) 65         { 66             this._message = msg; 67             this._type = type; 68         } 69     } 70  71     class MsgEvnet 72     { 73         public delegate void MsgEvent(object sender, MsgEventArgs msg); 74         public static event MsgEvent msgEvent; 75  76         public static void SendMsg(string msg) 77         { 78             if (MsgEvnet.msgEvent != null) 79             { 80                 MsgEvnet.msgEvent(null, new MsgEventArgs(msg)); 81             } 82         } 83  84         public static void SendMsg(string msg, MsgType type) 85         { 86             if (MsgEvnet.msgEvent != null) 87             { 88                 MsgEvnet.msgEvent(null, new MsgEventArgs(msg, type)); 89             } 90         } 91  92         public static void SendMsg(object sender, string msg) 93         { 94             if (MsgEvnet.msgEvent != null) 95             { 96                 MsgEvnet.msgEvent(sender, new MsgEventArgs(msg)); 97             } 98         } 99 100         public static void SendMsg(object sender, string msg, MsgType type)101         {102             if (MsgEvnet.msgEvent != null)103             {104                 MsgEvnet.msgEvent(sender, new MsgEventArgs(msg, type));105             }106         }107     }108 }
消息委托类

放弃的传递消息模式

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5  6 namespace UI 7 { 8     public enum MsgType 9     {10         /// <summary>11         /// 常规消息显示(黑色)12         /// </summary>13         Show,14         /// <summary>15         /// 提示信息(绿色)16         /// </summary>17         Info,18         /// <summary>19         /// 状态栏显示20         /// </summary>21         State,22         /// <summary>23         /// 错误消息,可识别,可控类型(蓝色)24         /// </summary>25         Error,26         /// <summary>27         /// 警告消息,程序异常,不可处理(红色)28         /// </summary>29         Warn,30         /// <summary>31         /// 他人发送消息的颜色32         /// </summary>33         OtherMessage,34         /// <summary>35         /// 本人发送消息的颜色36         /// </summary>37         OwnerMessage38     }39 40     public class MsgEventArgs : EventArgs41     {42         public string Message;43         public MsgType Type;44 45         public MsgEventArgs(string msg)46         {47             this.Message = msg;48 49             this.Type = MsgType.Show;50         }51         public MsgEventArgs(string msg, MsgType type)52         {53             this.Message = msg;54             this.Type = type;55         }56     }57 58     public class MsgEvent59     {60         public event EventHandler<MsgEventArgs> msgEvent;61 62         public void SendMsg(string msg)63         {64             if (this.msgEvent != null)65             {66                 this.msgEvent(this, new MsgEventArgs(msg));67             }68         }69 70         public void SendMsg(string msg, MsgType type)71         {72             if (this.msgEvent != null)73             {74                 this.msgEvent(this, new MsgEventArgs(msg, type));75             }76         }77     }78 }
放弃的消息传递模式


  下面这段代码之前放出来过(http://www.VEVb.com/preacher/p/4105810.html),做了几点修改,计算的方式:根据每个正六边形的中心点,计算出下三边的相对位置然后绘制下三边。以下算法中,把之前的基类Control改为了Label,主要方便控件透明,Control为基类是不支持控件透明。

六边形阵列绘制算法

  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 UI.Controls  8 {  9     public class SixSidesControl : Label 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(
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表