public static void Initialize() { // Get the path of the save game string fullpath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, GameConstants.LEADERBOARDS_FILE_NAME); // Check to see if the save exists if (!File.Exists(fullpath)) { //If the file doesn't exist, make a fake one... // Create the data to save HighScoreData data = new HighScoreData(5); data.PlayerName[0] = "CPU"; data.Score[0] = 2000; data.PlayerName[1] = "CPU"; data.Score[1] = 1000; data.PlayerName[2] = "CPU"; data.Score[2] = 500; data.PlayerName[3] = "CPU"; data.Score[3] = 100; data.PlayerName[4] = "CPU"; data.Score[4] = 50; SaveHighScores(data); } }
public void SaveHighScore(int score, string name) { // Create the data to save HighScoreData data = LoadHighScores(); int scoreIndex = -1; for (int i = 0; i < data.Count; i++) { if (score > data.Score[i]) { scoreIndex = i; break; } } if (scoreIndex > -1) { //New high score found ... do swaps for (int i = data.Count - 1; i > scoreIndex; i--) { data.PlayerName[i] = data.PlayerName[i - 1]; data.Score[i] = data.Score[i - 1]; } data.PlayerName[scoreIndex] = name; data.Score[scoreIndex] = score; SaveHighScores(data); } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { RandomNumberGenerator.Initialize(); HighScoreData.Initialize(); _ship = new Ship(Content, "Ship", new Vector2(GameConstants.WINDOW_WIDTH / 2, GameConstants.WINDOW_HEIGHT / 2)); _asteroids = new List <Asteroid>(); _projectiles = new List <Projectile>(); _menu = new Menu(Content); _player = new Player(Content, "baseFont", new Vector2(GameConstants.WINDOW_WIDTH / 2, GameConstants.WINDOW_HEIGHT / 2)); _score = new Score(Content, new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }); base.Initialize(); }
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); } } }
public static void SaveHighScores(HighScoreData data) { // Get the path of the save game string fullpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GameConstants.LEADERBOARDS_FILE_NAME); // Open the file, creating it if necessary FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate); try { // Convert the object to XML data and put it in the stream XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); serializer.Serialize(stream, data); } finally { // Close the file stream.Close(); } }
/// <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); }