Time for my favorite part of Monday – Another article improving my TriangleShooter game! Last week, I promised you that I would spawn some more enemies, and that I would add in some other enemy types to make it more visually appealing. The great news is, both of those things are pretty easy. We’ll get started with the code base from last week, so you can grab that from my SkyDrive share.

We can start by adding in some additional enemy types. To begin with, we’ll drag and drop the rest of the Enemies from the Enemies file from SkyDrive into the Content project with the first enemy. We’ll then create some variables to hold the textures.

Textures for Enemy
  1. Texture2D txEnemy3;
  2. Texture2D txEnemy4;
  3. Texture2D txEnemy5;
  4. Texture2D txEnemy6;

We’ll then load them from the content project in the LoadContent method.

Loading the Enemy Textures
  1. txEnemy3 = Content.Load<Texture2D>("Enemy-3");
  2. txEnemy4 = Content.Load<Texture2D>("Enemy-4");
  3. txEnemy5 = Content.Load<Texture2D>("Enemy-5");
  4. txEnemy6 = Content.Load<Texture2D>("Enemy-6");

We can try this out by changing the lines further down in LoadContent to make use of the new textures.

Creating Diverse Enemies
  1. enemies.Add(new Enemy() { Avatar = txEnemy3, Position = new Vector2(600f, 200f), Rotation = 0f, Speed = 3f });
  2. enemies.Add(new Enemy() { Avatar = txEnemy4, Position = new Vector2(600f, 300f), Rotation = 0f, Speed = 3.5f });
  3. enemies.Add(new Enemy() { Avatar = txEnemy5, Position = new Vector2(600f, 400f), Rotation = 0f, Speed = 4f });

image

Next up, we need to start spawning some more enemies. What we’re looking to do here is to bring in new enemies every so often. The faster you spawn the enemies, the more difficult the game would be, so you can play with the amount of time between spawns. In order to track them, though, we’ll need a variable to track how long we have left until the next spawn. Since we’ve got some different enemy types, we may as well add in some randomization, so we’ll create a variable to handle randomness as well. As always, we’ll drop the variable up at the top of the game file.

Spawn Timer and Randomization
  1. TimeSpan timeToSpawn;
  2.  
  3. Random random;

And then we initialize them down in Initalize()

Initializing Spawn and Random
  1. timeToSpawn = new TimeSpan(0, 0, 0, 0, 1000);
  2.  
  3. random = new Random();

This sets up timeToSpawn as one second. We’ll count down until this reaches zero, then spawn another enemy. Luckily, there’s a easy way to get elapsed game time in the Update Method with the gameTime argument. We’ll subtract the elapsed game time from the timeToSpawn variable, then check to see if it is less than or equal to zero. If it is, we create a new enemy and add it to the enemies list. To add variety, we can pick a random edge, and spawn the enemy somewhere along it so that it doesn’t just always come from the same spot. Finally, we’ll reset the timeToSpawn so that we can spawn another. All of this will go in the Update method after we check the enemy collision.

Spawning More Enemies
  1. timeToSpawn -= gameTime.ElapsedGameTime;
  2.  
  3. if (timeToSpawn < TimeSpan.Zero)
  4. {
  5.     Vector2 spawnPosition;
  6.     switch (random.Next(4))
  7.     {
  8.         case 0:
  9.             spawnPosition = new Vector2(0, random.Next(480));
  10.             break;
  11.         case 1:
  12.             spawnPosition = new Vector2(800, random.Next(480));
  13.             break;
  14.         case 2:
  15.             spawnPosition = new Vector2(random.Next(800), 0);
  16.             break;
  17.         case 3:
  18.             spawnPosition = new Vector2(random.Next(800), 480);
  19.             break;
  20.         default:
  21.             spawnPosition = Vector2.Zero;
  22.             break;
  23.     }
  24.  
  25.     switch (random.Next(4))
  26.     {
  27.         case 0:
  28.             enemies.Add(new Enemy() { Position = spawnPosition, Rotation = 0f, Avatar = txEnemy3, Speed = 3f });
  29.             break;
  30.         case 1:
  31.             enemies.Add(new Enemy() { Position = spawnPosition, Rotation = 0f, Avatar = txEnemy4, Speed = 3.5f });
  32.             break;
  33.         case 2:
  34.             enemies.Add(new Enemy() { Position = spawnPosition, Rotation = 0f, Avatar = txEnemy5, Speed = 4f });
  35.             break;
  36.         case 3:
  37.             enemies.Add(new Enemy() { Position = spawnPosition, Rotation = 0f, Avatar = txEnemy6, Speed = 4.5f });
  38.             break;
  39.         default:
  40.             break;
  41.     }
  42.  
  43.     timeToSpawn = new TimeSpan(0, 0, 0, 0, random.Next(500) + 250);
  44. }

image

There’s just so many of them!

Next week, we’ll add in shooting! Until this point, we were just a triangle moving around, but now we’ll finally be a triangle that shoots!

Download the latest version of the source code.