【实例简介】含地图编辑模块
【实例截图】
【核心代码】
/*
* /
* Program : Game
* Author : hwenycocodq520 (hwenycocodq520#163.com)
* Date : 2014/3/27
* Resume : 游戏框架类,实现了基本的游戏循环,派生类需要实现游戏的初始化操作
*
* /
* Modifiy History
*
* Date :
* Resume :
*
*/
using System.Diagnostics;
namespace Hweny.ConsoleGame
{
public abstract class Game
{
protected int fps;
protected Engine engine;
public Game()
{
engine = new Engine();
}
protected abstract void Start();
protected virtual void Cleanup() { }
public void Run()
{
long gameTime = 0L;
long elapsedTime = 0L;
long lastTime = 0L;
long accumulatedTime = 0L;
long timeDiff = 0L;
long lastFpsTime = 0L;
int fpsTicks = 0;
Stopwatch sw = new Stopwatch();
sw.Start();
Start();
if (fps <= 0)
fps = 60;
long FRAME_PERIOD = 1000 / fps;
while (!engine.IsShutdown)
{
gameTime = sw.ElapsedMilliseconds;
elapsedTime = gameTime - lastTime;
lastTime = gameTime;
accumulatedTime = elapsedTime;
//处理控制台输入缓冲区中的输入记录
engine.Input.ProcessedInput();
if (!engine.IsPause)
{
//处理当前状态的输入操作
engine.StateMgr.HandleInput();
var update = false;
while (accumulatedTime > FRAME_PERIOD)
{
//更新当前状态逻辑
engine.StateMgr.Update(FRAME_PERIOD / 1000f);
accumulatedTime -= FRAME_PERIOD;
update = true;
}
if (update)
{
//“渲染”当前状态
engine.StateMgr.Render();
}
#if DEBUG
fpsTicks ;
if (gameTime - lastFpsTime > 1000)
{
System.Console.Title = engine.Title " fps:" fpsTicks;
lastFpsTime = gameTime;
fpsTicks = 0;
}
#endif
timeDiff = sw.ElapsedMilliseconds - gameTime;
int sleepTime = (int)(FRAME_PERIOD - timeDiff);
if (sleepTime > 0)
{
System.Threading.Thread.Sleep(sleepTime);
}
}
else
{
engine.StateMgr.OnPause();
System.Threading.Thread.Sleep((int)FRAME_PERIOD * 2);
}
}
engine.Shutdown();
Cleanup();
}
}
}