zoukankan      html  css  js  c++  java
  • 飞机大战简单版

    看完飞机大战的视频教程,自己写了一个简单版的飞机大战(图片自己去抠一下)

    • 实现功能
      • 背景移动
      • 我方飞机跟随鼠标移动
      • 我方飞机发射子弹
      • 敌人飞机移动 判断敌人飞机位置 敌我飞机的击中情况 敌人飞机与子弹的碰撞
    •   主要的类
      • backGround 背景类:实现背景移动
      • EnemyPlane 敌人飞机类
      • HwaWeiPlane 我方飞机
      • HuaWei 我方飞机子弹

    页面样子

    backGround 类

    class BackGround
        {
    
            #region 图片导入 背景属性 构造函数
            Image img = Resources.background;
            private int X { get; set; }
            private int Y { get; set; }
            private int Speed { get; set; }
            public BackGround(int x, int y, int speed)
            {
                this.X = x;
                this.Y = y;
                this.Speed = speed;
            } 
            #endregion
    
            #region 绘制游戏背景
            public void Draw(Graphics g)
            {
                g.DrawImage(img, this.X, this.Y);
            } 
            #endregion
    
            #region 背景移动函数
            public void Move()
            {
                this.Y += Speed;
            } 
            #endregion
            
            #region 判断背景左上角位置
            public void MoveToBorder()
            {
                if (this.Y >= 0)
                {
                    this.Y = -450;
                } 
            }
            #endregion     
    
        }

    EnemyPlane类

    class EnemyPlane
        {
            #region 敌人飞机属性 和 图片导入
            int X { get; set; }
            int Y { get; set; }
            int Speed { get; set; }
            Image img = Resources.pingguo; 
            #endregion
    
            #region 敌人飞机构造函数
            public EnemyPlane(int x, int y, int speed)
            {
                this.Speed = speed;
                this.X = x;
                this.Y = y;
            } 
            #endregion
    
            #region 绘制敌人飞机
            public void Draw(Graphics g)
            {
                g.DrawImage(img, this.X, this.Y, img.Width / 2, img.Height / 2);
            } 
            #endregion
    
            #region 敌人飞机移动函数
            public void Move()
            {
                this.Y += Speed;
            } 
            #endregion
    
            #region 敌人飞机边缘判断
            public void MoveToBorder()
            {
                if (this.Y >= 450)
                {
                    SingleObject.GetSingle().RemoveObject(this);
                }
            }
            
            #endregion
    
            #region 获得当前敌人飞机矩形
            public Rectangle GetRectangle()
            {
                return new Rectangle(this.X, this.Y, img.Width / 2, img.Height / 2);
            } 
            #endregion
        }

    HuaWeiPlane 类

     class HuaweiPlane
        {
            #region 图片导入 属性 构造函数
            Image img = Resources.huawei;
            public int X { get; set; }
            public int Y { get; set; }
            public int  Life { get; set; }
            public HuaweiPlane(int x, int y, int life)
            {
                this.X = x;
                this.Y = y;
                this.Life = life;
            } 
            #endregion
    
            #region 绘制玩家飞机
            public void Draw(Graphics g)
            {
                g.DrawImage(img, this.X, this.Y, img.Height / 2, img.Width / 2);
            } 
            #endregion
    
            #region 玩家飞机边缘检测
            public void MoveToBorder()
            {
                if (this.X >= 300)
                {
                    this.X = 300;
                }
                if (this.X <= 0)
                {
                    this.X = 0;
                }
                if (this.Y >= 450)
                {
                    this.Y = 450;
                }
                if (this.Y <= 0)
                {
                    this.Y = 0;
                }
            } 
            #endregion
    
            #region 获取鼠标坐标
            public void GetMouse(MouseEventArgs e)
            {
                this.X = e.X-25;
                this.Y = e.Y-25;
            } 
            #endregion
    
        }

    HuaWeiZidan 类

    class HuaWeiZiDan
        {
    
            #region 导入图片 子弹属性 构造函数
            Image img = Resources.bullet;
            int X { get; set; }
            int Y { get; set; }
            int Speed { get; set; }
            public HuaWeiZiDan(int x, int y, int speed)
            {
                this.X = x;
                this.Y = y;
                this.Speed = speed;
            } 
            #endregion
    
            #region 绘制子弹
            public void Draw(Graphics g)
            {
                g.DrawImage(img, this.X, this.Y);
            } 
            #endregion
    
            #region 子弹移动函数
            public void Move()
            {
                this.Y -= this.Speed;
            } 
            #endregion
    
            #region 边缘检测函数
            public void MoveToBoeder()
            {
                if (this.Y <= 0)
                {
                    SingleObject.GetSingle().RemoveObject(this);
                }
            } 
            #endregion
    
            #region 获得当前子弹的矩形
            public Rectangle GetRectangle()
            {
                return new Rectangle(this.X, this.Y, img.Width, img.Height);
            } 
            #endregion
    
    
    
        }

    SingleObject 类(单例类)

    class SingleObject
        {
            #region 单例开发模式
            //1.构造函数私有化
            private SingleObject() 
            { }
            //2.声明全局唯一的单例对象
            private static SingleObject _single = null;
            //3.声明一份静态函数返回全局唯一的对象
            public static SingleObject GetSingle()
            {
                if (_single == null)
                {
                    _single = new SingleObject();
                }
                return _single;
            }
            #endregion
    
            #region 存储游戏对象
            //在单例类中存储背景对象
            private BackGround BG { get; set; }
            //在单例类中存储玩家飞机
            public HuaweiPlane HP { get; set; }
            //在单例类中存储敌人飞机
            public  List<EnemyPlane> listEnemyPlane = new List<EnemyPlane>();
            //在单例类中存储玩家子弹
            public List<HuaWeiZiDan> listHuaweiZidan = new List<HuaWeiZiDan>();
            #endregion
    
            #region 添加游戏对象
            public void AddObject(Object Ob)
            {
                if (Ob is BackGround)
                {
                    this.BG =Ob as BackGround;
                }
                if (Ob is HuaweiPlane)
                {
                    this.HP = Ob as HuaweiPlane;
                }
                if (Ob is EnemyPlane)
                {
                    this.listEnemyPlane.Add(Ob as EnemyPlane);
                }
                if (Ob is HuaWeiZiDan)
                {
                    this.listHuaweiZidan.Add(Ob as HuaWeiZiDan);
                }
            }
            #endregion
    
            #region 移除游戏对象
            public void RemoveObject(Object Ob)
            {
                if (Ob is EnemyPlane)
                {
                    listEnemyPlane.Remove(Ob as EnemyPlane);
                }
                if (Ob is HuaWeiZiDan)
                {
                    listHuaweiZidan.Remove(Ob as HuaWeiZiDan);
                }
            }
            #endregion
    
            #region 将对象绘制到窗体
            public void DrawObject(Graphics g)
            {
                this.BG.Draw(g);
                this.BG.Move();
                this.BG.MoveToBorder();
                this.HP.Draw(g);
                this.HP.MoveToBorder();
                for (int i = 0; i < listEnemyPlane.Count ; i++)
                {
                    listEnemyPlane[i].Draw(g);
                    listEnemyPlane[i].Move();
                    listEnemyPlane[i].MoveToBorder();
                }
                for (int i = 0; i < listHuaweiZidan.Count ; i++)
                {
                    listHuaweiZidan[i].Draw(g);
                    listHuaweiZidan[i].Move();
                    listHuaweiZidan[i].MoveToBoeder();
                }
                g.DrawString("你打了"+this .Code.ToString()+"个苹果",new Font("微软雅黑",15),Brushes.Blue,10,10);
    
    
            }
            #endregion
    
            #region 碰撞检测函数
            public void  PZJC()
            {
              
                for (int i = 0; i < listEnemyPlane.Count; i++)
                {
                    for (int j = 0; j < listHuaweiZidan.Count; j++)
                    {
                        if (listEnemyPlane[i].GetRectangle().IntersectsWith(listHuaweiZidan[j].GetRectangle()))
                        {
                            SingleObject.GetSingle().RemoveObject(listHuaweiZidan[j]);
                            SingleObject.GetSingle().RemoveObject(listEnemyPlane[i]);
                            this.Code=this .Code+1;
                            break;
                        }
                    }
                }
         
            } 
            #endregion
    
            private int Code { get; set; }
    
        }

    运行结果

    代码简单 学习C# 的同学可以敲起来!!!

  • 相关阅读:
    【CF1172E】Nauuo and ODT(Link-Cut Tree)
    【UOJ#242】【UR#16】破坏蛋糕(计算几何)
    【CF528E】Triangles 3000(计算几何)
    【CF1053E】Euler tour
    【CodeChef】Find a special connected block
    【CF933E】A Preponderant Reunion(动态规划)
    【CF704D】Captain America(上下界网络流)
    【BZOJ4823】[CQOI2017]老C的方块(网络流)
    【LOJ#2162】【POI2011】Garbage(欧拉回路)
    【CF241E】Flights(差分约束)
  • 原文地址:https://www.cnblogs.com/niaofei123/p/12920142.html
Copyright © 2011-2022 走看看