예제 #1
0
        // 重写Draw方法
        public override void Draw(Graphics g)
        {
            //根据当前飞机的类型绘制爆炸效果图片
            switch (this.Type)
            {
            case 0:
                for (int i = 0; i < imgsSmall.Length; i++)
                {
                    g.DrawImage(imgsSmall[i], this.X, this.Y);
                }
                break;

            case 1:
                for (int i = 0; i < imgsMiddle.Length; i++)
                {
                    g.DrawImage(imgsMiddle[i], this.X, this.Y);
                }
                break;

            case 2:
                for (int i = 0; i < imgsLarge.Length; i++)
                {
                    g.DrawImage(imgsLarge[i], this.X, this.Y);
                }
                break;
            }

            //爆炸图片播放完成后销毁该爆炸效果
            SingleObject.GetInstance().RemoveGameObject(this);
        }
예제 #2
0
        // 重写IsOver方法:判断敌人是否阵亡
        public override void IsOver()
        {
            if (this.Life <= 0)
            {
                SingleObject singleObject = SingleObject.GetInstance();
                // 1.首先移除自身
                singleObject.RemoveGameObject(this);
                // 2.播放敌人爆炸图片
                singleObject.AddGameObject(new BoomEnemy(this.X, this.Y, this.EnemyType));
                // 3.播放敌人爆炸声音
                SoundPlayer sp = new SoundPlayer(Resources.enemy0_down1);
                sp.Play();
                // 4.根据不同的敌人类型添加不同的分数
                switch (this.EnemyType)
                {
                case 0:
                    singleObject.Score += 100;
                    break;

                case 1:
                    singleObject.Score += 200;
                    break;

                case 2:
                    singleObject.Score += 300;
                    break;
                }
            }
        }
예제 #3
0
        // 重写Move方法
        public override void Move()
        {
            // 根据指定的移动方向进行移动
            switch (Dir)
            {
            case Direction.Up:
                this.Y -= this.Speed;
                break;

            case Direction.Down:
                this.Y += this.Speed;
                break;
            }

            // 子弹发射之后控制子弹纵坐标
            if (this.Y <= 0)
            {
                this.Y = -100;
                // 在游戏中移除子弹对象
                SingleObject.GetInstance().RemoveGameObject(this);
            }
            if (this.Y >= 670)
            {
                this.Y = 870;
                // 在游戏中移除子弹对象
                SingleObject.GetInstance().RemoveGameObject(this);
            }
        }
예제 #4
0
 private void MainForm_MouseMove(object sender, MouseEventArgs e)
 {
     if (isStarted)
     {
         // 让飞机跟着鼠标的移动而移动
         SingleObject.GetInstance().Player.MouseMove(e);
     }
 }
        public static SingleObject GetInstance()
        {
            if (singleInstance == null)
            {
                singleInstance = new SingleObject();
            }

            return(singleInstance);
        }
예제 #6
0
 // 重写Draw方法:绘制爆炸效果图片
 public override void Draw(Graphics g)
 {
     for (int i = 0; i < imgsBoom.Length; i++)
     {
         g.DrawImage(imgsBoom[i], this.X, this.Y);
     }
     //绘制完成后将爆炸的图片移除
     SingleObject.GetInstance().RemoveGameObject(this);
 }
예제 #7
0
        // 重写IsOver方法:判断玩家是否阵亡
        public override void IsOver()
        {
            SingleObject singleObject = SingleObject.GetInstance();

            singleObject.AddGameObject(new BoomPlayer(this.X, this.Y));
            if (singleObject.Score >= 100)
            {
                singleObject.Score -= 50;
            }
        }
예제 #8
0
 private void MainForm_MouseDown(object sender, MouseEventArgs e)
 {
     // 首先判断是否按下了鼠标左键
     if (isStarted && e.Button == MouseButtons.Left)
     {
         // 调用玩家飞机发射子弹的方法
         SingleObject.GetInstance().Player.Fire();
         // 播放子弹发射声音
         PlayFireMusic();
     }
 }
예제 #9
0
        private void InitializeEnemyPlanes(SingleObject singleObject)
        {
            for (int i = 0; i < 4; i++)
            {
                singleObject.AddGameObject(new PlaneEnemy(random.Next(0, this.Width), -400, random.Next(0, 2)));
            }

            if (random.Next(0, 100) > 80)
            {
                // 20%的机会出现大飞机
                singleObject.AddGameObject(new PlaneEnemy(random.Next(0, this.Width - 80), -400, 2));
            }
        }
예제 #10
0
        // 重写Draw方法
        public override void Draw(Graphics g)
        {
            // 标题图片不停向下移动
            this.Y += Speed;
            if (this.Y >= 670)
            {
                this.Y = 800;
                // 移除游戏标题对象
                SingleObject.GetInstance().RemoveGameObject(this);
            }

            // 坐标改变后绘制图片到屏幕
            g.DrawImage(imgTitle, this.X, this.Y);
        }
