If your game is working and…​

  • both the Danger and Target return to the screen

  • your Player moves up and down with the arrow keys

you have all the tools you need to begin this challenge!

# update-danger :: Number, Number ‑> Number # takes in danger’s x and y-coordinates and produces the next x-coordinate

The update-danger function only moves our DANGER left or right…​ because it doesn’t do anything with the y-coordinate!

Suppose we wanted to write a new function, update-danger-2 that moves the DANGER diagonally…​

1 What is the update danger function doing with the second input in its Domain?

2 What, if anything, will have to change about the Range if we want to get our character moving diagonally?

Since an (x, y) coordinate has two Numbers, one idea might be to write the contract this way:

# update-danger-2 :: Number Number ‑> Number Number # take in danger’s x- and y-coordinate, and produces the next x- and next y-coordinate

…​But that contract breaks an important rule about functions:

Given an input, all functions must produce one output!

We need some way to package two Numbers together into a single value.

Fortunately, our programming language has another data type, called a Posn, which utilizes two Numbers to describe a single "position"!

We can make a Posn to represent the position (100, 200) with the following code: posn​(​100, 200​)

3 What expression will make a Posn representing the origin?

4 Write the contract for the posn function on the line below.

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 contact@BootstrapWorld.org.