public void Draw(SpriteBatch spriteBatch) { if (_gameState != GameState.PLAYING && _gameState != GameState.GAMEOVER && _gameState != GameState.NAME_ENTRY) { spriteBatch.Draw(_background, Vector2.Zero, Color.White); } if (_gameState == GameState.GAMEOVER) { spriteBatch.Draw(_backgroundGameOver, Vector2.Zero, Color.White); spriteBatch.Draw(_playAgainButton, _playAgainPosition, Color.White); spriteBatch.Draw(_mainMenuButton, _mainMenuPosition, Color.White); } if (_gameState == GameState.STARTUP) { spriteBatch.Draw(_startButton, _startResumePosition, Color.White); spriteBatch.Draw(_controlsButton, _controlsPosition, Color.White); spriteBatch.Draw(_leaderboardsButton, _leaderboardsPosition, Color.White); } if (_gameState == GameState.NAME_ENTRY) { Vector2 nameCommandPos = new Vector2( GameConstants.WINDOW_WIDTH / 2 - _font.MeasureString(GameConstants.MENU_ENTER_NAME_COMMAND).X / 2, GameConstants.WINDOW_HEIGHT / 2 - _font.MeasureString(GameConstants.MENU_ENTER_NAME_COMMAND).Y / 2 - GameConstants.MENU_ENTER_NAME_COMMAND_MARGIN); spriteBatch.DrawString(_font, GameConstants.MENU_ENTER_NAME_COMMAND, nameCommandPos, Color.White); } if (_gameState == GameState.PAUSED) { spriteBatch.Draw(_resumeButton, _startResumePosition, Color.White); spriteBatch.Draw(_controlsButton, _controlsPosition, Color.White); spriteBatch.Draw(_leaderboardsButton, _leaderboardsPosition, Color.White); } if (_showControls && _gameState != GameState.PLAYING && _gameState != GameState.GAMEOVER) { spriteBatch.Draw(_controls, new Vector2(GameConstants.WINDOW_WIDTH / 2, GameConstants.MENU_BUTTON_AREA_Y), Color.White); } if (_showLeaderboards && _gameState != GameState.PLAYING && _gameState != GameState.GAMEOVER) { HighScoreData data = HighScoreData.LoadHighScores(); for (int i = 0; i < data.Count; i++) { spriteBatch.DrawString(_font, data.PlayerName[i], new Vector2(GameConstants.WINDOW_WIDTH / 2, GameConstants.MENU_LEADERBOARD_Y + GameConstants.MENU_LEADERBOARD_MARGIN_Y * i), Color.White); spriteBatch.DrawString(_font, data.Score[i].ToString(), new Vector2(GameConstants.WINDOW_WIDTH / 2 + GameConstants.MENU_LEADERBOARD_MARGIN_X, GameConstants.MENU_LEADERBOARD_Y + GameConstants.MENU_LEADERBOARD_MARGIN_Y * i), Color.White); } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { KeyboardState currentKeyboardState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); #region Mouse Visibility Support if (_menu.Gamestate != GameState.PLAYING) { IsMouseVisible = true; } else { IsMouseVisible = false; } #endregion #region Menu Support if (currentKeyboardState.IsKeyDown(Keys.Escape) && _oldKeyboardState.IsKeyUp(Keys.Escape) && _menu.Gamestate == GameState.PLAYING) { _menu.Gamestate = GameState.PAUSED; _menu.ShowControls = true; _menu.ShowLeaderboards = false; } else if (currentKeyboardState.IsKeyDown(Keys.Escape) && _oldKeyboardState.IsKeyUp(Keys.Escape) && _menu.Gamestate == GameState.PAUSED) { _menu.Gamestate = GameState.PLAYING; } if (_restart == true) { PlayAgain(); } if (_canStart == true && _menu.Gamestate != GameState.GAMEOVER) { StartGame(); } if (_updateLeaderboard) { HighScoreData.LoadHighScores().SaveHighScore(_score.CurrentScore, _player.Name); _updateLeaderboard = false; } #endregion #region Updates _menu.Update(mouseState); _player.Update(_menu.Gamestate, currentKeyboardState); #endregion #region GameObject Updates if (_menu.Gamestate == GameState.PLAYING) { if (_ship.IsAlive) { _ship.Update(gameTime, Keyboard.GetState(), _menu.Gamestate); UpdateAsteroids(gameTime); UpdateProjectiles(gameTime); GenerateNewAsteroids(); } #region Collision Support CheckOutOfBounds(); ResolveShipAsteroidCollision(); ResolveAsteroidProjectileCollision(); CheckShipHealth(); #endregion #region Cleaning Support ClearFinishedExplosions(); ClearOffScreenProjectiles(); ClearDestroyedAsteroids(); #endregion UpdateExplosions(gameTime); } #endregion _oldKeyboardState = currentKeyboardState; base.Update(gameTime); }