這里的的GameScene就是一個用于管理所有對象的類。整個游戲只有一個Scene
(其實(shí)還有MenuScene,LoadScene之類的,但這里沒有做,在下一個游戲《超級瑪麗》里會有詳細(xì)的介紹)
先來看看申明部分
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<string>[] mMapInfo;
東西挺多的,但其實(shí)只是各個游戲部件的整合而已。比如GameBulleManager、GameEnemyManager之類的,這些東西之后會介紹
然后是初始化,稍微多了一點(diǎn)點(diǎn)
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<string>[Config.OneMapTime]; // 3分鐘
for ( int i = 0; i < Config.OneMapTime; i++ )
{
this.mMapInfo[i] = new List<string>();
}
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");
}
調(diào)用各個類的構(gòu)造函數(shù)進(jìn)行初始化,唯一注意的是開啟了一個Timer控件來當(dāng)做游戲的主循環(huán)。。。因?yàn)镃# WINFORM很蛋疼,基于事件機(jī)制。。
接下來跳過載入地圖信息。。。因?yàn)檫€沒用到。
先來看看添加對象
public void AddBullet(GameBullet bullet)
{
BulletManager.AddBullet(bullet);
}
public void AddEnemy(GameEnemy enemy)
{
EnemyManager.AddEnemy(enemy);
}
public void AddBoss(GameBoss boss)
{
BossManager.AddBoss(boss);
}
其實(shí)都是調(diào)用的類本身的函數(shù)。。。。。
然后看看蛋疼的主循環(huán)
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();
}
看起來很多,其實(shí)大部分代碼是用來計算FPS的~~~哈哈,很簡單
接下來是Update和Render。都是調(diào)用具體類的響應(yīng)方法。。。
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);
}
這么一來,框架大概有了,雖然比較混亂。