protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // update our virtual thumbsticks VirtualThumbsticks.Update(); // countdown until we spawn more enemies spawnTimer -= gameTime.ElapsedGameTime; if (spawnTimer <= TimeSpan.Zero) { // we spawn 1-3 enemies per spawn int numToSpawn = rand.Next(1, 3); for (int i = 0; i < numToSpawn; i++) { // create the enemy EnemyShip enemy = new EnemyShip(Content.Load <Texture2D>("alien")); // target the player's ship enemy.Player = player; // we randomly pick either the left or right side of the screen // to place the enemy if (rand.Next() % 2 == 0) { enemy.Position.X = -worldWidth / 2f - (graphicsWidthHalf + 10); } else { enemy.Position.X = worldWidth / 2f + (graphicsWidthHalf + 10); } // we randomly pick either the top or bottom side of the screen // to place the enemy if (rand.Next() % 2 == 0) { enemy.Position.Y = -worldHeight / 2f - (graphicsHeightHalf + 10); } else { enemy.Position.Y = worldHeight / 2f + (graphicsHeightHalf + 10); } // add the enemy to our list enemies.Add(enemy); } // reset our timer spawnTimer = spawnRate; } // update the player player.Update(gameTime); // update all the enemies foreach (var enemy in enemies) { enemy.Update(gameTime); } // create a couple lists to hold the bullets and enemies // we need to remove due to collisions List <Bullet> bulletsToRemove = new List <Bullet>(); List <EnemyShip> enemiesToRemove = new List <EnemyShip>(); // figure out what bullets and enemies are colliding foreach (var b in player.Bullets) { foreach (var e in enemies) { // check if the enemy contains the bullet's position if (e.ContainsPoint(b.Position)) { // add the bullet and enemy to the lists to be removed bulletsToRemove.Add(b); enemiesToRemove.Add(e); // break the inner loop so bullets only collide // with one enemy break; } } } // remove all marked bullets and enemies foreach (var b in bulletsToRemove) { player.Bullets.Remove(b); } foreach (var e in enemiesToRemove) { enemies.Remove(e); } base.Update(gameTime); }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // update our virtual thumbsticks VirtualThumbsticks.Update(); // countdown until we spawn more enemies spawnTimer -= gameTime.ElapsedGameTime; if (spawnTimer <= TimeSpan.Zero) { // we spawn 1-3 enemies per spawn int numToSpawn = rand.Next(1, 3); for (int i = 0; i < numToSpawn; i++) { // create the enemy EnemyShip enemy = new EnemyShip(Content.Load<Texture2D>("alien")); // target the player's ship enemy.Player = player; // we randomly pick either the left or right side of the screen // to place the enemy if (rand.Next() % 2 == 0) { enemy.Position.X = -worldWidth / 2f - (graphicsWidthHalf + 10); } else { enemy.Position.X = worldWidth / 2f + (graphicsWidthHalf + 10); } // we randomly pick either the top or bottom side of the screen // to place the enemy if (rand.Next() % 2 == 0) { enemy.Position.Y = -worldHeight / 2f - (graphicsHeightHalf + 10); } else { enemy.Position.Y = worldHeight / 2f + (graphicsHeightHalf + 10); } // add the enemy to our list enemies.Add(enemy); } // reset our timer spawnTimer = spawnRate; } // update the player player.Update(gameTime); // update all the enemies foreach (var enemy in enemies) enemy.Update(gameTime); // create a couple lists to hold the bullets and enemies // we need to remove due to collisions List<Bullet> bulletsToRemove = new List<Bullet>(); List<EnemyShip> enemiesToRemove = new List<EnemyShip>(); // figure out what bullets and enemies are colliding foreach (var b in player.Bullets) { foreach (var e in enemies) { // check if the enemy contains the bullet's position if (e.ContainsPoint(b.Position)) { // add the bullet and enemy to the lists to be removed bulletsToRemove.Add(b); enemiesToRemove.Add(e); // break the inner loop so bullets only collide // with one enemy break; } } } // remove all marked bullets and enemies foreach (var b in bulletsToRemove) player.Bullets.Remove(b); foreach (var e in enemiesToRemove) enemies.Remove(e); base.Update(gameTime); }