public void writeScoreData(ScoreData newScore) { if (scoreListData.ScoreListData.Count == 0) { scoreListData.ScoreListData.Add(newScore); }else{ for (int i = 0; i < scoreListData.ScoreListData.Count; i++) { if (scoreListData.ScoreListData[i].Level == newScore.Level) { scoreListData.ScoreListData[i] = newScore; break; } } scoreListData.ScoreListData.Add(newScore); } IAsyncResult storageDevice = StorageDevice.BeginShowSelector(PlayerIndex.One, null, null); device = StorageDevice.EndShowSelector(storageDevice); // Open a storage container. IAsyncResult result = device.BeginOpenContainer("ScoreData", null, null); // Wait for the WaitHandle to become signaled. result.AsyncWaitHandle.WaitOne(); StorageContainer container = device.EndOpenContainer(result); // Close the wait handle. result.AsyncWaitHandle.Close(); string filename = "savegame.sav"; // Check to see whether the save exists. if (container.FileExists(filename)) // Delete it so that we can create one fresh. container.DeleteFile(filename); // Create the file. Stream stream = container.CreateFile(filename); // Convert the object to XML data and put it in the stream. XmlSerializer serializer = new XmlSerializer(typeof(ScoreList)); serializer.Serialize(stream, scoreListData); // Close the file. stream.Close(); // Dispose the container, to commit changes. container.Dispose(); }
public PlayScreen(ContentManager theContent, GraphicsDevice graphicDevice, EventHandler theScreenEvent) : base(theScreenEvent) { bgPosition = new Rectangle(0, 0, graphicDevice.Viewport.Width, graphicDevice.Viewport.Height); viewPortWidth = graphicDevice.Viewport.Width; bgTexture = theContent.Load<Texture2D>("image/bg"); btnKembaliActive = theContent.Load<Texture2D>("image/kembali-active"); btnKembaliHover = theContent.Load<Texture2D>("image/kembali-hover"); btnKembali = btnKembaliActive; btnKembaliPosition = new Rectangle(100, 521, 150, 35); lihatSoalActive = theContent.Load<Texture2D>("image/lihat-soal-active"); lihatSoalHover = theContent.Load<Texture2D>("image/lihat-soal-hover"); btnLihatSoalPosition = new Rectangle(550, 521, 150, 35); btnLihatSoal = lihatSoalActive; font = theContent.Load<SpriteFont>("MenuFont"); gameFont = theContent.Load<SpriteFont>("gameFont"); finishFont = theContent.Load<SpriteFont>("finishedText"); highScore = theContent.Load<SpriteFont>("highscore"); readScoreData(); scoreData = (from s in scoreListData.ScoreListData where s.Level.Equals(SUB_LEVEL) select s).FirstOrDefault(); scoreData.Level = (scoreData.Level == 0) ? SUB_LEVEL : scoreData.Level; if (SUB_LEVEL == Game1.LEVEL11) { var xml = theContent.Load<List<Kata>>("data/level1"); sapi = new SAPIEngine(xml); textInfo = theContent.Load<Texture2D>("image/level1msg"); textInfoPosition = new Rectangle(107, 130, 600, 60); levelSound = theContent.Load<SoundEffect>("sound/angka_apa_ini"); }else if (SUB_LEVEL == Game1.LEVEL12) { var xml = theContent.Load<List<Kata>>("data/level12"); sapi = new SAPIEngine(xml); textInfo = theContent.Load<Texture2D>("image/level1msg"); textInfoPosition = new Rectangle(107, 130, 600, 60); levelSound = theContent.Load<SoundEffect>("sound/angka_apa_ini"); }else if (SUB_LEVEL == Game1.LEVEL21) { var xml = theContent.Load<List<Kata>>("data/level21"); sapi = new SAPIEngine(xml); textInfo = theContent.Load<Texture2D>("image/level2msg"); textInfoPosition = new Rectangle(107, 130, 600, 60); levelSound = theContent.Load<SoundEffect>("sound/huruf_apa_ini"); } else if (SUB_LEVEL == Game1.LEVEL22) { var xml = theContent.Load<List<Kata>>("data/level21"); sapi = new SAPIEngine(xml); textInfo = theContent.Load<Texture2D>("image/level2msg"); textInfoPosition = new Rectangle(107, 130, 600, 60); listSoalHurufKata = theContent.Load<List<Soal>>("data/level22"); levelSound = theContent.Load<SoundEffect>("sound/huruf_apa_ini"); } else if (SUB_LEVEL == Game1.LEVEL3) { var xml = theContent.Load<List<Kata>>("data/level3data"); sapi = new SAPIEngine(xml); textInfo = theContent.Load<Texture2D>("image/level3msg"); textInfoPosition = new Rectangle(107, 130, 600, 60); listSoalHurufKata = theContent.Load<List<Soal>>("data/level3soal"); levelSound = theContent.Load<SoundEffect>("sound/kata_apa_ini"); } trueSound = theContent.Load<SoundEffect>("sound/true_sound"); falseSound = theContent.Load<SoundEffect>("sound/false_sound"); winSound = theContent.Load<SoundEffect>("sound/minigame_win"); loseSound = theContent.Load<SoundEffect>("sound/minigame_lose"); levelStartSound = theContent.Load<SoundEffect>("sound/level_start"); levelStartInstance = levelStartSound.CreateInstance(); levelStartInstance.Volume = 0.2f; levelStartInstance.Play(); levelSoundInstance = levelSound.CreateInstance(); levelSoundInstance.Play(); listSoal = new List<string>(); soal = ""; Game1.MENU_SOUND_INSTANCE.Stop(); }
//Update all of the elements that need updating in the Controller Detect Screen public override void Update(GameTime theTime) { MouseState mouse = Mouse.GetState(); Rectangle hoverPosition = new Rectangle(mouse.X, mouse.Y, 1, 1); if (hoverPosition.Intersects(btnKembaliPosition)) { btnKembali = btnKembaliHover; } else if (hoverPosition.Intersects(btnLihatSoalPosition)) { btnLihatSoal = lihatSoalHover; } else { btnKembali = btnKembaliActive; btnLihatSoal = lihatSoalActive; } if (mouse.LeftButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed) { Point pointer = new Point(mouse.X, mouse.Y); if (btnKembaliPosition.Contains(pointer)) { Game1.MOUSE_DOWN.Play(); Thread.Sleep(200); sapi.stopListening(); sapi.unload(); if (SUB_LEVEL == Game1.LEVEL11 || SUB_LEVEL == Game1.LEVEL12) { ScreenEvent.Invoke(Game1.LEVEL1, new EventArgs()); return; } else if (SUB_LEVEL == Game1.LEVEL21 || SUB_LEVEL == Game1.LEVEL22) { ScreenEvent.Invoke(Game1.LEVEL2, new EventArgs()); return; } else { ScreenEvent.Invoke(Game1.LEVEL_SCREEN, new EventArgs()); return; } } else if (btnLihatSoalPosition.Contains(pointer)) { ScreenEvent.Invoke(Game1.SOAL, new EventArgs()); return; } } if (levelStartInstance.State == SoundState.Stopped && levelSoundInstance.State == SoundState.Stopped) { if (!selesai) { if (newSoal) { if (listSoal.Count < jumlahSoal) { drawSoal(); newSoal = false; sapi.prepareNewListening(); sapi.startListening(); Thread.Sleep(200); } else { newSoal = false; } } if (sapi.getResult) { //MessageBox.Show(sapi.soal + " " + sapi.isTrue); if (sapi.isTrue) { benar += 1; trueSound.Play(); } else { falseSound.Play(); } sapi.stopListening(); Thread.Sleep(200); newSoal = true; /* check selesai atau tidak */ if (listSoal.Count >= jumlahSoal) { selesai = true; } } } else { //MessageBox.Show("Selesai"); if (writeScore) { double score = Math.Round(((double)benar / jumlahSoal) * 100, 2); ScoreData newScore = new ScoreData(); newScore = scoreData; if (score > scoreData.First) { newScore.Third = newScore.Second; newScore.Second = newScore.First; newScore.First = score; } else if (score > scoreData.Second) { newScore.Third = newScore.Second; newScore.Second = score; } else if (score > scoreData.Third) { newScore.Third = score; } writeScoreData(newScore); writeScore = false; } } } base.Update(theTime); }