Exemple #1
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            //this function is called every time the timer tool is changed (20 milseconds)
            //put player ship and score in place
            player.Top      = Height - 100;
            score_label.Top = Height - 75;
            //player moving left and right unless out of bounds
            if (goleft && player.Left > 0)
            {
                player.Left -= playerSpeed;
            }
            else if (goright && player.Left + 75 < Width)
            {
                player.Left += playerSpeed;
            }

            // end of player moving left and right

            //check for enemy is on form. enemies moving on the form
            foreach (Control x in Controls)
            {
                if (x is PictureBox && (string)x.Tag == "invader")
                {
                    if (((PictureBox)x).Bounds.IntersectsWith(player.Bounds))
                    {
                        gameOver();
                    }
                    if (((PictureBox)x).Left + speed > Width - 51)
                    {
                        change_side(false);
                        break;
                    }
                    else if (((PictureBox)x).Left - speed < 1)
                    {
                        change_side(true);
                    }
                }
            }
            //move on screen
            foreach (Control x in Controls)
            {
                if (x is PictureBox && (string)x.Tag == "invader")
                {
                    ((PictureBox)x).Left += speed;
                }
            }
            // end of enemies moving on the form

            //animating the bullets and removing them when the have left the scene
            foreach (Control y in Controls)
            {
                if (y is PictureBox && (string)y.Tag == "bullet")
                {
                    y.Top -= 20;
                    if (((PictureBox)y).Top < 10)// a bit lower than top screen
                    {
                        Controls.Remove(y);
                    }
                }
            }
            // end of animating the bullets.

            // bullet and enemy collision start
            foreach (Control i in Controls)
            {
                foreach (Control j in Controls)
                {
                    if (i is PictureBox && (string)i.Tag == "invader")
                    {
                        if (j is PictureBox && (string)j.Tag == "bullet")
                        {
                            if (i.Bounds.IntersectsWith(j.Bounds))
                            {
                                score++;
                                Controls.Remove(i);
                                Controls.Remove(j);
                            }
                        }
                    }
                }
            }
            // bullet and enemy collision end

            // keeping and showing score
            score_label.Text = "Score : " + score;
            if (score > totalEnemies - 1)
            {
                gameOver();
                //MessageBox.Show("You Win");
            }
            // end of keeping and showing score.
        }