飞机游戏,飞机大战,c#,源代码

打飞机游戏

最近接到一个小小的活,做个打飞机游戏,通过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;
                }
            }

        }

完整代码在此处下载:

https://download.csdn.net/download/LinuxCard/87218021

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值