📄 gamemain.cs
字号:
mode = ModeSwitch.None;
// Store the tick at which this frame started
Int64 startTick = sw.CurrentTick();
// Check if the user pressed the exit key
if (gi.KeyPressed((int)gi.HardwareKeys[Player.ButtonMap()[
(int)Player.Buttons.Quit]]))
{
done = true;
}
else if (levelLoaded && (level.Done || gi.KeyPressed(
(int)gi.HardwareKeys[Player.ButtonMap()[
(int)Player.Buttons.ResetLevel]])))
{
Reset();
Application.DoEvents();
continue;
}
// Update the game
update();
// Check for pending events from the OS
Application.DoEvents();
// Lock the framerate...
Int64 deltaMS = sw.DeltaTimeMilliseconds(sw.CurrentTick(), startTick);
Int64 minMS = (Int64)(1000.0F * MinSecondsPerFrame);
Int64 maxMS = (Int64)(1000.0F * MaxSecondsPerFrame);
// Check if the frame time was fast enough
if (deltaMS <= minMS)
{
// Loop until the frame time is met
do
{
if (gi.KeyPressed((int)gi.HardwareKeys[
Player.ButtonMap()[(int)Player.Buttons.Quit]]))
{
done = true;
break;
}
// Thread.Sleep(0);
Application.DoEvents();
deltaMS = sw.DeltaTimeMilliseconds(sw.CurrentTick(),
startTick);
} while (deltaMS < minMS);
}
// Increment the number of frames
numFrames++;
// Increment the overall time for these frames
if (deltaMS < maxMS)
secondsElapsedForCurrentFrames += deltaMS / 1000.0F;
else
secondsElapsedForCurrentFrames += maxMS / 1000.0F;
// Make sure enough time has elapsed since the last check
if (level != null && secondsElapsedForCurrentFrames >
level.FrameRateCheckRate)
{
currentSecondsPerFrame =
secondsElapsedForCurrentFrames / numFrames;
numFrames = 0;
secondsElapsedForCurrentFrames = 0;
}
}
}
/// <summary>
/// Breaks the run loop and causes the game to exit
/// </summary>
public void Stop()
{
done = true;
}
/// <summary>
/// Load the introduction to the level.
/// </summary>
public void LoadIntro()
{
DataSet dsIntro = new DataSet();
Debug.Assert(dsIntro != null,
"GameMain.LoadIntro: Failed to initialize intro DataSet");
dsIntro.Locale = CultureInfo.InvariantCulture;
// Load the intro xml file
dsIntro.ReadXml(GetFullPath(@"Data\Intro\intro.xml"));
// Load the intr resources specified in the xml file
intro = new Intro(dsIntro, graphics);
Debug.Assert(intro != null,
"GameMain.LoadIntro: Failed to initialize Intro");
// The intro is loaded at this point
introLoaded = true;
}
/// <summary>
/// Load the current level. For this demo there is only one.
/// </summary>
public void LoadLevel()
{
DataSet dsLevel = new DataSet();
Debug.Assert(dsLevel != null,
"GameMain.LoadLevel: Failed to initialize level DataSet");
dsLevel.Locale = CultureInfo.InvariantCulture;
// Load the level xml file
dsLevel.ReadXml(GetFullPath(@"Data\Level\level.xml"));
// Load the level resources specified in the xml file
level = new Level(dsLevel, graphics);
Debug.Assert(level != null,
"GameMain.LoadLevel: Failed to initialize level");
// The level is loaded at this point
levelLoaded = true;
}
/// <summary>
/// Update the level. This method is called during level gameplay.
/// </summary>
public void UpdateLevel()
{
// Update
level.Update(gi);
ui.Update(level);
// Draw
level.Draw();
ui.Draw(graphics);
// Flip the back buffer
graphics.Flip();
}
/// <summary>
/// Update the count down that is displayed before gameplay starts.
/// </summary>
public void UpdateCountdown()
{
if (startCountDown)
{
// Decrement the count
countDown -= currentSecondsPerFrame;
// Check if the count down is done
if (countDown <= 0.0F)
{
// Start updating the level
mode = ModeSwitch.UpdateLevel;
return;
}
}
// Update UI
ui.Update(level);
// Draw
level.Draw();
ui.Draw(graphics);
// Display the time left until the game starts. If the time is
// less than 1/2 second then flash
if (startCountDown && (countDown > 0.5F ||
(countDown <= 0.5F && (numFrames % 2 == 0))))
{
// Display "Start" for the last 1/2 second
graphics.DrawText(graphics.ScreenWidth >> 1,
graphics.ScreenHeight >> 1,
countDown > 0.5F ? string.Format(CultureInfo.InvariantCulture,
"{0}",
(int)(countDown + .49F)) : "Start",
Color.White, ui.Font, FontDrawOptions.DrawTextCenter);
}
if (!startCountDown)
{
int fireId = Player.ButtonMap()[(int)Player.Buttons.FireShot]
+ 1;
int quitId = Player.ButtonMap()[(int)Player.Buttons.Quit] + 1;
int resetId = Player.ButtonMap()[
(int)Player.Buttons.ResetLevel] + 1;
int x = graphics.ScreenWidth >> 1;
int y = (graphics.ScreenHeight >> 1) - 60;
graphics.DrawText(x, y, "<- -> to move player", Color.Yellow,
ui.Font, FontDrawOptions.DrawTextCenter);
#if (SMARTPHONE || MAINSTONE || DESKTOP)
y += 20;
graphics.DrawText(x, y, string.Format(
CultureInfo.InvariantCulture,
"Button {0} to fire",
fireId), Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
y += 20;
graphics.DrawText(x, y, string.Format(
CultureInfo.InvariantCulture,
"Button {0} to Quit",
quitId), Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
y += 20;
graphics.DrawText(x, y, string.Format(
CultureInfo.InvariantCulture,
"Button {0} to Reset",
resetId), Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
#else // PPC
y += 20;
graphics.DrawText(x, y,
"Action button to fire",
Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
y += 20;
graphics.DrawText(x, y,
"Click the upper right",
Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
y += 20;
graphics.DrawText(x, y,
"corner GMan to quit",
Color.Yellow, ui.Font,
FontDrawOptions.DrawTextCenter);
#endif
y += 20;
graphics.DrawText(x, y, "Press any key to start", Color.Red,
ui.Font, FontDrawOptions.DrawTextCenter);
if (gi.AnyKeyPressed())
startCountDown = true;
}
// Flip the back buffer
graphics.Flip();
}
/// <summary>
/// Update the intro that leads into the current level.
/// </summary>
public void UpdateIntro()
{
// If the level has finished loading and the intro is done
// playing then start the count down
if (levelLoaded && intro.Done)
{
mode = ModeSwitch.UpdateCountdown;
return;
}
// Update the intro
intro.Update(graphics);
// Draw the intro
intro.Draw(graphics);
// Flip the back buffers
graphics.Flip();
}
/// <summary>
/// Update the splash screen.
/// </summary>
public void UpdateSplash()
{
// Calculate the upper-left corner of the splash screen image
int x = (graphics.ScreenWidth - splash.Width) >> 1;
int y = (graphics.ScreenHeight - splash.Height) >> 1;
// The source region is the entire image
src.X = 0;
src.Y = 0;
src.Width = splash.Width;
src.Height = splash.Height;
// Draw the splash screen
graphics.DrawBitmap(x, y, src, splash);
// Flip the back buffers
graphics.Flip();
// Store the tick at which this frame started
Timer.Stopwatch sw = new Timer.Stopwatch();
Int64 startTick = sw.CurrentTick();
// Draw the splash screen
graphics.DrawBitmap(x, y, src, splash);
// Draw a "Loading" message as this might take a while
graphics.DrawText(graphics.ScreenWidth >> 1,
graphics.ScreenHeight - 50,
"Loading...",
Color.White, ui.Font, FontDrawOptions.DrawTextCenter);
// Flip the back buffers
graphics.Flip();
if (!introLoaded) LoadIntro();
// delay if the splash screen hasn't been up long enough
while (sw.DeltaTimeMilliseconds(sw.CurrentTick(), startTick)
< TotalSplashTime * 1000.0F)
Thread.Sleep(0);
mode = ModeSwitch.UpdateIntro;
}
/// <summary>
/// Clean up the game's resources.
/// </summary>
public void Dispose()
{
if (intro != null)
intro.Dispose();
if (ui != null)
ui.Dispose();
if (level != null)
level.Dispose();
if (graphics != null)
graphics.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -