// removes a life and checks to see when the user has lost all his lives
        private int UpdateLives(ref int livesRemaining)
        {
            livesRemaining = livesRemaining - 1;

            if (livesRemaining == 0)
            {
                //  stop the timer
                gameTimer.Stop();
                // stops the background music
                backSound.Stop();
                // shows the game over screen
                LoseScreen theForm = new LoseScreen();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }

            return(livesRemaining);
        }
        private void mainGameTimer(object sender, EventArgs e)
        {
            // linking the jumpspeed integer with the player picture boxes to location
            player.Top += jumpSpeed;

            // refresh the player picture box consistently
            player.Refresh();

            // if jumping is true and force is less than 0
            // then change jumping to false
            if (jumping && force < 0)
            {
                jumping = false;
            }
            // if jumping is true
            // then change jump speed to -12
            // reduce force by 1
            if (jumping)
            {
                jumpSpeed = -12;
                force    -= 1;
            }
            else
            {
                // else change the jump speed to 12
                jumpSpeed = 12;
            }

            // if go left is true and players left is greater than 100 pixels
            // only then move the character towards left of the
            if (goleft && player.Left > 100)
            {
                player.Left -= playSpeed;
            }
            // by doing the if statement above, the player picture will stop

            // if go right Bool is true
            // player left plus players width plus 100 is less than the forms width
            // then we move the player towards the right by adding to the players left
            if (goright && player.Left + (player.Width + 100) < this.ClientSize.Width)
            {
                player.Left += playSpeed;
            }
            // by doing the if statement above , the player picture will stop on the forms right

            //if go right is true and the background picture left is greater 1352
            // then we move the background picture towards the left
            if (goright && background.Left > -1353)
            {
                background.Left -= backLeft;


                // the for loop below is checking to see the platforms and coins in the level.
                // when they are found it will move them towards the left
                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && (String)x.Tag == "platform" || x is PictureBox && (String)x.Tag == "coin" || x is PictureBox && (String)x.Tag == "door" ||
                        x is PictureBox && (String)x.Tag == "key" || x is PictureBox && (String)x.Tag == "armor" || x is PictureBox && (String)x.Tag == "flame")
                    {
                        x.Left -= backLeft;
                    }
                }
            }
            // if go left is true and the background pictures left is less than 2
            // then we move the background picture towards the right
            if (goleft && background.Left < 2)
            {
                background.Left += backLeft;

                // below the is the for loop thats checking to see the platforms and coins in the level
                // when they are found in the level it will move them all towards the right with the background
                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && (String)x.Tag == "platform" || x is PictureBox && (String)x.Tag == "coin" || x is PictureBox && (String)x.Tag == "door" ||
                        x is PictureBox && (String)x.Tag == "key" || x is PictureBox && (String)x.Tag == "armor" || x is PictureBox && (String)x.Tag == "flame")
                    {
                        x.Left += backLeft;
                    }
                }
            }

            // below if the for loop thats checking for all of the controls in this form
            foreach (Control x in this.Controls)
            {
                // is X is a picture box and it has a tag of a platform
                if (x is PictureBox && (String)x.Tag == "platform")
                {
                    // then we are checking if the player is colliding with the platform
                    // and jumping is set to false
                    if (player.Bounds.IntersectsWith(x.Bounds) && !jumping)
                    {
                        // then we do the following
                        // set the force to 8
                        force      = 8;
                        player.Top = x.Top - player.Height;    // also we place the player on top of the picture box
                        jumpSpeed  = 0;                        // set the jump speed to 0
                    }
                }
                // if the picture box found has a tag of a coin
                if (x is PictureBox && (String)x.Tag == "coin")
                {
                    // now if the player collides with the pic box
                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        // plays the coin sound effect
                        Play(Application.StartupPath + "\\Coin.mp3");
                        this.Controls.Remove(x);         // then we are going to remove the coin image
                        score++;                         // add 1 to the score
                        // updates the score label to display the score
                        lblCoins.Text = ("Coins:" + score);
                    }
                }
                // if the picture box found has a tag of a coin
                if (x is PictureBox && (String)x.Tag == "armor")
                {
                    // now if the player collides with the pic box
                    if (player.Bounds.IntersectsWith(armor.Bounds))
                    {
                        // instructs the user on how to complete the level
                        lblObjective.Text = ("OBJ: Obtain the Key");
                        // play sound
                        Play(Application.StartupPath + "\\armor.mp3");

                        // removes the armor from the form
                        this.Controls.Remove(armor);                         // then we are going to remove the armor image


                        // changes the image of the player to the armor
                        player.Image = Properties.Resources.newPlayerSword;
                        // edits the boolean variable so that the game acknowledges the player has the armor
                        hasArmor = true;
                    }
                }
                if (x is PictureBox && (String)x.Tag == "key")
                {
                    // if the player collides with the key picture box and has the armor

                    if (player.Bounds.IntersectsWith(key.Bounds) && hasArmor == true)
                    {
                        // instructs the user on how to complete the level
                        lblObjective.Text = ("OBJ: Return to the Portal");
                        //play sound
                        Play(Application.StartupPath + "\\Unlock.mp3");

                        // changes the door to a portal
                        door.Image = Properties.Resources.portal_2;

                        // then we remove the key from the game
                        this.Controls.Remove(key);
                        // change the has key boolean to true
                        hasKey = true;
                    }
                }
            }

            // if the player collides with the door and has key  boolean is true along with the hasArmor boolean

            if (player.Bounds.IntersectsWith(door.Bounds) && hasKey && hasArmor)
            {
                // stop the timer
                gameTimer.Stop();
                // move to level 2
                Level2 theForm = new Level2();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }



            // this is where the player dies
            // if the player goes below the forms height then we will end the game
            if (player.Top + player.Height > this.ClientSize.Height + 60)
            {
                gameTimer.Stop();                        // stop the timer
                // stops the background music
                backSound.Stop();
                // shows the "you Lose" screen
                LoseScreen theForm = new LoseScreen();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }
        }
        private void mainGameTimer(object sender, EventArgs e)
        {
            // linking the jumpspeed integer with the player picture boxes to location
            player.Top += jumpSpeed;

            // refresh the player picture box consistently
            player.Refresh();

            // if jumping is true and force is less than 0
            // then change jumping to false
            if (jumping && force < 0)
            {
                jumping = false;
            }
            // if jumping is true
            // then change jump speed to -25
            // reduce force by 1
            if (jumping)
            {
                jumpSpeed = -25;

                force -= 1;
            }
            else
            {
                // else change the jump speed to 12
                jumpSpeed = 12;
            }

            // if go left is true and players left is greater than 100 pixels
            // only then move the character towards left of the
            if (goleft && player.Left > 100)
            {
                player.Left -= playSpeed;
            }
            // by doing the if statement above, the player picture will stop

            // if go right Bool is true
            // player left plus players width plus 100 is less than the forms width
            // then we move the player towards the right by adding to the players left
            if (goright && player.Left + (player.Width + 100) < this.ClientSize.Width)
            {
                player.Left += playSpeed;
            }
            // by doing the if statement above , the player picture will stop on the forms right

            //if go right is true and the background picture left is greater 1352
            // then we move the background picture towards the left
            if (goright && background.Left > -1353)
            {
                background.Left -= backLeft;


                // the for loop below is checking to see the platforms and coins in the level.
                // when they are found it will move them towards the left
                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && (String)x.Tag == "platform" || x is PictureBox && (String)x.Tag == "coin" || x is PictureBox && (String)x.Tag == "door" ||
                        x is PictureBox && (String)x.Tag == "sword" || x is PictureBox && (String)x.Tag == "monster" || x is PictureBox && (String)x.Tag == "flame" ||
                        x is PictureBox && (String)x.Tag == "slowPortal" || x is PictureBox && (String)x.Tag == "portal" || x is PictureBox && (String)x.Tag == "bat")
                    {
                        x.Left -= backLeft;
                    }
                }
            }
            // if go left is true and the background pictures left is less than 2
            // then we move the background picture towards the right
            if (goleft && background.Left < 2)
            {
                background.Left += backLeft;

                // below the is the for loop thats checking to see the platforms and coins in the level
                // when they are found in the level it will move them all towards the right with the background
                foreach (Control x in this.Controls)
                {
                    if (x is PictureBox && (String)x.Tag == "platform" || x is PictureBox && (String)x.Tag == "coin" || x is PictureBox && (String)x.Tag == "door" ||
                        x is PictureBox && (String)x.Tag == "sword" || x is PictureBox && (String)x.Tag == "monster" || x is PictureBox && (String)x.Tag == "flame" ||
                        x is PictureBox && (String)x.Tag == "slowPortal" || x is PictureBox && (String)x.Tag == "portal" || x is PictureBox && (String)x.Tag == "bat")
                    {
                        x.Left += backLeft;
                    }
                }
            }

            // below if the for loop thats checking for all of the controls in this form
            foreach (Control x in this.Controls)
            {
                // is X is a picture box and it has a tag of a platform
                if (x is PictureBox && (String)x.Tag == "platform")
                {
                    // then we are checking if the player is colliding with the platform
                    // and jumping is set to false
                    if (player.Bounds.IntersectsWith(x.Bounds) && !jumping)
                    {
                        // then we do the following
                        // set the force to 8
                        force      = 8;
                        player.Top = x.Top - player.Height;    // also we place the player on top of the picture box
                        jumpSpeed  = 0;                        // set the jump speed to 0
                    }
                }
                // is X is a picture box and it has a tag of a platform
                if (x is PictureBox && (String)x.Tag == "slowPortal")
                {
                    // then we are checking if the player is colliding with the portal
                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        // plays a sound
                        Play(Application.StartupPath + "\\Portal.mp3");

                        // removes a life
                        UpdateLives(ref lives);
                        // updates the lives label
                        lblLives.Text = ("Lives:" + lives);
                        this.Controls.Remove(x);                         // then we are going to remove the portal image
                    }
                }
                // is X is a picture box and it has a tag of a platform
                if (x is PictureBox && (String)x.Tag == "portal")
                {
                    // then we are checking if the player is colliding with the portal
                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        // stops the timer
                        gameTimer.Stop();
                        // stops the background music
                        backSound.Stop();
                        // displays the lose screen
                        LoseScreen theForm = new LoseScreen();
                        this.Hide();
                        theForm.ShowDialog();
                        this.Close();
                    }
                }
                // if the picture box found has a tag of a coin
                if (x is PictureBox && (String)x.Tag == "bat")
                {
                    // now if the player collides with the pic box
                    if (player.Bounds.IntersectsWith(x.Bounds))
                    {
                        this.Controls.Remove(x);                         // then we are going to remove the coin image
                        if (hasSword == true)
                        {
                            Play(Application.StartupPath + "\\bat.mp3");

                            score++;                             // add 1 to the score
                            lblBats.Text = ("Bats defeated:" + score);
                        }
                        else if (hasSword == false)
                        {
                            // you lose a life

                            UpdateLives(ref lives);
                            lblLives.Text = ("Lives:" + lives);
                        }
                    }
                }
                // if the picture box found has a tag of a coin
                if (x is PictureBox && (String)x.Tag == "sword")
                {
                    // now if the player collides with the pic box
                    if (player.Bounds.IntersectsWith(sword.Bounds))
                    {
                        // plays the sound
                        Play(Application.StartupPath + "\\Sword.mp3");
                        // shows obj label to instruct the user
                        lblObjective.Text = ("OBJ: Defeat the Beast");
                        this.Controls.Remove(sword);                         // then we are going to remove the sword image
                        // change the image of the player
                        player.Image = Properties.Resources.newPlayerSword;
                        // change the has sword boolean to true
                        hasSword = true;
                    }
                }
            }

            // if the player collides with the monster and has sword boolean is true

            if (player.Bounds.IntersectsWith(monster.Bounds) && hasSword)
            {
                // and stop the timer
                gameTimer.Stop();
                // stops the background music
                backSound.Stop();
                // displays the win screen
                Level3 theForm = new Level3();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }
            // if the player collides with the beast and the sword boolean is false
            if (player.Bounds.IntersectsWith(monster.Bounds) && hasSword == false)
            {
                //  stop the timer
                gameTimer.Stop();
                // stops the background music
                backSound.Stop();
                // shows the game over screen
                LoseScreen theForm = new LoseScreen();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }



            // this is where the player dies
            // if the player goes below the forms height then we will end the game
            if (player.Top + player.Height > this.ClientSize.Height + 60)
            {
                gameTimer.Stop();                // stop the timer
                // stops the background music
                backSound.Stop();
                // the loss screen will be displayed
                LoseScreen theForm = new LoseScreen();
                this.Hide();
                theForm.ShowDialog();
                this.Close();
            }
        }