Musical Shooter – Enemies Testing

I spent some time yesterday implementing a class of enemies so that I can spawn a number of them each with random sizes, start poistion, direction and speed. Once the basic behaviours were coded it was then a case of collision checking for:
– the edges of the screen so that they bounce back
– the bullets
– the player sprite

To check for collisions with the edge of the screen is pretty straightforward – you simply compare the enemy’s x,y location with either 0 or the width and height properties to see if they are outside of these bounds. If they are, just invert the velocity property which controls the direction of travel.

To check for bullets is a little trickier as you need to check every enemy against every bullet that is currently being generated. The bullets are created within an array so we can step through each element in the array and compare their respective locations to see if the distance between them falls below a defined threshold. If there is a collision, I’m drawing an ellipse over the enemy location to provide a visual indication to the player.
For every collision, the enemy’s damage variable is incremented and when it reaches a threshold the enemy can be destroyed and a sound is played- this is actually constrained to only occur on a musical interval:

if(damage > 25 && frameCount % 32 == 0 && impactCheck == true)

the impactCheck variable is being used so that the enemy is only destroyed if the player is actually shooting it as without this it felt a little odd.
When the enemy is destroyed the totalDead variable is incremented and when all the enemies have been destroyed, the game is finished.

To check for collisions with the player sprite is pretty straightforward, you just need to take the enemy diameter and player sprite diameter into account. If there is a collision, I’m drawing an ellipse over the player sprite to provide a visual indication.
If there is a collision, the playerHealth variable is reduced, this is used to determine the size of the ‘health’ bar in the top left of the screen – there’s nothing else going on yet, I was just seeing what it would look like.

Here’s a video of what I’ve got so far:

 

The next things on the list are:
– to destroy bullets on collision with an enemy
– to refine the aiming mechanism as its quite clumsy at the moment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s