예제 #11
0
        // 初始化游戏对象
        private void InitializeGameObjects()
        {
            SingleObject singleObject = SingleObject.GetInstance();

            // 1.1 初始化游戏背景
            singleObject.AddGameObject(new GameBackground(0, -850, 5));
            // 1.2 初始化游戏标题
            singleObject.AddGameObject(new GameTitle(20, 50, 10));
            // 1.3 初始化玩家飞机
            singleObject.AddGameObject(new PlanePlayer(this.Width / 2 - 50, this.Height * 3 / 4, 5, 3, Direction.Up));
            // 2.初始化Timer组件
            this.timerBackground.Interval = 50;
            this.timerBackground.Enabled  = true;
            this.timerPlayTime.Interval   = 1000;
            this.timerPlayTime.Enabled    = true;
            // 3.初始化敌人飞机
            //InitializeEnemyPlanes(singleObject);
        }
예제 #12
0
 private void timerBackground_Tick(object sender, EventArgs e)
 {
     // 1.每50毫秒让窗体屏幕发生重绘事件
     this.Invalidate();
     if (isStarted)
     {
         // 2.每50毫秒判断敌人飞机是否已经移动出屏幕
         SingleObject singleObject = SingleObject.GetInstance();
         int          count        = singleObject.EnemyList.Count;
         if (count <= 1)
         {
             // 再次初始化四架敌人飞机
             InitializeEnemyPlanes(singleObject);
         }
         // 3.每50毫秒进行一次碰撞检测
         singleObject.CollisionDetect();
     }
 }
예제 #13
0
 // 初始化游戏时间Timer组件的Tick事件
 private void timerPlayTime_Tick(object sender, EventArgs e)
 {
     if (isStarted)
     {
         // 1.开始计时
         playTime++;
         // 2.结束条件:这里只允许游戏进行20s
         if (playTime == 20)
         {
             // 结束游戏
             isStarted = false;
             SoundPlayer sp = new SoundPlayer(Resources.game_over1);
             sp.Play();
             // 将玩家分数传给服务器端
             byte[] bytes = Encoding.UTF8.GetBytes(SingleObject.GetInstance().Score.ToString());
             if (clientSocket != null && clientSocket.Connected)
             {
                 clientSocket.Send(bytes);
             }
             // 清空时间
             playTime = 0;
         }
     }
 }
예제 #14
0
        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            SingleObject singleObject = SingleObject.GetInstance();

            if (!isStarted)
            {
                // 1.通过统一绘制入口绘制游戏对象
                singleObject.DrawFirstBackground(e.Graphics);
                // 2.绘制游戏结果排名与分数
                if (isShowResult)
                {
                    isShowResult = false;
                    MessageBox.Show(gameResult, "游戏结束", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                // 1.通过统一绘制入口绘制游戏对象
                singleObject.DrawGameObjects(e.Graphics);
                // 2.获取玩家即时分数并绘制到屏幕中
                string score = singleObject.Score.ToString();
                e.Graphics.DrawString(score, new Font("微软雅黑", 20, FontStyle.Bold), Brushes.Red, new Point(10, 20));
            }
        }
예제 #15
0
 // 开炮发射子弹
 public void Fire()
 {
     SingleObject.GetInstance().AddGameObject(new BulletPlayer(this, 15, 1));
 }
예제 #16
0
 // 敌人飞机发射子弹
 public void Fire()
 {
     SingleObject.GetInstance().AddGameObject(new BulletEnemy(this, 20, 1));
 }
예제 #17
0
        // 重写Move方法
        public override void Move()
        {
            //根据游戏对象的方向进行移动
            switch (this.Dir)
            {
            case Direction.Up:
                this.Y -= this.Speed;
                break;

            case Direction.Down:
                this.Y += this.Speed;
                break;

            case Direction.Left:
                this.X -= this.Speed;
                break;

            case Direction.Right:
                this.X += this.Speed;
                break;
            }

            if (this.X <= 0)
            {
                this.X = 0;
            }
            if (this.X >= 380)
            {
                this.X = 380;
            }
            if (this.Y <= 0)
            {
                this.Y = 0;
            }
            if (this.Y >= 670)
            {
                this.Y = 870;
                // 移除敌人飞机对象
                SingleObject.GetInstance().RemoveGameObject(this);
            }

            // 当敌人飞机为小飞机时并且移动到某个坐标时不停改变其的横坐标
            if (this.EnemyType == 0 && this.Y >= 250)
            {
                if (X >= 0 && this.X <= 220)
                {
                    this.X += random.Next(0, 50);
                }
                else
                {
                    this.X -= random.Next(0, 50);
                }
            }
            else // 如果不是小飞机则加快移动速度
            {
                this.Speed += 1;
            }

            // 敌人飞机发射子弹
            if (random.Next(0, 100) > 90)
            {
                Fire();
            }
        }