예제 #1
0
        protected override void Update(GameTime gameTime)
        {
            //Escape - quits the game
            if (Keyboard.GetState().GetPressedKeys().Contains <Keys>(Keys.Escape))
            {
                this.Exit();
            }

            explosionEffect.Update(gameTime);
            if (!lost)
            {
                //Space - fires a missile
                if (Keyboard.GetState().GetPressedKeys().Contains <Keys>(Keys.Space))
                {
                    fireMissile((int)gameTime.TotalGameTime.TotalMilliseconds);
                }

                ship.Update(gameTime, GraphicsDevice.Viewport.Bounds);

                List <int> asteroidsToRemove = new List <int>();
                List <int> missilesToRemove  = new List <int>();

                //Update missiles and remove ones that have gone off screen
                float x, y;
                for (int i = 0; i < projectiles.Count; i++)
                {
                    projectiles[i].Update(gameTime, GraphicsDevice.Viewport.Bounds);
                    x = projectiles[i].Position.X;
                    y = projectiles[i].Position.Y;
                    if (x < -missileScreenPadding || x > WIDTH + missileScreenPadding ||
                        y < -missileScreenPadding || y > HEIGHT + missileScreenPadding)
                    {
                        missilesToRemove.Add(i);
                    }
                }



                //Update asteroids and handle collision with ship and missiles
                bool done = false;
                for (int i = 0; i < asteroids.Count && !done; i++)
                {
                    asteroids[i].Update(gameTime, GraphicsDevice.Viewport.Bounds);
                    if (ship.CollisionRect.Intersects(asteroids[i].CollisionRect))
                    {
                        lost = true;
                        explode.Play();
                        explosionEffect.AddExplosion(ship.Position, 75, 1000, 3500, gameTime);
                        asteroidsToRemove.Add(i);
                    }
                    for (int j = 0; j < projectiles.Count && !done; j++)
                    {
                        projectiles[j].Update(gameTime, GraphicsDevice.Viewport.Bounds);
                        if (asteroids[i].CollisionRect.Intersects(projectiles[j].CollisionRect))
                        {
                            done = true;
                            gunshot.Play();
                            asteroidsToRemove.Add(i);
                            missilesToRemove.Add(j);
                            explosionEffect.AddExplosion(asteroids[i].Position, 20, 100, 1000, gameTime);
                        }
                    }
                }

                //Remove asteroids and missiles that have collided
                for (int i = asteroidsToRemove.Count - 1; i >= 0; i--)
                {
                    asteroids.RemoveAt(asteroidsToRemove[i]);
                }
                for (int i = missilesToRemove.Count - 1; i >= 0; i--)
                {
                    projectiles.RemoveAt(missilesToRemove[i]);
                }
            }
            base.Update(gameTime);
        }
예제 #2
0
        protected override void Update(GameTime gameTime)
        {
            //Escape - quits the game
            if (Keyboard.GetState().GetPressedKeys().Contains <Keys>(Keys.Escape))
            {
                this.Exit();
            }

            explosionEffect.Update(gameTime);
            if (!lost)
            {
                //Space - fires a missile
                if (Keyboard.GetState().GetPressedKeys().Contains <Keys>(Keys.Space))
                {
                    fireMissile((int)gameTime.TotalGameTime.TotalMilliseconds);
                }

                ship.Update(gameTime, GraphicsDevice.Viewport.Bounds);
                int        index = 0;
                List <int> ids   = new List <int>();
                foreach (ProjectileSprite ps in projectiles)
                {
                    ps.Update(gameTime, GraphicsDevice.Viewport.Bounds);
                    if (ps.Position.X < -missile.Height * MISSILE_SCALE * 2 || ps.Position.X > WIDTH + missile.Height * MISSILE_SCALE * 2 ||
                        ps.Position.Y < -missile.Height * MISSILE_SCALE * 2 || ps.Position.Y > HEIGHT + missile.Height * MISSILE_SCALE * 2)
                    {
                        ids.Add(index);
                    }
                    index++;
                }
                for (int i = ids.Count - 1; i >= 0; i--)
                {
                    projectiles.RemoveAt(ids[i]);
                }

                //Check collision between ship/missile and asteroid
                int d = 0;
                foreach (BouncingSprite bs in asteroids)
                {
                    bs.Update(gameTime, GraphicsDevice.Viewport.Bounds);
                    if (ship.CollisionRect.Intersects(bs.CollisionRect))
                    {
                        lost = true;
                        explode.Play();
                        explosionEffect.AddExplosion(ship.Position, 75, 1500, 3500, gameTime);
                        break;
                    }
                    d++;
                }

                List <int> hitAsteroids = new List <int>();
                if (lost)
                {
                    hitAsteroids.Add(d);
                }
                List <int> hitMissiles = new List <int>();
                for (int i = 0; i < asteroids.Count; i++)
                {
                    for (int j = 0; j < projectiles.Count; j++)
                    {
                        if (asteroids[i].CollisionRect.Intersects(projectiles[j].CollisionRect))
                        {
                            gunshot.Play();
                            hitAsteroids.Add(i);
                            hitMissiles.Add(j);
                            explosionEffect.AddExplosion(asteroids[i].Position, 20, 100, 1000, gameTime);
                        }
                    }
                }
                for (int i = hitAsteroids.Count - 1; i >= 0; i--)
                {
                    asteroids.RemoveAt(hitAsteroids[i]);
                }

                for (int i = hitMissiles.Count - 1; i >= 0; i--)
                {
                    projectiles.RemoveAt(hitMissiles[i]);
                }
            }

            base.Update(gameTime);
        }