Menacing Bloons
In this chapter, we'll add some bloons for you to pop. We will use the Pythagorean Theorem to measure distances and tell whether your big spiky ball is touching a bloon.
Add Bloons
Let's lay down some bloons to pop.
Create a new layer called Bloons. Arrange it underneath the Big Spiky Ball layer.

Now add various bloons all around the level.

Script Bloons
Add a new script to one of your Bloons. Edit the script, and name it something useful like "Bloon".
We're going to measure the distance to the big spiky ball. If it's less than a certain amount, this bloon will pop.
Get Big Spiky Ball
First, we will need a reference to the big spiky ball.
- Create a
When the level startsevent block.- Add a new property called
ballusing aSet instance ballproperty block. - Set
ballto afirst instance by tag "Ball"sensing block.
- Add a new property called
Why use
When the level startsinstead ofWhen created, as we did for the big spiky ball? Well, we don't know which order game objects will be created. If we tried to get the ball before it was created, our code would fail. We start the level only when everything has been properly created. This is a good time to look for things, because we know they must exist.Remember we said the tag "Ball" was important? This is where we use the tag to find the ball. If it's misspelled, our code will fail. Be sure to use the same capitalization.
Get XY Distances
Now we need to measure the distances between the big spiky ball and the bloon in the X and Y axes.
Luckily, we know the X and Y positions of each.
- Create a
Constantlyevent block.- Add a new variable called
dx(it means "difference in X") using aSet number dxvariable block. - Create a couple of loose
x position of myselfblocks.- Replace one
myselfwithball.
- Replace one
- Set
dxtox position of ball-x position of myself. - Do the same with
dy, making sure to change toy positionin both blocks.
- Add a new variable called

We now know the X and Y distances. But how do we get the straight line distance? We use Pythagoras.
Get Hypotenuse Distance
The shortest distance between two points is a straight line. The length of that line can be derived from the X and Y sides of a right-angle triangle, using the Pythagorean Theorem.
- Still in the
Constantlyevent block... - Add a new variable called
distanceusing aSet number distancevariable block.- Add a
square rootoperator block. - Add a
+operator block.- In the first field of the addition block, add a
^operator block, and set its second field to2. (^means "to the power of", so^2means "squared".) - Set the first field to
dx(thus "dx squared"). - Duplicate the power block and its contents. Change
dxtody. Add this to the addition operator block.
- In the first field of the addition block, add a
- Add a

That's it! This is the Pythagorean Theorem in action. Now you know the distance.
Check for Poppage
Finally, if the distance is short enough, we know the big spiky ball is close to the bloon. Bloons pop when they're close to big spiky balls.
What's a good distance? Today we'll use the width of the bloon. It's a decent approximation.
We can assume the bloon and the big spiky ball are of similar radius. When two objects touch, the distance between centers is equal to the two radii added together. If they're identical, two radii equals the diameter - or the width.
Let's check this now.
- Still in the
Constantlyevent block... - Add an
if... docontrol flow block.- Set the condition to
distance<Actual width of myself. - Set the
doto thedestroy myselfcontrol flow block.
- Set the condition to

Double check your work - you might be finished.
Script The Other Bloons
Don't forget to apply your new script to all the other bloons! Select it in the sidebar, and click or drag over all the other bloons.
Victory
Save and play your game. Are your bloons popping? If so, congratulations! You've successfully used the Pythagorean Theorem to measure distances, and used that to make game logic decisions.
If things still aren't working, go back and check your scripts. You're nearly there!
To see more of the bloons, check out our pro resource Bloons Trigonometry Defense. You'll learn about trigonometry, use Pythagoras, and write your own tower defense game. With monkeys.
You can also try your hand at 4 extra challenges. Just keep reading for more advanced activities...