/// <summary> /// Handles end game freezing, blinking and calls new level or end game function. /// </summary> private async void EndLevel() { PacUpdater.Stop(); GhostUpdater.Stop(); MusicPlayer.Stop(); for (int i = 0; i < 8; i++) { if (i % 2 == 0) { RenderMap(MapFresh, Color.White); } else { RenderMap(MapFresh, MapColor); } bg.Render(g); await Task.Delay(500); } Controls.Clear(); Level++; if (Level < MaxLevel && !Player2) { PlayGame(false); } else { EndGame(); } }
/// <summary> /// Ends game by stoping game loop and enabling menu functionality. /// Saves new highscore in case of beating the previous one by the player. /// Generally destroys all of the forms's controls and loads them again with their default settings. /// </summary> private void EndGame() { PacUpdater.Stop(); GhostUpdater.Stop(); gameOn = false; MapFresh = null; if (Score >= HighScore) { HighScoreClass hscr = new HighScoreClass(); hscr.SaveHighScore(Score); } MusicPlayer.Stop(); this.AutoSize = true; this.Size = defSize; this.Controls.Clear(); components.Dispose(); InitializeComponent(false); HighlightSelected(menuSelected.Item2, HighScr); // To unhighlight previous selection and enable highscore load. menuSelected = new Tuple <mn, Label>(mn.highscore, HighScr); menuLayer = mn.submenu; Menu(Menu_HighScore); }
/// <summary> /// Handles the event raised by unexcited pacman's contact with one of the ghosts. /// </summary> private async void KillPacman() { const int PauseBeforeDeath = 500; const int ExplodingTime = 600; const int P2ScoreForKill = 1500; PacUpdater.Stop(); GhostUpdater.Stop(); if (Music) { MusicPlayer.SoundLocation = "../sounds/pacman_death.wav"; MusicPlayer.Play(); } if (Player2) { Score2 += P2ScoreForKill; } Entities[0].Item3.Image = Image.FromFile("../textures/PacStart.png"); Refresh(); await Task.Delay(PauseBeforeDeath); Entities[0].Item3.Image = Image.FromFile("../textures/PacExplode.png"); Refresh(); await Task.Delay(ExplodingTime); Lives--; Controls.Clear(); if (Lives > 0) { PlayGame(true); } else { EndGame(); } }
/// <summary> /// Provides loading and general preparing of the game at the level start-up. /// </summary> /// <param name="restart">Whether triggered by lavel's restart or finish.</param> private async void PlayGame(bool restart) { Label loading = new Label(); Label levelLabel = new Label(); LoadingAndInit(loading, levelLabel, Map.Item4); if (!restart) { if (Music) { MusicPlayer.SoundLocation = "../sounds/pacman_intermission.wav"; MusicPlayer.PlayLooping(); } Task playAnim = PlayAnimation(); await playAnim; } LoadHud(Lives - 2); // Gets the position of the first ghost located on the top of a ghost house. TopGhostInTiles = new Tuple <int, int>(Map.Item3.Item1 - 1, Map.Item3.Item2 - 1); LoadEntities(); // Places ready label displayed at the beginning of each game. Label ready = new Label(); PlaceLabel(ready, "READY!", Color.Yellow, new Point(11 * TileSizeInPxs - 8, 20 * TileSizeInPxs - 6), new Font("Ravie", 14, FontStyle.Bold)); // Nulls pellets and actual map in case of new level laod. if (!restart) { CollectedDots = 0; DeepCopy(Map.Item1, ref MapFresh); if (Level <= 13) { GhostUpdater.Interval -= 5; } } RenderMap(MapFresh, Map.Item4); loading.Visible = false; levelLabel.Visible = false; Refresh(); if (Music) { MusicPlayer.SoundLocation = "../sounds/pacman_beginning.wav"; MusicPlayer.Play(); } bg.Render(g); await Task.Delay(OpeningThemeLength); // It's possible that the player has pressed escape during the opening theme. if (gameOn) { ready.Dispose(); Update(); // Corects start positions of pacman and first ghost as they were located between the tiles at first. Entities[0].Item3.Location = new Point(Entities[0].Item3.Location.X - 9, Entities[0].Item3.Location.Y); Entities[1].Item3.Location = new Point(Entities[1].Item3.Location.X - 9, Entities[1].Item3.Location.Y); if (Music) { MusicPlayer.SoundLocation = "../sounds/pacman_siren.wav"; MusicPlayer.PlayLooping(); } // Starts updater that provides effect of main game cycle. PacUpdater.Start(); GhostUpdater.Interval = Player2 ? (PacTimer + 40 - (Level > 13 ? 65 : Level * 5)) : PacUpdater.Interval + 10; GhostUpdater.Start(); } }