Time for action fix the script

We can fix our problem in any number of ways. We could replace the red stripe with a new sprite and make the game end when one of the balls touches the sprite. In the following exercise, we're going to test two conditions to determine if the game should end:

  1. Select the ball2 sprite from the list of sprites.
  2. Drag the touching color red block out of the wait until block and place it somewhere in the scripts area. We'll use it again in a moment.
  3. From the Operators palette, drag the and block into the wait until block.
  4. Drag the touching color red block into the first value of the and block.
  5. Now, we need a way to exclude the times when ball2 touches the red ball. From the Operators palette, drag the not block into the second value of the and block.
  6. Let's add the exclusion. From the Sensing palette, drag the touching block into the value of the not block.
  7. Select ball from the drop-down list. Our revised code looks like the following screenshot:
    Time for action fix the scriptTroll pongsecond level, adding
  8. Play the game to test the solution.
  9. If you're satisfied with this fix, duplicate it on the other ball sprite.

What just happened?

We previously had two conditions that gave us a red color, and that caused problems. As the balls flew around the stage, ball2 continually evaluated if it touched the color red. Now, if ball2 touched red, it also evaluated whether or not it is touching the other ball.

In human speak, we would say if the ball touched the color red and the other ball, do nothing; however, if the ball touched the color red while not touching the other ball, stop all scripts.

Using Boolean logic

When we use conditional statements, we can incorporate Boolean logic to determine whether or not we should run the code. The Boolean operators are the and block, the or block, and the not bock. They can all be found in the Operators palette.

If you want to ensure that two conditions are met, use the and block. If only one of two conditions need to be met at any given time, use the or block.

The not block checks whether a specified condition is met or not. And if the condition is not met, it returns a value of true. A true value triggers the conditional statement to run.

Pop quiz

  1. If you want to create a variable for a sprite's exclusive use, which of the following options do you select when creating the variable?
    • For all sprites
    • Not for this sprite
    • For all sprites but this one
    • For this sprite only
  2. Which of the following blocks is not a Boolean operator?
    • wait
    • or
    • and
    • not

Add levels

We programmed the logic to create levels. Now, let's add a visual indicator to count the levels.

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

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