enum GameState { TitleScreen, Playing, PlayerDead, GameOver }; GameState gameState = GameState.TitleScreen;
Vector2 gameOverPosition = new Vector2(350, 300); Vector2 livesPosition = new Vector2(600, 580); Texture2D titleScreen; float deathTimer = 0.0f; float deathDelay = 5.0f;
LoadContent()
method that loads straight into the first level of the game. Replace the current LoadContent()
method with the following:protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); TileMap.Initialize( Content.Load<Texture2D>(@"TexturesPlatformTiles")); TileMap.spriteFont = Content.Load<SpriteFont>(@"FontsPericles8"); pericles8 = Content.Load<SpriteFont>(@"FontsPericles8"); titleScreen = Content.Load<Texture2D>(@"TexturesTitleScreen"); Camera.WorldRectangle = new Rectangle(0, 0, 160 * 48, 12 * 48); Camera.Position = Vector2.Zero; Camera.ViewPortWidth = 800; Camera.ViewPortHeight = 600; player = new Player(Content); LevelManager.Initialize(Content, player); }
StartNewGame()
helper method to the Game1 class:private void StartNewGame() { player.Revive(); player.LivesRemaining = 3; player.WorldLocation = Vector2.Zero; LevelManager.LoadLevel(0); }
Update()
method with the following to remove any temporary code and implement the game state logic:protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); KeyboardState keyState = Keyboard.GetState(); GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; if (gameState == GameState.TitleScreen) { if (keyState.IsKeyDown(Keys.Space) || gamepadState.Buttons.A == ButtonState.Pressed) { StartNewGame(); gameState = GameState.Playing; } } if (gameState == GameState.Playing) { player.Update(gameTime); LevelManager.Update(gameTime); if (player.Dead) { if (player.LivesRemaining > 0) { gameState = GameState.PlayerDead; deathTimer = 0.0f; } else { gameState = GameState.GameOver; deathTimer = 0.0f; } } } if (gameState == GameState.PlayerDead) { player.Update(gameTime); LevelManager.Update(gameTime); deathTimer += elapsed; if (deathTimer > deathDelay) { player.WorldLocation = Vector2.Zero; LevelManager.ReloadLevel(); player.Revive(); gameState = GameState.Playing; } } if (gameState == GameState.GameOver) { deathTimer += elapsed; if (deathTimer > deathDelay) { gameState = GameState.TitleScreen; } } base.Update(gameTime); }
Draw()
method with the following:protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin( SpriteSortMode.BackToFront, BlendState.AlphaBlend); if (gameState == GameState.TitleScreen) { spriteBatch.Draw(titleScreen, Vector2.Zero, Color.White); } if ((gameState == GameState.Playing) || (gameState == GameState.PlayerDead) || (gameState == GameState.GameOver)) { TileMap.Draw(spriteBatch); player.Draw(spriteBatch); LevelManager.Draw(spriteBatch); spriteBatch.DrawString( pericles8, "Score: " + player.Score.ToString(), scorePosition, Color.White); spriteBatch.DrawString( pericles8, "Lives Remaining: " + player.LivesRemaining.ToString(), livesPosition, Color.White); } if (gameState == GameState.PlayerDead) { } if (gameState == GameState.GameOver) { spriteBatch.DrawString( pericles8, "G A M E O V E R !", gameOverPosition, Color.White); } spriteBatch.End(); base.Draw(gameTime); }
Much as we did in our previous games, we defined a limited set of game states, beginning the game in TitleScreen
mode. The Update()
and Draw()
methods are segmented to take different actions based on the current gameState
value.
Non-interactive game states (PlayerDead
and GameOver
) are controlled by our standard timing mechanism, allowing the game to advance after a preset period of time has elapsed (five seconds in this case).
Of all of the games presented in this book, Gemstone Hunter is perhaps the most open for customization and expansion. Here are just a handful of suggestions for implementing enhancements to Gemstone Hunter based on things you have learned building the games in this book:
3.133.12.172