Creating levels

Computer games typically have multiple levels where the difficulty of playing the game progressively increases. This is what makes a game fun, challenging, and even somewhat addictive. We want to incorporate levels in our game as well.

One simple way to make the game play harder and harder is to increase the speed of the ball as the player continues to score points. You can create whatever tiers you like, but for the purpose of this tutorial, we will create a simple tier system: whenever a player scores 5 points, the player has completed a level. Again, to keep it simple, we will not pause game play (as is common in most computer games) when a level is completed. The player will automatically move on to the next level, which will also increase the speed of the ball.

To figure out whether a level change is needed or not, we will code the app to constantly check the value of score. Each time the score is incremented, the app will check to see whether it is a multiple of 5 (for example, score = 5, 10, 15, 20, and so on). If the score is indeed a multiple of 5, it will mean that the player has scored another 5 points and a level change should occur. To change the level, we will simply increase the speed a little bit.

As explained earlier in this chapter, the event, Ball1.EdgeReached, contains the code that increments the score: the set global score to block. Whenever the score increases, the label will update to display the new score (the previous score plus 1). As you can see in the blocks shown in the following screenshot, the block immediately after the score-increment block is the set Score_Level_Label.text to block to update the score displayed.

Now, we will add an if/then block right after the set Score_Level_Label.text block. Do you remember where to find the if/then block?

Creating levels

Now, let's think about the condition that we want to insert in the empty socket next to if. We want to check and see whether the score is a multiple of 5 (if it is a multiple of 5, then the remainder will be 0). In order to do so, we need to complete the following steps:

  1. Get the current value of the score.
  2. Divide its value by 5 and calculate the remainder.
  3. See whether the remainder is equal to 0.

To achieve these three subtasks, we will first get the get global score block just like we did when we used this block to increment the score. This block can be select from the Variables blocks or can be copied from your current set of blocks. This completes task 1.

Next, we will get the remainder of the block from under the Math blocks. This step is not as obvious. When you go to the Math blocks, you will not see any remainder block. Instead, select the modulo of block, as shown in the following screenshot:

Creating levels

Click on the downward-facing triangle to the right of modulo of and select remainder of from the drop-down list. Modulo, remainder, and quotient are different mathematical operations related to division. Hence, they all belong to the same block. Place the get global score block within the first slot of the remainder of block. Since we want to calculate the remainder of the score when divided by 5, place number 5 in the second slot of the remainder of block (that is, insert a Math number block and change the value from 0 to 5). This completes step 2.

Finally, we want to check whether this remainder is equal to 0. If it is equal to 0, then it is time to increase the level (speed). If it is not, the level (speed) will remain the same. To check for the equality of numbers, we will need an equal to block from the Math blocks. Plug the remainder of block (and its accompanying get global score / 5 blocks) that we created in step 2 into the left side of the equal to block and the number 0 into the right side. The following screenshot shows the completed steps plugged to the if statement:

Creating levels

Blocks placed within the then part of an if/then statement are executed only when the condition plugged to the if statement is true. In this case, when the score is a multiple of 5 (that is, the whole remainder equals 0 and the if block evaluates to be true), we would want to increase the speed. We need two blocks from the Ball1 drawer. The first block, Ball1.Speed, gives us the current value of Ball1's speed property. The second one, set Ball1.Speed, lets us change the speed.

Creating levels

Since we want to increase the speed relative to the current speed, we will use both these blocks by:

  1. Using Ball1.Speed to get the current speed.
  2. Increasing the Ball1.Speed value by a small amount.
  3. Using the result to set the new speed.

To complete these steps, drag an add block from the Math blocks and attach it to the set Ball1.Speed block. Insert the Ball1.Speed block into the opening on the left-hand side of the plus sign and a number Math block set to 2 (the small amount that we are increasing the speed by) in the opening on the right-hand side of the plus sign. This completes tasks 1, 2, and 3. Finally, we will place the block within the then block, as shown in the following screenshot:

Creating levels

To summarize, whenever the user scores a point, the new score will be calculated. Then, the app will check to see whether the new score is a multiple of 5 or not. If it is indeed a multiple of 5, the app will increase the speed by a little bit to make the game harder. If the score is not a multiple of 5, nothing will change; the speed will remain the same.

We have completed coding the leveling part of our app! Can you think of what is still missing? We need to display the new level in the label!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset