Example #1
0
        private void MainTimerEvent(object sender, EventArgs e)
        {
            // These if-else statement is for checking whether the player's health and shield value is equal to zero
            // If it is true then end the game
            if (playerHealth > 1 && playerShield >= 0)
            {
                healthBar.Value = playerHealth;
                shieldBar.Value = playerShield;
            }
            else
            {
                gameOver = true;


                // This if-else statements are for to display the character based on the chosen character by the user
                // The default value of choseCharacter variable is man character
                if (ChooseCharacter.chosenCharacter == "woman")
                {
                    player.Image = Properties.Resources.p2_death;
                }
                else
                {
                    player.Image = Properties.Resources.death;
                }

                GameTimer.Stop();
                MedicTimer.Stop();
                SuperGunTimer.Stop();
                LaserGunTimer.Stop();
                shieldTimer.Stop();
                GrenadeTimer.Stop();

                StreamWriter sw = File.AppendText(path);
                sw.WriteLine("Kills: " + score.ToString());
                sw.Close();

                DateTime     thisDay = DateTime.Now;
                StreamWriter dt      = File.AppendText(path2);
                dt.WriteLine(thisDay.ToString());
                dt.Close();
            }

            txtAmmo.Text  = "Ammo: " + ammo;
            txtScore.Text = "Kills: " + score;
            txtLevel.Text = "Level " + level;

            if (tempScore == 10)
            {  //level up condition
                level++;
                tempScore = 0;
                MakeZombies(); // create the new zombie to the list
            }

            if (goleft == true && player.Left > 0)
            {
                player.Left -= speed;
            }

            if (goright == true && player.Left + player.Width < this.ClientSize.Width)
            {
                player.Left += speed;
            }

            if (goup == true && player.Top > 75)
            {
                player.Top -= speed;
            }

            if (godown == true && player.Top + player.Height < this.ClientSize.Height)
            {
                player.Top += speed;
            }

            foreach (Control x in this.Controls)
            {
                if (x is PictureBox && (string)x.Tag == "ammo")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player intersects with ammo
                    {
                        SoundPlayer reload = new SoundPlayer("../../Sound/reload.wav");
                        reload.Play();             // play reload sound effect
                        this.Controls.Remove(x);   // remove the ammo on screen
                        ((PictureBox)x).Dispose(); // dispose the picture box
                        ammo += 5;                 // add the ammo to 5
                    }
                }

                if (x is PictureBox && (string)x.Tag == "medic")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player intersects with medic
                    {
                        SoundPlayer pickMedic = new SoundPlayer("../../Sound/pickMedic.wav");
                        pickMedic.Play();                // play the pickMedic sound effect
                        this.Controls.Remove(x);         // remove the medic on screen
                        medicList.Remove((PictureBox)x); // remove the medic from the medic list
                        ((PictureBox)x).Dispose();       // dispose the picture box
                        if ((playerHealth + 30) <= 100)
                        {
                            playerHealth += 30; // increase the player health by 30
                        }
                        else
                        {
                            playerHealth = 100; // if the player's health + 30 is greater than 100, player' health = 100
                        }
                    }
                }

                if (x is PictureBox && (string)x.Tag == "superGun")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player intersects with the superGun
                    {
                        this.Controls.Remove(x);                // remove the gun on screen
                        ((PictureBox)x).Dispose();              // dispose the picture box
                        inventory.collectItem("SuperGun");      // get super gun
                        superGun.Image        = Properties.Resources.SuperGun;
                        LaserGunTimer.Enabled = true;
                    }
                }

                if (x is PictureBox && (string)x.Tag == "laserGun")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player intersects with laserGun
                    {
                        this.Controls.Remove(x);                // remove the gun on screen
                        ((PictureBox)x).Dispose();              // dispose the picture box
                        inventory.collectItem("LaserGun");      // get laser gun
                        laserGun.Image = Properties.Resources.LaserGun;
                    }
                }

                if (x is PictureBox && (string)x.Tag == "shieldIcon")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // If the player intersects with the shield
                    {
                        SoundPlayer pickMedic = new SoundPlayer("../../Sound/pickMedic.wav");
                        pickMedic.Play();          // play the pickMedic sound
                        this.Controls.Remove(x);   // Remove the shield on the screen
                        ((PictureBox)x).Dispose(); // Dispose the picture box
                        playerShield = 100;        // If the player grab the shield then it will restore the player's shield until full
                    }
                }

                if (x is PictureBox && (string)x.Tag == "grenades")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player intersects with grenade
                    {
                        this.Controls.Remove(x);                // remove the grenade on screen
                        grenadeList.Remove((PictureBox)x);      // remove the medic from the grenade list
                        ((PictureBox)x).Dispose();              // dispose the picture box
                        inventory.collectItem("Grenades");      // get grenades
                        grenade.Image      = Properties.Resources.grenade;
                        grenade.Visible    = true;
                        isGrenadeCollected = true;
                    }
                }

                if (x is PictureBox && (string)x.Tag == "zombie")
                {
                    if (player.Bounds.IntersectsWith(x.Bounds)) // if the player's intersects with zombie
                    {
                        // If the value of player's shield is still more than 0 thus decrease the shield's value before reducing the player's health
                        if (playerShield > 0)
                        {
                            playerShield -= 1;
                        }
                        else
                        {
                            playerHealth -= 1;           //player's health minus by 1
                            if ((playerHealth - 1) == 0) //if player's heath minus 1 is equal 0, player's health equal 0
                            {
                                playerHealth = 0;
                            }
                        }
                    }
                    // let the zombies chase the player
                    if (x.Left > player.Left)
                    {
                        x.Left -= zombieSpeed;
                        ((PictureBox)x).Image = Properties.Resources.zleft;
                    }
                    if (x.Left < player.Left)
                    {
                        x.Left += zombieSpeed;
                        ((PictureBox)x).Image = Properties.Resources.zright;
                    }
                    if (x.Top > player.Top)
                    {
                        x.Top -= zombieSpeed;
                        ((PictureBox)x).Image = Properties.Resources.zup;
                    }
                    if (x.Top < player.Top)
                    {
                        x.Top += zombieSpeed;
                        ((PictureBox)x).Image = Properties.Resources.zdown;
                    }
                }

                foreach (Control j in this.Controls)
                {
                    if (j is PictureBox && (string)j.Tag == "bullet" && x is PictureBox && (string)x.Tag == "zombie")
                    {
                        if (x.Bounds.IntersectsWith(j.Bounds))   // if bullets are intersect with zombies
                        {
                            score++;                             // score plus 1
                            tempScore++;
                            this.Controls.Remove(j);             // remove the bullets on screen
                            ((PictureBox)j).Dispose();           // dispose the picture box
                            this.Controls.Remove(x);             // remove the zombie on screen
                            ((PictureBox)x).Dispose();           // dispose the picture box
                            zombiesList.Remove(((PictureBox)x)); // remove the zombie from the list
                            MakeZombies();                       // create the zombies
                        }
                    }
                }

                //throw grenade
                foreach (Control k in this.Controls)
                {
                    if (k is PictureBox && (string)k.Tag == "explosion" && x is PictureBox && (string)x.Tag == "zombie")
                    {
                        Rectangle intersectionArea = Rectangle.Intersect(x.Bounds, k.Bounds);
                        while (intersectionArea.IsEmpty == false) // if grenade range are intersect with zombies
                        {
                            score++;                              // score plus 1
                            tempScore++;
                            this.Controls.Remove(x);              // remove the zombie on screen
                            ((PictureBox)x).Dispose();            // dispose the picture box
                            zombiesList.Remove(((PictureBox)x));  // remove the zombie from the list
                            MakeZombies();                        // create the zombies
                            break;
                        }
                        while (intersectionArea.IsEmpty && explosionTime == 1)
                        {
                            this.Controls.Remove(k);   // remove the grenade on screen
                            ((PictureBox)k).Dispose(); // dispose the picture box
                            explosionTimer.Stop();
                            explosionTime = 0;
                            break;
                        }
                    }
                }
            }
        }
