The road ahead is like a marathon

Today’s post while short, might be interesting to you if you’re wondering about the bread and butter behind an Arkanoid game, collisions.  I’ve been a little sick today so I mainly focused on playing my game in its current state and coming up with a laundry list of things to add.  Some of these items possibly include: powerups, moving blocks, blocks that change tone or image after they have been hit, music, and sound effects etc.  There is only so much you can do with an Arkanoid game / clone, and yet to me it feels like a mountain.  I’ll be honest I’m taking courses and looking at guides to help me with the foundation of building my first mini game.  Although I’m shooting for a small size, seeing what I have to learn to accomplish all of this, move on to the next game, and eventually get enough skills so I can one day in the future make a game to sell is a little overwhelming.  A comparison might be, an extremely overweight person who is trying to train for a marathon and complete it in qualifying or record time, even though they have never run a 3k race before in their life.

So to keep up my spirits and move on, I celebrate the small victories.  No matter how trivial, no matter how long it took to do one thing, like get the blocks to destroy on collision.  Or yesterday’s problem: struggling for an hour wondering why your sprites weren’t rendering properly and coming out in different shades whenever you played it.  The answer was because the depth or Z-Depth of the sprites and my background was the same, so my background image would impact which sprites would change color whenever I moved them.  (Do’h!)

Collisions Make an Arkanoid

Today’s small victory was finally implementing the code to break the blocks in my game.  The code looks like this inside my brick sprite and i’ll explain it as best I can.  Keep in mind this is only a small piece of a much larger part of code, so some parts are missing.

void Start ( )

{

                timesHit = 0;

}

void CollisionEnter2D (Collision2D col)

{

                 timesHit++;

                 if (timesHit >= maxHits)

                     {

                       Destroy (gameObject);

                      }

}

So you might be wondering what is this mumbo jumbo above.  This is likely the most important code in Arkanoid that really makes the game.  It is located in my brick script attached all bricks I insert in the game.  We have the start function where we have an integer called timesHit that is set to 0.  Next we have another method or function titled CollisionEnter2D.  This is where the game senses that a collision has taken place between two objects.

In the method CollisionEnter2D you have an argument called Collision2D and a variable called col (not the most important in what I’m explaining, so don’t worry about it).  Inside the method you have the integer timesHit increasing by 1 every time a collision is detected.  Then we have an if statement where when the #of timesHit is greater than or equal to maxHits another integer, the object will destroy itself.  The gameObject being the current sprite containing the brick script.

What’s cool about this code?

What’s nice about this code is that it will apply to all objects you’ve included in your game that contain this script.  So instead of editing each individual object’s code, I just have to edit the script and it is applied to all objects that share the script.  Pretty cool!