/// <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();
            }
        }