打飞机游戏
最近接到一个小小的活,做个打飞机游戏,通过socket连接数据,因为用户各种原因,最后退款了,所以我把源码发上来,给大家做个参考
要求是这样的,
双方摆放战机,摆放好以后,输入坐标,打飞机,打到头部,就算对方死亡,打到身体,就算受伤,打到空白处,算无效攻击,界面是这样的
其实这个游戏比较简单,主要有两点,一个是数据的接收和返回,一个是界面的显示,当然,ai部分,我用的随机位置来确定了,因为也没要求.
private void ReceiveTask()
{
Task.Run(() =>
{
while (true)
{
byte[] buffer = new byte[32];
int TotalReceiveLength = 0;
NetworkStream stream = client.GetStream();
do
{
var reclen = stream.Read(buffer, TotalReceiveLength, buffer.Length - TotalReceiveLength);
if (reclen == 0) break;
TotalReceiveLength += reclen;
} while (stream.DataAvailable);
string msg = Encoding.UTF8.GetString(buffer);
msg=msg.TrimEnd('\0');
var arr = msg.Split(new char[] { ',' });
this.Invoke(new Action(() =>
{
if (arr[0] == "0")
{
//敌方轰炸
var colindex = int.Parse(arr[1]);
var rowindex = int.Parse(arr[2]);
var v = arr1[colindex - 1, rowindex - 1];
string retmsg ="1,"+ arr[1] + "," + arr[2] + ",";
v.isboom = true;//设定为该点被轰炸
if (v.status == 0)
{
//空
retmsg += "0,";
}
else if (v.status == 1)
{
//伤
retmsg += "1,";
}
else if (v.status == 2)
{
//死
retmsg += "2,";
}
//判断是否游戏结束,我方所有飞机都被炸死?
List<MyGridCell> list = new List<MyGridCell>();
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
list.Add(arr1[j, i]);
}
var count = list.Where(p => p.isboom).Where(p => p.status == 2).Count();
if (count == 3)
retmsg += "1";
else retmsg += "0";//游戏结束标记
//返回轰炸结果
byte[] sendbuf = Encoding.UTF8.GetBytes(retmsg);
stream = client.GetStream();
stream.Write(sendbuf, 0, sendbuf.Length);
button3.Enabled = true;
if (count == 3)
{
lblstatus.Text = "你输了!";
button3.Enabled = false;
}
else
{
if (checkBox1.Checked)
{
//自动下棋
int x=0;
int y=0;
bool bfind = false;
while(!bfind)
{
x = r.Next(1, 11);
y = r.Next(1, 11);
if(arr2[x-1,y-1]==-1)
{
bfind = true;
break;
}
}
textBox1.Text = x.ToString() + "-" + y.ToString();
for (int i = 0; i < 50; i++)
{
Thread.Sleep(5);
Application.DoEvents();
}
button3.PerformClick();
}
}
ShowMyPlane();
}
else
{
//我方轰炸后的返回结果
//0空 1伤 2死
var colindex = int.Parse(arr[1]);
var rowindex = int.Parse(arr[2]);
var result = int.Parse(arr[3]);
var isgameover = int.Parse(arr[4]);
arr2[colindex - 1, rowindex - 1] = result;
ShowEnemyPlane();
if(isgameover==1)
{
lblstatus.Text = "你赢了!";
}
}
}));
}
});
}
/// <summary>
/// 将飞机状态显示在图上
/// </summary>
private void ShowMyPlane()
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
var c = arr1[j, i];
var d = dgv.Rows[i].Cells[j];
if (c.status == 0)
{
d.Value = "";
if (c.isboom)
{
d.Style.BackColor = Color.Green;
d.Value = "空";
}
else d.Style.BackColor = Color.White;
}
else if (c.status == 1)
{
//身
d.Value = c.planeindex;
if (c.isboom)
{
d.Style.BackColor = Color.Yellow;
d.Value = "伤";
}
else d.Style.BackColor = Color.Brown;
}
else
{
//头
d.Value = c.planeindex;
if (c.isboom)
{
d.Style.BackColor = Color.Red;
d.Value = "死";
}
else d.Style.BackColor = Color.Brown;
}
}
}
private void ShowEnemyPlane()
{
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
{
var c = arr2[i, j];
if (c == -1)
{
//未被轰炸
enemydgv.Rows[j].Cells[i].Style.BackColor = Color.White;
}
else if (c == 0)
{
enemydgv.Rows[j].Cells[i].Value = "空";
enemydgv.Rows[j].Cells[i].Style.BackColor = Color.Green;
}
else if (c == 1)
{
enemydgv.Rows[j].Cells[i].Value = "伤";
enemydgv.Rows[j].Cells[i].Style.BackColor = Color.Yellow;
}
else if (c == 2)
{
enemydgv.Rows[j].Cells[i].Value = "死";
enemydgv.Rows[j].Cells[i].Style.BackColor = Color.Red;
}
}
}
public class bodypoint
{
public int x { get; set; }
public int y { get; set; }
//0 身体 1头
public int flag { get; set; }
}
private void PlacePlane(int rowidx, int colidx, int flag)
{
var lst = new List<bodypoint>();
lst.Add(new bodypoint()
{
x = colidx,
y = rowidx,
flag = 1,
});
//5
lst.Add(new bodypoint()
{
x = colidx - 2,
y = rowidx + 1,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx - 1,
y = rowidx + 1,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx,
y = rowidx + 1,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx + 1,
y = rowidx + 1,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx + 2,
y = rowidx + 1,
flag = 0,
});
//1
lst.Add(new bodypoint()
{
x = colidx,
y = rowidx + 2,
flag = 0,
});
//3
lst.Add(new bodypoint()
{
x = colidx - 1,
y = rowidx + 3,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx,
y = rowidx + 3,
flag = 0,
});
lst.Add(new bodypoint()
{
x = colidx + 1,
y = rowidx + 3,
flag = 0,
});
foreach (var v in lst)
{
if (v.flag == 1)
{
arr1[v.x - 1, v.y - 1].status = 2;
arr1[v.x - 1, v.y - 1].planeindex = flag;
}
else
{
arr1[v.x - 1, v.y - 1].status = 1;
arr1[v.x - 1, v.y - 1].planeindex = flag;
}
}
}
完整代码在此处下载: