(Also available in WeScheme)

Students use function composition and the distance formula to detect when characters in their games collision.

Lesson Goals

Students will be able to:

  • Explain how the distance formula is related to the Pythagorean theorem.

  • Write a function for the distance formula.

Student-Facing Lesson Goals

  • I can explain how the distance formula is connected to the Pythagorean theorem.

  • I can write a function that takes in 2 points and returns the distance between them.



the smallest unit that makes up a digital image. The more pixels, the more detailed an image or video can appear.

🔗Problem Decomposition Returns! 20 minutes


Students revisit problem decomposition - the idea of breaking down a complex problem into simpler pieces, solving those pieces separately, and then composig the solutions to solve the original..


"Problem Decomposition" is a powerful tool, which lets us break apart complex problems into simpler ones that we can solve, test, and then glue together into a complex solution.

Students may remember that there are two strategies for doing this:

  1. Top-Down: Describe the problem at a high level, then fill in the details later

  2. Bottom-Up: Focus on the smaller parts that you’re sure of, then build them together to get the big picture


For the following complex word problem, have students first decide which strategy they want to use, and then apply the Design Recipe to build the functions they need.

A retractable flag pole starts out 24 inches tall, and can grow at a rate of 0.6in/sec. An elastic is tied to the top of the pole and anchored 200 inches from the base, forming a right triangle. Define functions that compute the height of the pole and the area of the triangle after a given number of seconds.

Have students complete the Top Down / Bottom Up worksheet, using Problem Decomposition and the Design Recipe to solve this problem!


  • Which strategy did students use?

  • Did they start out with one, and then switch to another?

🔗Collision Detection 20 minutes


Students once again see function composition at work, as they compose a simple inequality with the distance function they’ve created.


Knowing how far apart our characters are is the first step. We still need the computer to be asking: "True or False: is there a collision?"


Using Word Problem: is-collision, have students write a function that takes in two coordinate pairs (four numbers) of the PLAYER and a character ((px, py) and (cx, cy)), and returns true if they are within 50 pixels of each other.

Fix the is-collision function in your game file, and click "Run"!


  • You started by writing the distance function first, and then is-collision. Is this Top-Down or Bottom-Up decomposition?

  • Explicitly point out that this function is easy to write because we can re-use the distance function.

  • Connect this back to profit, revenue, cost and onscreen from the Problem Decomposition lesson.

🔗Additional Exercises:

For teachers who’ve already introduced your class to flags, Flag of Trinidad and Tobago Starter File makes use of Pythagorean Theorem and could make for an interesting connection to this lesson.

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting