Power-Ups: Basics
Now you will add some power-ups. There currently three different power-ups:
- Ball Catch: When this power-up is active, the ball is caught by the paddle and it is released only when the spacebar is pressed.
- Multi-Ball: This power-up spawns another ball at the current paddle location.
- Paddle Size: This power-up increases the width of the paddle. It doesn't stack, so there's only one size increase that's possible.
Power-ups will have a chance of spawning every time a block is destroyed. They will then fall vertically, and if the player picks up the power-up with the paddle before it goes offscreen, they'll get that power-up.
When the player loses a life (which you'll redefine as having 0 balls left), any active power-ups reset.
Before you actually implement the custom functionality for each power-up, you need to add the code that randomly spawns one of the power-ups on occasion.
First, create a new class that inherits from GameObject called PowerUp. In this class, you will want to add an enum that defines the three different types of power-ups (much as you did for BlockColor).
Based on the power-up enum passed to the constructor, set it to either powerup_c (for ball catch), powerup_b (for multi-ball), or powerup_p (for paddle size).
Next, have the PowerUp.Update function increase the Y position based on speed and deltaTime. This will cause the PowerUp to fall once it is spawned.
Once you have this PowerUp class, you need to add a list of power-ups in Game1.cs, much as you did for the blocks.
Next, create a function in Game1 called SpawnPowerUp, which takes a Vector2 called position as a parameter. Don't put anything in this function just yet.
In order to randomly spawn a power-up, you need a pseudo-random number generator. C# has one built in: the Random class.
Add an instance of one as a member variable to Game1:
Random random = new Random();
This also automatically sets the seed based on the clock, so you don't have to pass in a seed like you might in C/C++.
Next, create a double that represents the probability that a power-up spawns when you destroy a block. It has to be a double, not a float.
For now, use 0.2, which means that when you destroy a block, a power-up will spawn 20% of the time.
Then go to the code that destroys blocks. When one is destroyed, use random.NextDouble to get a random double that's between 0.0 and 1.0.
If this double is less than your power-up chance, you should then call SpawnPowerUp and pass in the position of the block that was just destroyed.
Now you can implement SpawnPowerUp. In here, use random.Next(3) to get an integer from 0-2. Take this int, convert it to the PowerUpType enum, create the appropriate PowerUp, set its position to the position that was passed into SpawnPowerUp, and add the PowerUp to the list of power-ups.
Next, you need to add the code that loops through the list of power-ups and draws them, which goes in Game1.Draw.
Likewise, you need to add the code that loops through the list of power-ups and calls Update on each of them; you should do this before the CheckCollisions call in Game1.Update.
Finally, you need to detect whether any of the power-ups have moved offscreen. One way to do this is to create a public bool in the PowerUp class that tracks whether a power-up should be removed. Then in PowerUp.Update, check whether the Y position is off the screen; if it is, set that bool to true.
You can then loop through all the power-ups in a function in Game1 and remove any that have this bool set.
Remember that you can't use a foreach while you're removing elements from the list, so one solution is to use a regular for loop in reverse.
It will look something like this:
for (int i = powerups.Count - 1; i >= 0; i--) { if (powerups[i].shouldRemove) { powerups.RemoveAt(i); } }
Once you implement this code, you should notice power-ups spawning occasionally when you destroy a block, which will look like the following figure:
Remember that if you get stuck, you can always check the code provided at the end of the article.
Power-Ups: Collision
Now you need to actually allow the player to pick up power-ups. To do this, you want to check whether the bounding rectangle of the paddle intersects with the bounding rectangle of a power-up. If it does, you grant that power-up capability and mark it for removal from the world.
MonoGame has a Rectangle struct you can use for this purpose. First, in GameObject add a property called BoundingRect that returns the Rectangle bounds of the object.
The Rectangle constructor takes four parameters: the x value of the top-left corner, the y value of the top-left corner, the width, and the height.
In any event, this BoundingRect property should return a newly constructed Rectangle in the get function.
Now that you have this property, you can add a new CheckForPowerups function in Game1 that checks for collision with the power-ups. You should call this function before the call to RemovePowerUps.
In CheckForPowerups, you first need to get the BoundingRect for the paddle. Next, loop through the power-up list and grab the BoundingRect for each power-up.
To check whether one Rectangle intersects with another, there's a handy Intersects member function that returns true if the Rectangle intersects with the one you pass in.
So when the paddle intersects with a power-up, have it call an ActivatePowerUp function, which takes in the PowerUp to be activated as a parameter.
In this new ActivatePowerUp function, for now just set the removal flag to true and play the "powerup" sound effect. Once it is working, you can then implement the actual power-ups.
Power-Up: Ball Catch
The first power-up you'll implement is the ball catch. If the player gets this power-up, the ball doesn't bounce off the paddle. Instead, it gets caught and the player can release the ball using the spacebar.
To support this, you need to add a bool to Ball.cs that tracks whether the ball is caught—if it is, you don't want to change the position in Ball.Update.
Next, you need to add code in Game1 that tracks whether the ball catch power-up is active. If it is, when the ball collides with the paddle, set the caught bool on the ball to true.
If the player presses the spacebar while the power-up is active, release the ball (you could also do this check in Ball.cs). Don't forget that this power-up should be disabled in LoseLife.
You also will want to move the ball/paddle collision counter from Game1.cs into Ball.cs. This way, you can decrement the counter only if the ball isn't already caught (and the counter is > 0, of course). This change also will be necessary to support multi-ball.
There's one last thing you need to do, If you move the paddle while a ball is caught, you need to also move the ball. One way to handle this is to save the X position of the paddle before you call paddle.Update on it and then get the X position after paddle.Update. Apply this difference to the ball's position if it is caught.
I also suggest making a slight improvement to the catch behavior. Set the direction of the ball when it's caught to be either (-0.707f, -0.707f) or (0.707f, -0.707f), depending on whether it was caught on the left or right half of the paddle. This way, players know which way the ball will travel when they release it.
Power-Up: Paddle Size
This power-up is the easiest to implement. All you have to do when it's activated is to change the paddle's texture to paddle_long. In LoseLife, change the texture back to the regular paddle. Just add a member function to Paddle that can swap the correct texture in.
As long as you made all the paddle-related logic relative to width/height, you don't have to change anything else.
Power-Up: Multi-Ball
When the multi-ball power-up is activated, you want to spawn a new ball at the current location of the paddle. To do this, you have to change some of the code in Game1.cs, which currently is designed for only one ball.
First, create a list of balls, much as you did for the blocks/power-ups. Next, make a SpawnBall function that creates a ball based on an offset from the paddle position and adds this ball to the list.
Change Game1.Update/Game1.Draw to loop through all the balls in the list.
You also need to change the CheckForCollisions function so that it takes in a particular ball as a parameter and call the function on each ball during Update.
Because there can now be more than one ball, you don't want to automatically call LoseLife if any particular ball goes off the screen. Instead, mark the ball for removal and then later remove it in a RemoveBalls function, which you should call after you've looped through all the balls in Game1.Update.
If you end up with zero balls in the list, you should then call LoseLife and spawn a new ball.
Once you implement it, you'll be able to have multiple balls up at once, as shown in the following figure:
After adding all these power-ups, the game is definitely a lot more dynamic. However, it's still boring that there isn't a score, lives, or more than one level. You'll add support for these features in the fourth and final article in this tutorial series.