private void DoSaveGame() { if (store.FileExists(ScoreFileName)) { using (IsolatedStorageFileStream fs = store.OpenFile(ScoreFileName, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { using (StreamWriter sw = new StreamWriter(fs)) { // Convert the object to XML data and put it in the stream. XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); HighScoreData savedData = new HighScoreData(); try { savedData = (HighScoreData)serializer.Deserialize(fs); } catch (InvalidOperationException e) { Debug.WriteLine("An error occurred: '{0}'", e); Debug.WriteLine("ISSUE! THE SAVE DATA WILL NOW BE CLEARED AND RECREATED"); fs.Dispose(); //Prevents the file from throwing an in-use exception store.DeleteFile(ScoreFileName); StartHighScoreData(); return; } for (int i = 0; i < savedData.PlayerNames.Count; i++) { if (CurrentGameLength < savedData.Scores[i]) { savedData.Scores.RemoveAt(savedData.Scores.Count - 1);//Removes last item in list savedData.PlayerNames.RemoveAt(savedData.PlayerNames.Count - 1); savedData.Scores.Insert(i, (int)CurrentGameLength); savedData.PlayerNames.Insert(i, winnerName); fs.SetLength(0);//Erases the old data serializer.Serialize(fs, savedData); return; } } } } } } else { throw new InvalidOperationException("The file was never created."); } }
private HighScoreData DoLoadGame() { // Check to see whether the save exists. if (!store.FileExists(ScoreFileName)) { throw new InvalidOperationException("The file was never created."); } using (IsolatedStorageFileStream fs = store.OpenFile(ScoreFileName, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); HighScoreData data = (HighScoreData)serializer.Deserialize(fs); return(data); } } }
private void StartHighScoreData() { //Checks to see if the high score data exits, and creates it if doesn't exist yet. This should only run once per machine. IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); if (!store.FileExists(ScoreFileName)) { using (IsolatedStorageFileStream fs = store.OpenFile(ScoreFileName, FileMode.CreateNew)) { using (StreamWriter sw = new StreamWriter(fs)) { // Convert the object to XML data and put it in the stream. XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData)); HighScoreData data = new HighScoreData(); data.PlayerNames.Add("play1"); data.Scores.Add(100); data.PlayerNames.Add("play2"); data.Scores.Add(101); data.PlayerNames.Add("play3"); data.Scores.Add(102); data.PlayerNames.Add("play4"); data.Scores.Add(103); data.PlayerNames.Add("play5"); data.Scores.Add(104); serializer.Serialize(fs, data); } } } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.White); spriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, SpriteScale); spriteBatch.Draw(background, new Rectangle(new Point(0, 0), mainFrame), Color.White); switch (gameState) { case GameState.MainMenu: scoreFont.Draw(spriteBatch, version, new Vector2(mainFrame.X - 100, 50), Color.White); menuFont.Draw(spriteBatch, "Welcome to Wizard Pong!", new Vector2(mainFrame.X / 2, 300), colorOptionOne); scoreFont.Draw(spriteBatch, "To access the help screen, press " + (!XBoxGame ? "H" : "X") + ". To start a new two player game, press " + (!XBoxGame ? "B" : "Y") + ". To start a single player game, press " + (!XBoxGame ? "G" : "A") + ".", new Vector2(mainFrame.X / 2, 500), colorOptionTwo); scoreFont.Draw(spriteBatch, "To quit the game, press " + (!XBoxGame ? "escape" : "start") + ".", new Vector2(mainFrame.X / 2, mainFrame.Y - 100), colorOptionTwo); break; case GameState.Pause: for (int i = 1; i < walls.Count; i++) //Same thing as in NewGame thing { walls[i].Draw(spriteBatch); } //package.Draw(spriteBatch); //Looks bad if also drawing playerOne.Draw(spriteBatch); playerTwo.Draw(spriteBatch); for (int i = 0; i < activeSpells.Count; i++) { activeSpells[i].Draw(spriteBatch); } scoreFont.Draw(spriteBatch, playerOne.Score.ToString(), new Vector2(mainFrame.X / 2 - 70, 60), Color.White); scoreFont.Draw(spriteBatch, playerTwo.Score.ToString(), new Vector2(mainFrame.X / 2 + 70, 60), Color.White); scoreFont.Draw(spriteBatch, playerOne.Mana.ToString(), new Vector2(mainFrame.X / 2 - 200, 60), Color.White); scoreFont.Draw(spriteBatch, playerTwo.Mana.ToString(), new Vector2(mainFrame.X / 2 + 200, 60), Color.White); menuFont.Draw(spriteBatch, "Pause", new Vector2(mainFrame.X / 2, 300), colorOptionOne); scoreFont.Draw(spriteBatch, "To continue the game, press " + (!XBoxGame ? "P" : "the left joystick") + ".", new Vector2(mainFrame.X / 2, 500), colorOptionTwo); scoreFont.Draw(spriteBatch, "To access the help screen, press " + (!XBoxGame ? "H" : "X") + ".", new Vector2(mainFrame.X / 2, 1000), colorOptionTwo); scoreFont.Draw(spriteBatch, "To exit this game, press " + (!XBoxGame ? "M" : "B") + ".", new Vector2(mainFrame.X / 2, mainFrame.Y - 500), colorOptionTwo); break; case GameState.Game: for (int i = 0; i < walls.Count; i++) { walls[i].Draw(spriteBatch); } playerOne.Draw(spriteBatch); playerTwo.Draw(spriteBatch); package.Draw(spriteBatch); if (package.stuckBall) { menuFont.Draw(spriteBatch, "Please stop. It is just annoying.", new Vector2(mainFrame.X / 2, mainFrame.Y / 2), Color.Red, 0); } for (int i = 0; i < activeSpells.Count; i++) { activeSpells[i].Draw(spriteBatch); } scoreFont.Draw(spriteBatch, playerOne.Score.ToString(), new Vector2(mainFrame.X / 2 - 70, 60), Color.White); scoreFont.Draw(spriteBatch, playerTwo.Score.ToString(), new Vector2(mainFrame.X / 2 + 70, 60), Color.White); scoreFont.Draw(spriteBatch, playerOne.Mana.ToString(), new Vector2(mainFrame.X / 2 - 200, 60), Color.White); scoreFont.Draw(spriteBatch, playerTwo.Mana.ToString(), new Vector2(mainFrame.X / 2 + 200, 60), Color.White); break; case GameState.Help: menuFont.DrawControls(spriteBatch, "Help:", new Vector2(mainFrame.X / 15, 100), colorOptionOne, 0, true); scoreFont.DrawControls(spriteBatch, "P1", new Vector2(mainFrame.X / 7, 200), colorOptionTwo, 0, true); scoreFont.DrawControls(spriteBatch, "P2", new Vector2(mainFrame.X / 7 + 120, 200), colorOptionTwo, 0, true); List <string> CrlLabel = new List <string>(); List <string> spellInfo = new List <string>(); CrlLabel.Add("Move up:"); CrlLabel.Add("Move down:"); CrlLabel.Add("Move left:"); CrlLabel.Add("Move right:"); CrlLabel.Add("Force field:"); CrlLabel.Add("Slow time:"); CrlLabel.Add("Slime ball:"); CrlLabel.Add("Portal trap:"); spellInfo.Add(""); spellInfo.Add(""); spellInfo.Add(""); spellInfo.Add(""); spellInfo.Add("Creates a temporary wall to block the ball. Costs: " + ForceField.Cost.ToString()); spellInfo.Add("Slows the opposing player. Costs: " + SlowTime.Cost.ToString()); spellInfo.Add("Makes the ball invisible on the enemy side. Costs: " + SlimeBall.Cost.ToString()); spellInfo.Add("Creates a portal trap in front of the goal, but causes the player to be stuck. Costs: " + PortalTrap.Cost.ToString()); for (int i = 0; i < CrlLabel.Count; i++) { scoreFont.DrawRightAlgnWithSpace(spriteBatch, CrlLabel[i], new Vector2(mainFrame.X / 7 - 10, 200), colorOptionTwo, i + 1); scoreFont.DrawControls(spriteBatch, playerOne.CrlSpt(i), new Vector2(mainFrame.X / 7, 200), colorOptionTwo, i + 1, true); scoreFont.DrawControls(spriteBatch, playerTwo.CrlSpt(i), new Vector2(mainFrame.X / 7 + 120, 200), colorOptionTwo, i + 1, true); scoreFont.DrawControls(spriteBatch, spellInfo[i], new Vector2(mainFrame.X / 6 + 500, 200), colorOptionTwo, i + 1, true); } //TODO: Put spell icons on help menu scoreFont.DrawControls(spriteBatch, "Some rules of the game:", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 3, true); scoreFont.DrawControls(spriteBatch, "Each player starts with 50 mana, which automatically regenerates over time. It has maximum value of 100.", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 4, true); scoreFont.DrawControls(spriteBatch, "Each game is up to 5 rounds. The first player to three wins. Press " + (!XBoxGame ? "P" : "the left joystick") + " to pause during the game.", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 5, true); scoreFont.DrawControls(spriteBatch, "To exit the help menu, press " + (!XBoxGame ? "H" : "X") + ".", new Vector2(mainFrame.X / 15, mainFrame.Y * 6 / 7), colorOptionTwo, 0, true); //Only shows volume related things when on a XBox because a computer would display its volume manager scoreFont.DrawControls(spriteBatch, !XBoxGame ? "" : "To change the volume, use the DPad left and right arrows. It is currently at: " + ((int)(volume * 100)).ToString() + "%", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 6, true); scoreFont.DrawControls(spriteBatch, "To turn " + (MediaPlayer.State == MediaState.Stopped ? "on" : "off") + " background music, press " + (!XBoxGame ? "M" : "DPadLeft") + ".", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 7, true); scoreFont.DrawControls(spriteBatch, "To turn " + (!soundEffectsOn ? "on" : "off") + " sound effects, press " + (!XBoxGame ? "S" : "DPadRight") + ".", new Vector2(mainFrame.X / 15, 200), colorOptionTwo, CrlLabel.Count + 8, true); break; case GameState.EnterName: if (XBoxGame) { scoreFont.Draw(spriteBatch, "Using the DPad Up and Down to enter your initials. When you are done with a alphabet wheel,", new Vector2(mainFrame.X / 2, mainFrame.Y / 2 - 400), colorOptionTwo); scoreFont.Draw(spriteBatch, "use A to advance to the next wheel (or save the name).", new Vector2(mainFrame.X / 2, mainFrame.Y / 2 - 300), colorOptionTwo); scoreFont.Draw(spriteBatch, alphabet[firstWheel].ToString(), new Vector2(mainFrame.X / 2 - 100, mainFrame.Y / 2), colorOptionTwo); scoreFont.Draw(spriteBatch, alphabet[secondWheel].ToString(), new Vector2(mainFrame.X / 2, mainFrame.Y / 2), colorOptionTwo); scoreFont.Draw(spriteBatch, alphabet[thirdWheel].ToString(), new Vector2(mainFrame.X / 2 + 100, mainFrame.Y / 2), colorOptionTwo); } else { scoreFont.Draw(spriteBatch, "Please enter your name for the high score list. Press enter when you are done.", new Vector2(mainFrame.X / 2, 300), colorOptionTwo); scoreFont.Draw(spriteBatch, winnerName, new Vector2(mainFrame.X / 2, 500), colorOptionTwo); } break; case GameState.Score: scoreFont.Draw(spriteBatch, playerOne.Score.ToString(), new Vector2(mainFrame.X / 2 - 70, 60), Color.White); scoreFont.Draw(spriteBatch, playerTwo.Score.ToString(), new Vector2(mainFrame.X / 2 + 70, 60), Color.White); if (recentWinner == 1) { scoreFont.Draw(spriteBatch, "Player One won the round.", new Vector2(mainFrame.X / 2, mainFrame.Y / 2), colorOptionTwo); scoreFont.Draw(spriteBatch, "To continue playing, press " + (!XBoxGame ? "G" : "A") + ".", new Vector2(mainFrame.X / 2, mainFrame.Y / 2 + 100), colorOptionTwo); } else if (recentWinner == 2) { scoreFont.Draw(spriteBatch, "Player Two won the round.", new Vector2(mainFrame.X / 2, mainFrame.Y / 2), colorOptionTwo); scoreFont.Draw(spriteBatch, "To continue playing, press " + (!XBoxGame ? "G" : "A") + ".", new Vector2(mainFrame.X / 2, mainFrame.Y / 2 + 100), colorOptionTwo); } spriteBatch.Draw(scoreAnim, new Rectangle(mainFrame.X / 4 - scoreAnim.Width / (2 * scoreAnimColCount), mainFrame.Y / 2 - scoreAnim.Height / 2, scoreAnim.Width / scoreAnimColCount, scoreAnim.Height), new Rectangle(scoreAnim.Width / scoreAnimColCount * (int)scoreAnimPos, 0, scoreAnim.Width / scoreAnimColCount, scoreAnim.Height), Color.White); spriteBatch.Draw(scoreAnim, new Rectangle(mainFrame.X * 3 / 4 - scoreAnim.Width / (2 * scoreAnimColCount), mainFrame.Y / 2 - scoreAnim.Height / 2, scoreAnim.Width / scoreAnimColCount, scoreAnim.Height), new Rectangle(scoreAnim.Width / scoreAnimColCount * (int)scoreAnimPos, 0, scoreAnim.Width / scoreAnimColCount, scoreAnim.Height), Color.White); break; case GameState.Over: if (playerOne.Score > playerTwo.Score) { scoreFont.Draw(spriteBatch, "Player One Wins!", new Vector2(mainFrame.X / 2, mainFrame.Y / 2), colorOptionTwo); } else if (playerOne.Score < playerTwo.Score) { scoreFont.Draw(spriteBatch, "Player Two Wins!", new Vector2(mainFrame.X / 2, mainFrame.Y / 2), colorOptionTwo); } scoreFont.Draw(spriteBatch, "Press " + (!XBoxGame ? "G" : "A") + " to start a new game, or press " + (!XBoxGame ? "M" : "B") + " to go to the main menu.", new Vector2(mainFrame.X / 2, mainFrame.Y / 2 + 100), colorOptionTwo); HighScoreData scores = DoLoadGame(); if (playerTwo is AIPlayer) //Only shows high scores if after a game that could get on them { scoreFont.Draw(spriteBatch, "HIGHSCORES:", new Vector2(mainFrame.X / 2, 200), colorOptionTwo, 0); for (int i = 0; i < scores.PlayerNames.Count; i++) { scoreFont.Draw(spriteBatch, scores.PlayerNames[i].ToString() + " " + scores.Scores[i].ToString(), new Vector2(mainFrame.X / 2, 200), colorOptionTwo, i + 2); //i+2 to give space to title } } spriteBatch.Draw(overAnim, new Rectangle(200, mainFrame.Y / 2 - overAnim.Height / 2, overAnim.Width / overAnimColCount, overAnim.Height), new Rectangle(overAnim.Width / overAnimColCount * (int)overAnimPos, 0, overAnim.Width / overAnimColCount, overAnim.Height), Color.White); spriteBatch.Draw(overAnim, new Rectangle((mainFrame.X - 200) - (overAnim.Width / overAnimColCount), mainFrame.Y / 2 - overAnim.Height / 2, overAnim.Width / overAnimColCount, overAnim.Height), new Rectangle(overAnim.Width / overAnimColCount * (int)overAnimPos, 0, overAnim.Width / overAnimColCount, overAnim.Height), Color.White); break; } if (displayVolumeCounter > 0 && XBoxGame) { scoreFont.Draw(spriteBatch, "Vol: " + ((int)(volume * 100)).ToString() + "%", new Vector2(150, 50), Color.White); } spriteBatch.End(); base.Draw(gameTime); }