This practice will be retired on May 31, 2020.
Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Find the distance between two players on a mock tower defense game

0:00
All right, this exercise requires a tiny bit of set up to properly motivate you.

0:05
So imagine you're building a Tower Defense Game.

0:07
This game has a map that's essentially a twodimensional coordinate system

0:11
like a graph with X and Y points.

0:14
There are towers at various spots and the location of a tower is marked with

0:17
a coordinate, which is represented as a combination of an X and a Y value.

0:22
So in the playground here, you've been provided with four values, towerX and

0:27
towerY, which corresponds to the location of the tower.

0:30
And then enemyX and enemyY, which corresponds to the location of the enemy.

0:36
Now in this game, towers have a defined range.

0:38
So for a game to work,

0:40
we need to write logic to determine how far the enemy is from the tower.

0:45
So to find the distance between two points on a coordinate space,

0:49
we use the distance formula, which has the following stats.

0:53
So first we subtract the distance between the X coordinates of the Tower and

0:57
the Enemy and then we square it.

0:59
We then do the same with the Y values.

1:01
We add these resulting values together and then we obtain the square root.

1:06
So in order to do this correctly, you either need to write these in steps or

1:10
apply the right operators with the correct precedents into account.

1:15
Now at this point, you don't know how to obtain the square root

1:17
which is this last step here, but that's okay.

1:19
At the very bottom, I have added this code for you.

1:23
So here in this line of code, for you to get the square root,

1:28
just enter the value for the sum of the squares inside these parenthesis.

1:32
So replace this one.

1:34
Okay, good luck, and in the next video, let's go over the solution.
You need to sign up for Treehouse in order to download course files.
Sign up