Space Invaders – Making the enemies shoot

One of my task was to make the space invaders shot at the player. We used block code in the Arcanoid engine as our enemies and the ball code for the projectiles the player can shoot. I thought the code in the ball could be reused for the enemy’s projectile so I created Bullet.cpp and Bullet.h using largely the same structure as the Ball.cpp and Ball.h.

I then used the code in the GameState for making the ball (or projectile) follow the player and activate when pressing down a key, and then used that in a bullet class. Now just one enemy would shoot, the first in the row. To make the enemies I needed to use a random function with the Bullet entity to randomize which enemy that would shoot.

I had some problems to create the random generator but Anthon Fredriksson helped me.

std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution distribution(0, m_enemies.size() -1);
int random_enemy_index = distribution(generator);

Block* block = static_cast(m_enemies[random_enemy_index]);

About Sebastian Ringvold

2014  Programming