Reward for Balloon Pop
Since we have a working system to send balloons, the next step is to reward the user when they pop balloons.
In Balloons.pde, we check if balloons have positive hp when they are drawn in drawBalloon(). Here, we have an if statement that checks if
balloon[hp] <= 0, which means that the balloon has been popped (it has less than zero hp). We can add our method handleBalloonPop(), which will add money to the user’s balance once they pop a balloon. Here are the two methods
increaseBalance() in Currency.pde.
The second method,
increaseBalance() takes a parameter which is the amount that is to be added to the user’s balance. For popping balloons, the user gets $20, which is set in the global variables section,
handleBalloonPop(), this method is called whenever a balloon is popped, so we just call
increaseBalance() with the
rewardPerBalloon in order to reward the user.
All the towers now have a price! Whenever the user picks up a tower, they will need to purchase it using their balance. Since the
handlePickUp() method is called whenever the user picks up a tower, we will use it to charge the player when purchasing a tower and prevent them from picking up towers without sufficient money. When purchasing a tower, we first need to check if the player has enough money to purchase the tower. This can be done by comparing the player’s current balance to the cost of the tower. The costs of the towers are stored in the towerPrice array.
If the balance is greater than or equal to the cost of the tower, then allow the user to pick up the tower. The cost of that tower will be deducted from the player’s balance once they drop the tower onto a valid location (not in the trash).
If the balance is less than the cost of the tower, we will not allow the tower to be picked up.
Insufficient Funds Warnings
To make it easier for the player, we will warn them whenever they try to purchase a tower that they don’t have enough money for. To check if this is happening, we will first see if the user is clicking within the pick-up box. We will check the
mousePressed variable and
withinBounds() method to do so. Then we will check to see if the user has enough money, if they wanted to purchase the tower. We will use the
hasSufficientFunds() method that we had previously implemented to see if they do. If both of these conditions are true, then we should colour the text red to warn the user that they have insufficient funds.
attemptingToPurchaseTowerWithoutFunds(), to check if the user doesn’t have enough money. This will return a boolean value (either
False) to us, so we can put this in an if statement. If you check back above, it returns true when the user doesn’t have funds, so we change the fill colour to towerErrorColour (global variable in
Towers.pde when towers are placed illegally) in the if block. Otherwise, we change the text back to the default black text. We can then display the balance, using the