This practice will be retired on May 31, 2020.

**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Start a free Courses trial

to watch this video

Find the distance between two players on a mock tower defense game

All right, this exercise requires a tiny bit of set up to properly motivate you. 0:00 So imagine you're building a Tower Defense Game. 0:05 This game has a map that's essentially a two-dimensional coordinate system 0:07 like a graph with X and Y points. 0:11 There are towers at various spots and the location of a tower is marked with 0:14 a coordinate, which is represented as a combination of an X and a Y value. 0:17 So in the playground here, you've been provided with four values, towerX and 0:22 towerY, which corresponds to the location of the tower. 0:27 And then enemyX and enemyY, which corresponds to the location of the enemy. 0:30 Now in this game, towers have a defined range. 0:36 So for a game to work, 0:38 we need to write logic to determine how far the enemy is from the tower. 0:40 So to find the distance between two points on a coordinate space, 0:45 we use the distance formula, which has the following stats. 0:49 So first we subtract the distance between the X coordinates of the Tower and 0:53 the Enemy and then we square it. 0:57 We then do the same with the Y values. 0:59 We add these resulting values together and then we obtain the square root. 1:01 So in order to do this correctly, you either need to write these in steps or 1:06 apply the right operators with the correct precedents into account. 1:10 Now at this point, you don't know how to obtain the square root 1:15 which is this last step here, but that's okay. 1:17 At the very bottom, I have added this code for you. 1:19 So here in this line of code, for you to get the square root, 1:23 just enter the value for the sum of the squares inside these parenthesis. 1:28 So replace this one. 1:32 Okay, good luck, and in the next video, let's go over the solution. 1:34

You need to sign up for Treehouse in order to download course files.

Sign up