Esempio n. 1
0
        private void CreateAsteroids(Random rnd)
        {
            if (rnd.Next(0, 50) == 3)
            {

                var newAst = new Asteroid();
                newAst.picture = Content.Load<Texture2D>("Asteroide");
                newAst.placeHolder = newAst.picture.Bounds;
                newAst.placeHolder.X = 750;
                newAst.placeHolder.Y = rnd.Next(0, 500);
                asteroids.Add(newAst);

            }
        }
Esempio n. 2
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            // create and add asteroid every tickInterval ticks
            if (tickCount % tickInterval == 0)
            {
                Asteroid asteroid = asteroidFactory.CreateAsteroid();
                asteroids.Add(asteroid);
                Controls.Add(asteroid.Sprite);
            }

            // asteroids
            for (int i = asteroids.Count - 1; i >= 0; i--)
            {
                // both players died
                if (player1.Lives == 0 && player2.Lives == 0)
                {
                    numberOfLivesLabel1.Text = "Game over, press enter to restart...";
                    Controls.Add(numberOfLivesLabel1);
                    timer.Enabled = false;
                    break;
                }

                asteroids[i].Move();

                // collision with player 1
                if (asteroids[i].Sprite.Bounds.IntersectsWith(player1.Sprite.Bounds) && player1.Lives != 0)
                {
                    if (player1.Lives > 1)
                    {
                        // clear objects on screen
                        for (int j = asteroids.Count - 1; j >= 0; j--)
                        {
                            Controls.Remove(asteroids[j].Sprite);       // clear all asteroids images
                        }
                        Controls.Remove(numberOfLivesLabel);            // clear Number of Lives Label
                        Controls.Remove(scoreLabel);                    // clear Score label
                        Controls.Remove(player1.Sprite);                // clear Player 1
                        asteroids.Clear();                              // clear asteroid objects

                        player1.Lives--;                                // reduce Player 1 live
                        player1.Reposition(this.Width, this.Height, 1); // reposition Player 1

                        numberOfLivesLabel.Text = String.Format("Lives = {0}", player1.Lives);
                        Controls.Add(numberOfLivesLabel);
                        Controls.Add(scoreLabel);
                        Controls.Add(player1.Sprite);
                        break;
                    }
                    else
                    {
                        for (int j = asteroids.Count - 1; j >= 0; j--)
                        {
                            Controls.Remove(asteroids[j].Sprite);
                        }
                        Controls.Remove(numberOfLivesLabel);
                        Controls.Remove(scoreLabel);
                        Controls.Remove(player1.Sprite);
                        asteroids.Clear();
                        player1.Lives--;
                        break;
                    }
                }

                // collision with player 2
                if (asteroids[i].Sprite.Bounds.IntersectsWith(player2.Sprite.Bounds) && player2.Lives != 0)
                {
                    if (player2.Lives > 1)
                    {
                        for (int j = asteroids.Count - 1; j >= 0; j--)
                        {
                            Controls.Remove(asteroids[j].Sprite);
                        }
                        Controls.Remove(numberOfLivesLabel1);
                        Controls.Remove(scoreLabel1);
                        Controls.Remove(player2.Sprite);
                        asteroids.Clear();

                        player2.Lives--;
                        player2.Reposition(this.Width, this.Height, 2);

                        numberOfLivesLabel1.Text = String.Format("Lives = {0}", player2.Lives);
                        Controls.Add(numberOfLivesLabel1);
                        Controls.Add(scoreLabel1);
                        Controls.Add(player2.Sprite);
                        break;
                    }
                    else
                    {
                        for (int j = asteroids.Count - 1; j >= 0; j--)
                        {
                            Controls.Remove(asteroids[j].Sprite);
                        }
                        Controls.Remove(numberOfLivesLabel1);
                        Controls.Remove(scoreLabel1);
                        Controls.Remove(player2.Sprite);
                        asteroids.Clear();
                        player2.Lives--;
                        break;
                    }
                }



                // remove asteroids once out of screen
                if (asteroids[i].IsOutOfScreen(this.Height))
                {
                    Controls.Remove(asteroids[i].Sprite);
                    asteroids.RemoveAt(i);
                }
            }

            // missles
            for (int j = missles.Count - 1; j >= 0; j--)
            {
                missles[j].Move();

                // remove missle once out of screen
                if (missles[j].IsOutOfScreen())
                {
                    Controls.Remove(missles[j].Sprite);
                    missles.RemoveAt(j);
                }
            }

            // check if missle collides with asteroid
            for (int i = asteroids.Count - 1; i >= 0; i--)
            {
                for (int j = missles.Count - 1; j >= 0; j--)
                {
                    if (i > asteroids.Count - 1 || j > missles.Count - 1)
                    {
                        break;
                    }

                    if (asteroids[i].Sprite.Bounds.IntersectsWith(missles[j].Sprite.Bounds))
                    {
                        if (missles[j].PlayerNo == 1)
                        {
                            scoreLabel.Text = String.Format("Score = {0:D2}", ++player1.Score);
                        }
                        else
                        {
                            scoreLabel1.Text = String.Format("Score = {0:D2}", ++player2.Score);
                        }

                        Controls.Remove(missles[j].Sprite);
                        missles.RemoveAt(j);

                        Controls.Remove(asteroids[i].Sprite);
                        asteroids.RemoveAt(i);
                    }
                }
            }
            tickCount++;
        }