基于C#弹幕类射击游戏的实现——(四)玩家的战机

2017-09-19  本文已影响0人  UnSkyToo

有了子弹,子弹想有个飞机能发射子弹了。好,现在我们就实现玩家的战机

/// <summary>  
    /// Description of GamePlayer.  
    /// </summary>  
    public class GamePlayer : GameObject  
    {  
        private GameScene mScene = null;  
          
        private Vector2 InitPosition;  
        public Vector2 Speed;  
          
        public bool IsLive;  
          
        private float ShootDelay = 0;  
        private float ShootDelayTime = 0;  
          
        private int PlayerSpriteIndex;  
        private int Life = 0; // 生命值  
          
        private float ReliveTime = 0; // 重生时间  
        private float GodTime = 0; // 无敌时间  
}

这是玩家类的定义,当然也是继承自GameObject的。
我们可以看到,玩家类定义的东西比GameBullet明显多了一点。但大体上差不多的。
然后是具体的实例化和一些基本函数

public GamePlayer(GameScene scene, Vector2 position, Vector2 speed)  
        {  
            this.mScene = scene;  
              
            this.InitPosition = position;  
            this.Position = position;  
            this.Speed = speed;  
              
            this.BoxCollider = new Box((int)position.X, (int)position.Y, 8, 8);  
              
            this.IsLive = true;  
            this.Life = 3;  
              
            this.ShootDelay = 0.10f;  
            this.PlayerSpriteIndex = 0;  
        }  
          
        public bool Collide(Box box, int decLife)  
        {  
            if ( IsLive == false || GodTime > 0 )  
            {  
                return false;  
            }  
              
            if ( BoxCollider.Collide(box) == true )  
            {  
                DecLife(decLife);  
                return true;  
            }  
              
            return false;  
        }  
          
        public void DecLife(int decLife)  
        {  
            if ( GodTime > 0 )  
            {  
                return;  
            }  
              
            Life -= decLife;  
            GameBombManager.AddBomb(Position, false, 0.5f);  
                  
            if ( Life <= 0 && Config.IsDebug == false )  
            {  
                IsLive = false;  
            }  
            else  
            {  
                IsLive = false;  
                ReliveTime = 1.5f;  
            }  
        }  
          
        public void Init()  
        {  
            Position = InitPosition;  
            IsLive = true;  
            ShootDelayTime = 0;  
            PlayerSpriteIndex = 0;  
              
            GodTime = 3.0f;  
              
            BoxCollider.X = (int)Position.X;  
            BoxCollider.Y = (int)Position.Y + 3;  
        }

DecLife负责扣除玩家生命值,然后添加爆炸效果,然后处理无敌啊,重生啊之类的时间
接下来是绘制部分的代码, 很简单,就是根据玩家位置,把玩家画到屏幕上就行

public override void Render(Graphics g)  
        {  
            if ( IsLive == false )  
            {  
                return;  
            }  
              
            g.DrawImage(Data.PlayerSource,  
                        new Rectangle((int)Position.X - Config.HalfPlayerWidth, (int)Position.Y - Config.HalfPlayerHeight, Config.PlayerWidth, Config.PlayerHeight),  
                        new Rectangle(PlayerSpriteIndex * Config.PlayerWidth, 0, Config.PlayerWidth, Config.PlayerHeight), GraphicsUnit.Pixel);  
              
            g.DrawImage(Data.PlayerPointSource,  
                        new Rectangle((int)Position.X - 5, (int)Position.Y - 5, 10, 10),  
                        new Rectangle(0, 0, 10, 10),  
                        GraphicsUnit.Pixel);  
            g.DrawImage(Data.HpSource,  
                        new Rectangle(5, 25, 24, 24),  
                        new Rectangle(0, 0, 24, 24),  
                        GraphicsUnit.Pixel);  
              
            g.DrawString(Life.ToString(), Data.NormalFont, Brushes.White, 30, 27);  
              
            if ( Config.IsDebug && Config.IsShowBox )  
            {  
                g.DrawRectangle(Pens.Green, BoxCollider.ToRectangle());  
            }  
        }

