You have all the tools you need to begin this challenge if:

  • your game is working

  • both the Danger and Target return to the screen

  • your Player moves up and down with the arrow keys

# update-danger :: Number -> Number # consumes danger’s x-coordinate and produces the next x-coordinate

The update-danger function only moves our DANGER left or right.

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

1 What, if anything will have to change about the Domain?

2 What, if anything, will have to change about the Range?

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 # consumes 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, Pyret has another data type, called a Posn.
The Posn datatype 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, 1738598, 2031479, and 1501927). 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.