//初始化对象们 private void InitialGame() { //初始化背景对象 并且赋值 SingleObject.GetSingle().AddGameObject(new Background(0, -850, 20)); //初始化玩家对象 SingleObject.GetSingle().AddGameObject(new HeroPlane(200, 200, 20, 1, Direction.Up)); }
//根据属性画图片 public override void Draw(Graphics g) { switch (this.Type) { case 0: for (int i = 0; i < img1.Length; i++) { g.DrawImage(img1[i], this.X, this.Y); } break; case 1: for (int i = 0; i < img2.Length; i++) { g.DrawImage(img2[i], this.X, this.Y); } break; case 2: for (int i = 0; i < img3.Length; i++) { g.DrawImage(img3[i], this.X, this.Y); } break; } //画完移除 SingleObject.GetSingle().RemoveGameObject(this); }
//移除对象 public override void MoveToBorder() { if (this.Y <= 0 || this.Y > 812) { SingleObject.GetSingle().RemoveGameObject(this); } }
//画玩家图片 public override void Draw(Graphics g) { for (int i = 0; i < img1.Length; i++) { g.DrawImage(img1[i], this.X, this.Y); } SingleObject.GetSingle().RemoveGameObject(this); }
//3.声明一个静态函数返回全局唯一的单例对象 public static SingleObject GetSingle() { if (_single == null) { _single = new SingleObject(); } return(_single); }
//敌人死亡 public override void IsOver() { if (this.Lief == 0) { //爆炸效果 SingleObject.GetSingle().AddGameObject(new EnemyBoom(this.X, this.Y, this.EnemType)); //移除敌人飞机 SingleObject.GetSingle().RemoveGameObject(this); } }
//不停的让窗体重绘 private void timer1_Tick(object sender, EventArgs e) { //不停的让窗体重绘 记得开双缓存在Doubkebuffered中 this.Invalidate(); //不停的判断当前飞机的数量 if (SingleObject.GetSingle().listEnemyPlan.Count <= 1) { IntiaEnemyPlane(); } //碰撞检测 SingleObject.GetSingle().PZJC(); }
//初始化敌人飞机对象 private void IntiaEnemyPlane() { for (int i = 0; i < 4; i++) { SingleObject.GetSingle().AddGameObject(new EnemyPlane(r.Next(0, this.Width), -200, r.Next(0, 2))); } //让打飞机很少出现 if (r.Next(1, 101) > 60) { SingleObject.GetSingle().AddGameObject(new EnemyPlane(r.Next(0, this.Width), -200, r.Next(3))); } }
private void timer2_Tick(object sender, EventArgs e) { if (isStar) { playTime++; //如果开始游戏就开始计时间 if (playTime == 20) { //到达时间,停止游戏 isStar = false; //将结果发送给服务器 转为字符数组才能发送 byte[] buffer = Encoding.Default.GetBytes(SingleObject.GetSingle().Socore.ToString()); socket.Send(buffer); //绘制到paint isPaintResult = true; } } }
//在paint中绘制各种游戏对象 private void Form1_Paint(object sender, PaintEventArgs e) { //让界面先运行图画 SingleObject.GetSingle().BG.Draw(e.Graphics); if (isStar) { SingleObject.GetSingle().DrawGameObject(e.Graphics); } //绘制游戏的分数 string s = SingleObject.GetSingle().Socore.ToString(); e.Graphics.DrawString(s, new Font("微软雅黑", 20, FontStyle.Bold), Brushes.Red, new Point(0, 0)); if (isPaintResult) { //绘制排名结果 e.Graphics.DrawString(result, new Font("微软雅黑", 20, FontStyle.Bold), Brushes.Red, new Point(0, 200)); } }
//敌人飞机移动到边框 public override void MoveToBorder() { if (this.Y >= 812) { //把敌人飞机销毁 SingleObject.GetSingle().RemoveGameObject(this); } //写一个让小飞机到一定位置进行调位 if (this.EnemType == 0 && this.Y >= 200) { if (this.X >= 0 && this.X <= 240) { this.X += r.Next(0, 200); } else { this.X -= r.Next(0, 200); } } }
public override void IsOver() { //玩家死亡 SingleObject.GetSingle().AddGameObject(new HeroBoom(this.X, this.Y)); }
//玩家发射子弹 public override void Fire() { SingleObject.GetSingle().AddGameObject(new HeroZiDan(this, 1, 20)); }
//敌人发射子弹 public override void Fire() { SingleObject.GetSingle().AddGameObject(new EnemyZiDan(this, this.EnemType)); }
//把鼠标移动赋值给玩家飞机的坐标 private void Form1_MouseMove(object sender, MouseEventArgs e) { //调用函数 SingleObject.GetSingle().HP.MoveWithMouse(e); }
//鼠标双击击发射子弹 private void Form1_MouseClick(object sender, MouseEventArgs e) { SingleObject.GetSingle().HP.MouseDownLeft(e); }