然后是最重要的Update了。

public override void Update(float elapsedTime)  
        {  
            if ( IsLive == false )  
            {  
                if ( ReliveTime > 0 )  
                {  
                    ReliveTime -= elapsedTime;  
                      
                    if ( ReliveTime <= 0 )  
                    {  
                        Init();  
                    }  
                }  
                  
                return;  
            }  
              
            if ( GodTime > 0 )  
            {  
                GodTime -= elapsedTime;  
            }  
              
            if ( mScene.EnemyManager.Collide(BoxCollider, 10) == true )  
            {  
                DecLife(1);  
                return;  
            }  
              
            if ( mScene.BossManager.Collide(BoxCollider, 10) == true )  
            {  
                DecLife(1);  
                return;  
            }  
              
            if ( ShootDelayTime > 0 )  
            {  
                ShootDelayTime -= elapsedTime;  
            }  
              
            this.PlayerSpriteIndex = 0;  
            if ( Input.IsKeyDown(Keys.Left) )  
            {  
                Position.X -= (Speed.X * elapsedTime);  
                this.PlayerSpriteIndex = 1;  
            }  
            if ( Input.IsKeyDown(Keys.Right) )  
            {  
                Position.X += (Speed.X * elapsedTime);  
                this.PlayerSpriteIndex = 2;  
            }  
            if ( Input.IsKeyDown(Keys.Up) )  
            {  
                Position.Y -= (Speed.Y * elapsedTime);  
            }  
            if ( Input.IsKeyDown(Keys.Down) )  
            {  
                Position.Y += (Speed.Y * elapsedTime);  
            }  
              
            if ( Input.IsKeyDown(Keys.Z) || Input.IsKeyDown(Keys.Space) )  
            {  
                if ( ShootDelayTime <= 0 )  
                {  
                    Shoot();  
                }  
            }  
              
            if ( Position.X < Config.HalfPlayerWidth )  
            {  
                Position.X = Config.HalfPlayerWidth;  
            }  
            if ( Position.X >= Config.ScreenWidth - Config.HalfPlayerWidth )  
            {  
                Position.X = Config.ScreenWidth - Config.HalfPlayerWidth;  
            }  
            if ( Position.Y < Config.HalfPlayerHeight )  
            {  
                Position.Y = Config.HalfPlayerHeight;  
            }  
            if ( Position.Y >= Config.ScreenHeight - Config.HalfPlayerHeight )  
            {  
                Position.Y = Config.ScreenHeight - Config.HalfPlayerHeight;  
            }  
              
            BoxCollider.X = (int)Position.X;  
            BoxCollider.Y = (int)Position.Y;  
        }

可以看到,大概分为几部分
第一部分是处理重生了和无敌。。检测到自己IsLive为false后,就开始倒计时。
第二部分是处理碰撞,其实是委托给了EnemyManager和BossManager进行处理
第三部分是处理按键信息,唯一要注意的时,射击有CD的..
最后是射击的部分

private void Shoot()  
        {  
            ShootDelayTime = ShootDelay;  
            GameScene.MainScene.AddBullet(new GameBullet(true, new Vector2(Position.X, Position.Y - Data.BulletsSize[0].Height / 2), new Vector2(0, -400), new Vector2(0, 0), 0, 1, 0));  
        } 

也就是用了我们上一章的例子,创建子弹,并且赋值一些数据而已
好,我们现在有一架飞机,并且可以在屏幕上移动和射击了。。
等等,有人会问,我现在什么都看不到,而且代码怎么编译?
GameScene是什么?EnemyManager又是什么?别急,慢慢来,下一章就将GameScene

上一篇 下一篇

猜你喜欢

热点阅读