我想在单游戏中制作一个开始屏幕,在按下按钮开始之前游戏中不会发生任何事情
所以基本上,我想制作一个菜单样式的屏幕,其中它是标题屏幕,并且没有游戏机制在后台启动,例如计时器或其他进程,直到玩家按下空格进行提示。真正开始游戏,我不希望用枚举或过于复杂的东西来完成,而只是简单的标题屏幕提示用户继续。
我有结束标题屏幕,但我找不到一个简洁的解决方案来开始它
以下是我的结局
任何建议都表示赞赏
if (!player.dead)
player.animation.Draw(_spriteBatch);
if (!player.dead)
{
_spriteBatch.DrawString(MainFont, $"Time survived: {timer.ToString("F3")}",new Vector2(player.Position.X - 240, player.Position.Y + 270), Color.White);
}
else
{
_spriteBatch.Draw(endScreen, new Vector2(player.Position.X -900, player.Position.Y - 700), Color.White);
_spriteBatch.DrawString(MainFont, "YOU DIED", new Vector2(player.Position.X - 155.0f, player.Position.Y - 70), Color.Gold);
_spriteBatch.DrawString(MainFont, "PRESS ESCAPE TO EXIT", new Vector2(player.Position.X - 325.0f, player.Position.Y + -10.0f), Color.Gold);
_spriteBatch.DrawString(MainFont, $"Time Survived: {timer.ToString("F3")}", new Vector2(player.Position.X - 280.0f, player.Position.Y + 50.0f), Color.White);
// Creates a formatted string with the current timer value, including to the decimal place you want for this example its 3.
//$ indicates that the string will contain expressions, and the expressions will be evaluated and inserted into the string.
}
有几种方法可以做到这一点。您不需要使用枚举,但这样更具可读性。
您可以使用一种相当简单的状态机形式,其中包含游戏可能处于的所有不同状态:菜单、播放、暂停、游戏结束。通常,您有一个方法可以在这些状态之间切换并触发所有相关的效果、UI 等:
public enum GameStates { // Just for readability
Menu,
Playing,
Paused,
GameOver
}
public class GameSession {
GameStates _state = GameStates.Menu;
public void SetGameState(GameStates state) {
if (state != _state) {
_state = state;
switch (state) {
case GameStates.Menu:
// Show menu window or overlay here if not already open
// Pause game world time flow
// Start potential transitions here
break;
case GameStates.Playing:
// Resume game world time flow
break;
case GameStates.Paused:
// Pause game world time flow
// Maybe show game paused UI
break;
case GameStates.GameOver:
// Show game over screen or overlay
// Pause game world time flow (not strictly)
// Any interaction from player leads to state e.g. GameStates.Menu
break;
default:
break;
}
}
}
}
设置状态将在处理输入时发生,例如用于打开菜单的转义键(顺便说一下,暂停和菜单通常是一种状态),再次按下转义键将关闭并恢复等。死亡会使游戏结束,等等。
这样,它就可以轻松扩展并保持代码结构化。让我知道这对您是否有用!