Example #2
0
        private void RestartGame()
        {
            // This if-else statements are for to display the character based on the chosen character by the user
            // The default value of choseCharacter variable is man character
            if (ChooseCharacter.chosenCharacter == "woman")
            {
                player.Image = Properties.Resources.p2_up; // set player faces up
            }
            else
            {
                player.Image = Properties.Resources.up; // set player faces up
            }

            foreach (PictureBox i in zombiesList)
            {
                this.Controls.Remove(i); // remove all the zombies on the screen
            }
            zombiesList.Clear();         // clear the zombieList

            foreach (PictureBox i in medicList)
            {
                this.Controls.Remove(i); // remove all the medics on the screen
            }
            medicList.Clear();           // clear the medicList

            foreach (PictureBox i in gunList)
            {
                this.Controls.Remove(i); // remove all the guns on the screen
            }
            gunList.Clear();

            foreach (PictureBox i in shieldList)
            {
                this.Controls.Remove(i); // Remove all the shields on the screen
            }
            shieldList.Clear();

            foreach (PictureBox i in grenadeList)
            {
                this.Controls.Remove(i); // remove all the grenade on the screen
            }
            grenadeList.Clear();         // clear the grenadeList

            for (int i = 0; i < 3; i++)
            {
                MakeZombies(); // create 3 zombies on the screen
            }

            goup         = false;
            godown       = false;
            goleft       = false;
            goright      = false;
            gameOver     = false;
            playerHealth = 100;
            score        = 0;
            ammo         = 10;
            level        = 1;
            tempScore    = 0;

            superGun.Image = null;
            laserGun.Image = null;

            Array.Clear(inventory.bag, 0, inventory.bag.Length);
            inventory.numberOfItem = 0;

            GameTimer.Start();
            MedicTimer.Start();
            SuperGunTimer.Start();
            shieldTimer.Start();
            GrenadeTimer.Start();
        }