标签:des style class blog http tar
在项目之间有段“空项期”,上个项目刚刚完成,下个项目还没落实,时间比较充裕。去年9月份就经历了这么一次短暂的“空项期”,那时偶还是一名前端工作者,C#使用起来毫不含糊,还自己整过一个类SCSF的MVP框架AngelFrame(详见之前博客:http://www.cnblogs.com/wgp13x/p/99c2adc52d8f0dff30a038841ac32872.html)。在那段“空项期”之前,有位朋友托我做个小游戏,偶也满口的答应,只可惜之前项目太忙没时间做,就一直耽搁了,正好有这段“空项期”,所以做了一下,现在回想起来,做这个小游戏的过程中还是学习到了不少东西的,因为做游戏跟做项目的常用技术不同,于是在这里总结一下,别把这么宝贵的经验给弄丢了。
public class Network
{
private static Network _instace;
private const int Port = 100;
public static UdpClient UdpClient;
private static Encoding encoding = Encoding.GetEncoding("gb2312");
public static string HostName;
public static IPAddress IpAddress;
private static Thread listener;
public event EventHandler<GameMsg> ReceivedMsg;
public static Network Instance
{
get
{
if (_instace == null)
_instace = new Network();
return _instace;
}
}
private Network()
{
UdpClient = new UdpClient(Port);
HostName = Dns.GetHostName();
foreach (IPAddress ip in Dns.GetHostAddresses(HostName))
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
IpAddress = ip;
break;
}
}
}
//局域网内广播,在上线时调用,通知其它Client有人上线了
public void Broadcast(GameMsg msg)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
byte[] bytes = Tools.Serialize(msg);
UdpClient.Send(bytes, bytes.Length, ipEndPoint);
}
//接收消息线程入口,收到消息后触发各类事件
public void ReceiveMsg()
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Broadcast, Port);
while (true)
{
byte[] bytes = null;
try
{
bytes = UdpClient.Receive(ref ipEndPoint);
}
catch (SocketException ex)
{
return;
}
GameMsg msg = (GameMsg)Tools.Deserialize(bytes);
if (ReceivedMsg != null && !msg.IpAddress.Equals(IpAddress))
ReceivedMsg(this, msg);
}
}
//向某IP定向发送消息
public void Send(IPAddress ip, GameMsg msg)
{
IPEndPoint ipEndPoint = new IPEndPoint(ip, Port);
byte[] bytes = Tools.Serialize(msg);
UdpClient.Send(bytes, bytes.Length, ipEndPoint);
}
//向一些IP定向发送消息
public void Send(IPAddress[] ips, GameMsg msg)
{
foreach (IPAddress ipAddress in ips)
{
if (!ipAddress.Equals(IpAddress))
Send(ipAddress, msg);
}
}
}
|
[Serializable]
public class GameMsg : EventArgs
{
public MsgTypeEnum MsgType;
public IPAddress IpAddress;
public object MsgContent;
}
[Serializable]
public class LandGameMsg //OnlineReply, Hello
{
public string HostName;
public string UserName;
}
[Serializable]
public class CreateGameMsg
{
public string CreaterHostName;
public string CreaterUserName;
public IPAddress[] SelectedIpAddresses;
public string[] SelectedHostNames;
}
......
|
public partial class MainForm : Form
{
public static Thread Listener = new Thread(new ThreadStart(Network.Instance.ReceiveMsg)) { Name = "receiveMsg", Priority = ThreadPriority.Highest };
public static CurrStatEnum CurrStat = CurrStatEnum.Idle;
private static readonly MsgTypeEnum[] interestMsgTypes = new MsgTypeEnum[] { MsgTypeEnum.QuitGame };
public MainForm()
{
Listener.Start();
Network.Instance.ReceivedMsg += new EventHandler<GameMsg>(_network_ReceivedMsg);
Network.Instance.Broadcast(new GameMsg() { MsgType = MsgTypeEnum.LandGame, IpAddress = Network.IpAddress, MsgContent = new LandGameMsg() { HostName = Network.HostName } });
}
private delegate void Delegate_ReceivedMsg(GameMsg msg);
void _network_ReceivedMsg(object sender, GameMsg e)
{
Delegate_ReceivedMsg myDelegate = new Delegate_ReceivedMsg(handleReceivedMsg);
if (interestMsgTypes.Contains(e.MsgType))
Invoke(myDelegate, e);
}
void handleReceivedMsg(GameMsg msg)
{
switch (msg.MsgType)
{
case MsgTypeEnum.QuitGame: //收到某人退出游戏请求
MessageBox.Show("有小伙伴要求退出游戏");
ucUsersInGame_QuitGame(null, null);
break;
}
}
}
|
public partial class InviteOthers : Form
{
private static readonly MsgTypeEnum[] interestMsgTypes = new MsgTypeEnum[] { MsgTypeEnum.OnlineReply };
public List<GameMsg> OnlineReplys = new List<GameMsg>();
void _network_ReceivedMsg(object sender, GameMsg msg)
{
if (interestMsgTypes.Contains(msg.MsgType))
{
switch (msg.MsgType)
{
case MsgTypeEnum.OnlineReply:
OnlineReplys.Add(msg);
break;
}
}
}
}
|
/// 监控鼠标和窗口位置
private void timer1_Tick(object sender, EventArgs e)
{
int mouse_x = Cursor.Position.X, mouse_y = Cursor.Position.Y;
int window_x = this.Location.X, window_y = this.Location.Y;
int window_width = this.Size.Width, window_height = this.Size.Height;
if (isHiding == false && window_y == 0)
{
if (window_x - mouse_x > 10 || mouse_x - window_x - window_width > 10
|| mouse_y - window_y - window_height > 10)
{
timer1.Enabled = false;
timer2.Enabled = true;
}
}
if (isHiding == true && mouse_y <= 1 && mouse_x > window_x &&
mouse_x < window_x + window_width)
{
timer1.Enabled = false;
timer3.Enabled = true;
}
}
/// 隐藏界面
private void timer2_Tick(object sender, EventArgs e)
{
int window_height = this.Size.Height;
startY += window_height / 8;
if (startY < window_height)
{
this.Location = new Point(this.Location.X, -startY);
}
else
{
this.Location = new Point(this.Location.X, 1 - window_height);
isHiding = true;
timer2.Enabled = false;
timer1.Enabled = true;
}
}
/// 显示界面
private void timer3_Tick(object sender, EventArgs e)
{
int window_height = this.Size.Height;
startY -= window_height / 8;
if (startY > 0)
{
this.Location = new Point(this.Location.X, -startY);
}
else
{
this.Location = new Point(this.Location.X, 0);
isHiding = false;
timer3.Enabled = false;
timer1.Enabled = true;
}
}
|
标签:des style class blog http tar
原文地址:http://www.cnblogs.com/wgp13x/p/3800030.html