飛機大戰簡單版
- 2020 年 5 月 19 日
- 筆記
看完飛機大戰的影片教程,自己寫了一個簡單版的飛機大戰(圖片自己去摳一下)
- 實現功能
- 背景移動
- 我方飛機跟隨滑鼠移動
- 我方飛機發射子彈
- 敵人飛機移動 判斷敵人飛機位置 敵我飛機的擊中情況 敵人飛機與子彈的碰撞
- 主要的類
- 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# 的同學可以敲起來!!!