private static SingleObject _singleobject = null; //全局唯一对象 public static SingleObject GetSingle() //提供一个静态函数用于返回一个唯一的对象 { if (_singleobject == null) { _singleobject = new SingleObject(); } return(_singleobject); }
public override void Move() { switch (Dir) { case Direction.Up: this.Y -= this.Speed; break; case Direction.Down: this.Y += this.Speed; break; case Direction.Left: this.Width -= this.Speed; break; case Direction.Right: this.Height += this.Speed; break; } if (this.X <= 0) { this.X = 0; } if (this.X >= 400) { this.X = 400; } if (this.Y <= 0) { this.Y = 0; } if (this.Y > 700) //应该销毁敌机对象 { // this.Y = 1000; //同时 当敌人飞机离开窗体的时候 我们应该销毁当前敌人飞机 SingleObject.GetSingle().RemoveGameObject(this); //this表示当前当前对象 } //对于小飞机,我可以允许它带有一点晃动的特性 if (this.EnemyType == 0) { //MoveReturn(); if (this.X >= 0 && this.X <= 150) { //表示当前的小飞机在左边的范围内 //增加当前飞机的X值 this.X += r.Next(0, 30); this.X -= r.Next(0, 30); //加这一句是为了产生晃动 } else { this.X -= r.Next(0, 20); this.X += r.Next(0, 30); } } }
/// <summary> /// 玩家飞机跟随移动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseMove(object sender, MouseEventArgs e) { //当鼠标在移动的时候,会调用此函数,让玩家飞机跟随鼠标移动,就是将鼠标的 //坐标值赋值给飞机的坐标 // SingleObject.GetSingle().Hero.X = e.X; // SingleObject.GetSingle().Hero.Y = e.Y; 这是在飞机类外部实现功能,虽逻辑是正确的 //但架构不好,应该把在玩家飞机类中实现move方法,再在这里调用 SingleObject.GetSingle().Hero.MouseMove(e); }
private void timerBG_Tick(object sender, EventArgs e) { //每50毫秒就重绘制 this.Invalidate(); int PlaneNum = SingleObject.GetSingle().listPlaneEnemy.Count; if (PlaneNum < 3) { InitialPlaneEnemy(); } }
/// <summary> /// 不应该每次都把打飞机(boss)初始化出来,有一定的几率 /// </summary> private void InitialPlaneEnemy() { for (int i = 0; i < 3; i++) { SingleObject.GetSingle().AddGameObject(new PlaneEnemy(r.Next(0, this.Width), -200, 5, 8, r.Next(0, 2))); } //几率的实现:通过随机生成实现 if (r.Next(0, 100) > 80) { SingleObject.GetSingle().AddGameObject(new PlaneEnemy(r.Next(0, this.Width), -200, 5, 8, 2)); } }
/// <summary> /// 初始化游戏 /// </summary> /// public void InitialGame() { //首先初始化背景 SingleObject.GetSingle().AddGameObject(new BackGround(0, -850, 5)); //初始化玩家飞机 SingleObject.GetSingle().AddGameObject(new PlaneHero(200, 200, 20, 5, GameObject.Direction.Up)); ////初始化敌人飞机 //SingleObject.GetSingle().AddGameObject(new PlaneEnemy(20, 186, 5, 3, 0)); //SingleObject.GetSingle().AddGameObject(new PlaneEnemy(100, 140, 5, 3, 1)); //SingleObject.GetSingle().AddGameObject(new PlaneEnemy(280, 170, 5, 3, 2)); }
private void Form1_Paint(object sender, PaintEventArgs e) { //窗体被绘制的时候,会执行当前事件(绘制背景) //SingleObject.GetSingle().BG.Draw(e.Graphics);这一句也可实现绘制背景,但是窗体直接操作类函数不合适 SingleObject.GetSingle().DrawGameobject(e.Graphics);//我们通过单例来实现与窗体的交互 }
/// <summary> /// 不断生成飞机 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void JudgeFJ_Tick(object sender, EventArgs e) { int PlaneNum = SingleObject.GetSingle().listPlaneEnemy.Count; }
/// <summary> /// 玩家飞机发射子弹 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_MouseDown(object sender, MouseEventArgs e) { //按下左键 SingleObject.GetSingle().Hero.Fire(); }
public void Fire() { //初始化子弹对象 SingleObject.GetSingle().AddGameObject(new ZiDanHero(this, 10, 1)); }