基于C#弹幕类射击游戏的实现——(五)主场景

这里的的GameScene就是一个用于管理所有对象的类。整个游戏只有一个Scene
(其实还有MenuScene,LoadScene之类的,但这里没有做,在下一个游戏《超级玛丽》里会有详细的介绍)
先来看看申明部分

public static GameScene MainScene = null;  
  
        private Bitmap mSurface;  
        private Graphics mG;  
          
        public Bitmap Surface  
        {  
            get  
            {  
                return mSurface;  
            }  
        }  
          
        private int mFPS;  
        public int FPS  
        {  
            get  
            {  
                return mFPS;  
            }  
        }  
          
        public event RenderOverHandler RenderOver = null;  
          
        private Timer timer;  
        private Stopwatch watch;  
        private int loopCount;  
        private int timerCount;  
        private int RunTime = 0;  
          
        public GameBulletManager BulletManager;  
        private GameBarrage mBarrage;  
          
        public GameEnemyManager EnemyManager;  
        public GameBossManager BossManager;  
          
        public GamePlayer Player1;  
        public Vector2 Player1Position  
        {  
            get  
            {  
                return Player1.Position;  
            }  
        }  
          
        private int BackgroundIndex;  
          
        private List[] mMapInfo;

东西挺多的,但其实只是各个游戏部件的整合而已。比如GameBulleManager、GameEnemyManager之类的,这些东西之后会介绍
然后是初始化,稍微多了一点点

public GameScene()  
        {  
            MainScene = this;  
  
            mSurface = new Bitmap(Config.ScreenWidth, Config.ScreenHeight);  
            mG = Graphics.FromImage(mSurface);  
              
            BulletManager = new GameBulletManager(this);  
            mBarrage = new GameBarrage(this);  
              
            EnemyManager = new GameEnemyManager();  
            BossManager = new GameBossManager();  
              
            Player1 = new GamePlayer(this, new Vector2(200, 580), new Vector2(250, 250));  
              
            this.mMapInfo = new List[Config.OneMapTime]; // 3分钟  
            for ( int i = 0; i < Config.OneMapTime; i++ )  
            {  
                this.mMapInfo[i] = new List();  
            }  
              
            BackgroundIndex = 0;  
              
            watch = new Stopwatch();  
            watch.Start();  
              
            timer = new Timer();  
            timer.Interval = 33;  
            timer.Tick += Loop;  
            timer.Start();  
              
            loopCount = 0;  
            timerCount = 0;  
            mFPS = 30;  
              
            GameInit();  
        }  
          
        public void GameInit()  
        {  
            //AddEnemy(new Enemy6(new Vector2(200, 0)));  
            AddBoss(new Boss1(mBarrage, new Vector2(200, 150)));  
              
            RunTime = 0;  
              
            //LoadMapInfo(Application.StartupPath + @"\MapInfo.txt");  
        } 

调用各个类的构造函数进行初始化,唯一注意的是开启了一个Timer控件来当做游戏的主循环。。。因为C# WINFORM很蛋疼,基于事件机制。。
接下来跳过载入地图信息。。。因为还没用到。
先来看看添加对象

public void AddBullet(GameBullet bullet)  
        {  
            BulletManager.AddBullet(bullet);  
        }  
          
        public void AddEnemy(GameEnemy enemy)  
        {  
            EnemyManager.AddEnemy(enemy);  
        }  
          
        public void AddBoss(GameBoss boss)  
        {  
            BossManager.AddBoss(boss);  
        }

其实都是调用的类本身的函数。。。。。
然后看看蛋疼的主循环

private void Loop(object sender, EventArgs e)  
        {  
            Update((float)watch.Elapsed.TotalMilliseconds / 1000.0f);  
            Render();  
              
            if ( RenderOver != null )  
            {  
                RenderOver(this);  
            }  
              
            loopCount++;  
              
            watch.Stop();  
            timerCount += (int)watch.Elapsed.TotalMilliseconds;  
              
            // 一秒  
            if ( timerCount >= 1000 )  
            {  
                RunTime++;  
                // 处理地图信息  
                DealMapInfo(RunTime - 1);  
                  
                // 处理FPS显示  
                timerCount -= 1000;  
                mFPS = loopCount;  
                loopCount = 0;  
                  
                if ( mFPS < 30 )  
                {  
                    if ( timer.Interval > 20 )  
                    {  
                        timer.Interval -= 10;  
                    }  
                }  
            }  
              
            watch.Reset();  
            watch.Start();  
        }

看起来很多,其实大部分代码是用来计算FPS的~~~哈哈,很简单
接下来是Update和Render。都是调用具体类的响应方法。。。

private void Update(float elapsedTime)  
        {  
            Player1.Update(elapsedTime);  
            EnemyManager.Update(elapsedTime);  
              
            BulletManager.Update(elapsedTime);  
            BossManager.Update(elapsedTime);  
              
            GameBombManager.Update(elapsedTime);  
              
            BackgroundIndex--;  
            if ( BackgroundIndex < 0 )  
            {  
                BackgroundIndex = Data.BackgroundSource.Height - Config.ScreenHeight;  
            }  
        }  
          
        private void Render()  
        {  
            mG.Clear(Color.Black);  
  
            mG.DrawImage(Data.BackgroundSource, new Rectangle(0, 0, Config.ScreenWidth, Config.ScreenHeight), new Rectangle(0, BackgroundIndex, Data.BackgroundSource.Width, Config.ScreenHeight), GraphicsUnit.Pixel);  
              
            EnemyManager.Render(mG);  
            BossManager.Render(mG);  
              
            Player1.Render(mG);  
              
            BulletManager.Render(mG);  
              
            GameBombManager.Render(mG);  
              
            mG.DrawString("Time:" + RunTime.ToString(), Data.NormalFont, Brushes.White, 0, 0);  
        }

这么一来,框架大概有了,虽然比较混乱。

你可能感兴趣的:(基于C#弹幕类射击游戏的实现——(五)主场景)