Currency System


Currency Template Code

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.

See code

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 handleBalloonPop() and increaseBalance() in Currency.pde.

See code

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, rewardPerBalloon.

See code

For handleBalloonPop(), this method is called whenever a balloon is popped, so we just call increaseBalance() with the rewardPerBalloon in order to reward the user.

Purchasing Towers

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.

See code for checking funds

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).

See code for purchasing tower

If the balance is less than the cost of the tower, we will not allow the tower to be picked up.

See code for pick-up handling

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.

See code for funds warnings

Displaying Funds

See code
First we check if they are trying to buy a tower without funds, since then we make the text turn red to warn the user that they do not have enough funds. We call the method that was written before, attemptingToPurchaseTowerWithoutFunds(), to check if the user doesn’t have enough money. This will return a boolean value (either True or 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 text() method.