这是正确的方法!
创建一个类GameOverScreen:
public class GameOverScreen
{
private Texture2D texture;
private Game1 game;
private KeyboardState lastState;
public GameOverScreen(Game1 game)
{
this.game = game;
texture = game.Content.Load<Texture2D>("GameOverScreen");
lastState = Keyboard.GetState();
}
public void Update()
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Enter) && lastState.IsKeyUp(Keys.Enter))
{
game.StartGame();
}
else if (keyboardState.IsKeyDown(Keys.Escape) && lastState.IsKeyUp(Keys.Escape))
{
game.Exit();
}
lastState = keyboardState;
}
public void Draw(SpriteBatch spriteBatch)
{
if (texture != null)
spriteBatch.Draw(texture, new Vector2(0f, 0f), Color.White);
}
}
实施GameOverScreen类
现在,我们有我们需要将代码添加到Game1.cs来实现它GameOverScreen类。
首先,我们需要为新屏幕的变量。在Game1类顶部添加一个新的
GameOverScreen object:
StartScreen startScreen;
GamePlayScreen gamePlayScreen;
GameOverScreen gameOverScreen;
接下来,我们需要的情况下,在Game1.Update()方法添加到switch语句的GameOverScreen:
case Screen.GameOverScreen:
if (gameOverScreen != null)
gameOverScreen.Update();
break;
我们必须为绘制做同样的()方法:
case Screen.GameOverScreen:
if (gameOverScreen != null)
gameOverScreen.Draw(spriteBatch);
break;
现在,我们需要以添加将关闭GamePlayScreen并打开GameOverScreen残局()方法。这将在满足游戏结束条件时被调用。
public void EndGame()
{
gameOverScreen = new GameOverScreen(this);
currentScreen = Screen.GameOverScreen;
gamePlayScreen = null;
}
一个微小的变化需要在StartGame()方法来进行为好。在GameOverScreen我们将会给用户重新启动游戏,这将调用StartGame()方法的选项。因此,在StartGame()方法结束时,我们只需添加一行代码即可将gameOverScreen设置为null。
gameOverScreen = null;
游戏结束条件
我们需要做的最后一件事就是以游戏结束的条件,这将在GamePlayScreen类进行处理的照顾。打开GamePlayScreen.cs。 我们在这里首先需要的是一个新的整数来保存生命的玩家,将其添加到类的顶部量:例如:
int lives = 3;
此值并不一定是3,你可以把它改成任何你喜欢的东西。 接下来,我们需要添加代码来减少每次一片蛋糕从屏幕底部移出并移除时的生命值。当生命等于0时,Game1.EndGame()将被调用。该代码将被添加到HandleFallingCake()方法中。
if (toRemove.Count > 0)
{
foreach (Sprite cake in toRemove)
{
cakeList.Remove(cake);
--lives;
if (lives == 0)
game.EndGame();
}
}
this.Exit(); Fire onNavigatedFromMethod?因为从只有游戏页面而不是整个应用程序出来才是重要的。 – harry180 2012-03-12 20:55:30
检查我的新答案 – Razor 2012-03-13 08:32:24