基本上我正在尝试在屏幕上实现游戏(以及稍后的暂停屏幕等)。我现在要做这件事,只是用一个字符串说游戏结束重新绘制屏幕,并且我没有为游戏结束屏幕使用任何纹理。
我也很难获得键盘输入,所以当它确实说游戏结束时,我想要它以便在按下一个键(例如 ENTER 键)时让它重新启动游戏,但是对于 XNA,似乎当你按下菜单上的一个键,它的处理发生得太快了,(我假设这是由于更新和绘制方法被调用的次数......例如每秒60次)。综上所述,我希望在屏幕上按下游戏键和游戏实际重新启动并重新绘制之间有一点延迟,所以希望我可以为其他菜单实现这一点,所以我不会必须不断地问类似的问题,因此让您休息一下!:D 哈哈
好的,这是与我的情况最相关的代码:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
//if (userPlayer.Lives <= 0) //if hascollided is used here, it may change from true to false and vice versa, therefore the game over screen wont stay displayed and will revert back to the other screen when there isnt a coliison.
if (inGameScreenShowing == true)
{
InGameScreen(spriteBatch, gameTime);
}
spriteBatch.DrawString(someText, "time?...: " + delay, new Vector2(screenWidth / 2, screenHeight / 6), Color.Pink);
spriteBatch.End();
base.Draw(gameTime);
}
public void InGameScreen(SpriteBatch spriteBatch, GameTime gameTime)
{
if (userPlayer.Lives <=0)
{
if (inGameScreenShowing == true)
{
inGameScreenShowing = false;
gameOverScreenShowing = true;
GameOverScreen(spriteBatch, gameTime);
}
}
if (gameOverScreenShowing == false)
{
userPlayer.Draw(spriteBatch);
computerPlayer.Draw(spriteBatch);
//spriteBatch.DrawString(someText, "Plyr&CompCrashed: " + playerCompHaveCollided, new Vector2(screenWidth - 300, 30), Color.Blue);
spriteBatch.DrawString(someText, "Plyr Pos: " + userPlayer.Position, new Vector2(30, 30), Color.Red);
spriteBatch.DrawString(someText, "Plyr Lives: " + userPlayer.Lives, new Vector2(screenWidth - 300, 30), Color.Orange);
spriteBatch.DrawString(someText, "Plyr Score: " + userPlayer.Score, new Vector2(screenWidth - 300, 60), Color.LightBlue);
spriteBatch.DrawString(someText, "Window Size: " + screenWidth + " , " + screenHeight, new Vector2(30, 60), Color.Purple);
}
}
public void GameOverScreen(SpriteBatch spriteBatch, GameTime gameTime)
{
if (gameOverScreenShowing == true)
{
spriteBatch.DrawString(someText, "Game Over", new Vector2(screenWidth / 2 - 30, screenHeight / 2), Color.Yellow);
}
if (lastKeyboardState == null && currentKeyboardState == null)
{
lastKeyboardState = currentKeyboardState = Keyboard.GetState();
}
else
{
if (currentKeyboardState.IsKeyDown(Keys.Enter) && lastKeyboardState.IsKeyUp(Keys.Space))
{
delay += dt;
if (delay >= 1)
{
gameOverScreenShowing = false;
inGameScreenShowing = true;
userPlayer.Lives = 3;
this.Draw(gameTime);
//InGameScreen(spriteBatch, gameTime);
}
}
}
lastKeyboardState = currentKeyboardState;
}
我知道其中一些方法中的某些代码可能根本没有意义,例如延迟 +=dt... 我试图在按下键和实际更改游戏状态之间引入延迟... lastKeyboardState = currentKeyboardState 也可能应该在游戏结束方法中的其他地方,但我很久以前在这个方法中有这个,只是把它留在那里...... :)
感谢您的输入(没有双关语)伙计们:D