看完飞机大战的视频教程,自己写了一个简单版的飞机大战(图片自己去抠一下)
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 }
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 }
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 }
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 }
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# 的同学可以敲起来!!!
原文:https://www.cnblogs.com/niaofei123/p/12920142.html