Lab 3
About this lab
Your task is to explore and develop world programs. Use the examples we used in class (in “Files -> In-class examples” on Canvas) for reference; the documentation on the Universe Teachpack is also likely to be useful. is also a link to the book chapter there. Make sure to ask questions when you are stuck.
Lab tasks: a world with a falling object
For this task you start with the following world example:
humpty-dumpty.rkt. This provides a
skeleton for a simple world/big-bang
program that you need to
complete.
Your tasks are as follows:
- Read the description of the world above
main
to see what the world program is supposed to do and what the world state is. - Change
render
to place the marble ball at the x coordinate specified in the constantball-x-coord
and the y coordinate given by the world state (it starts at the top of the canvas). Note that thecheck-expect
tests indicate the correct behavior and provide a significant hint for the expression inrender
; make sure you uncomment them so you get the benefit of those tests when you run your file. You don’t need to implement the ball breaking as it reaches the ground. - Once you added rendering of the ball, you need to change the
fall
function to make the ball falling at the number of pixels per clock tick that’s given in the variablespeed
. Uncomment thecheck-expect
tests, fill in the right expression, and run the program to test. The ball doesn’t (for now) need to stop at the bottom of the canvas. - Change the
fall
function so that once the ball reaches the ground (theground-size
constant might be useful), it stops falling and remains at the same position. That requires makingfall
into anif
or acond
. Add one or two newcheck-expect
tests forfall
that check for appropriate behavior in this case. - Now you need to make it so that when one clicks on the ball, it
returns back to the top. In order to implement it, write two helper
functions,
distance
andwithin-radius?
. Make sure to read the signatures, the descriptions and the check-expects. Note that thewithin-radius?
helper function uses thedistance
. You might want to look up the formula for distance between two points. See the figure below for examples. - Write the
move-to-start
function usingwithin-radius?
as a helper function. Use the comments and tests formove-to-start
as a guide for what needs to be done. Make sure you uncomment thecheck-expect
tests. Also make sure you uncomment the line in the definition ofmain
(wherebig-bang
is called) that uses registersmove-to-start
as a mouse handler. Test your program. - Change the rendering so that once the ball
is on the ground, it displays the
broken-ball
image instead of theball
image; this will also require a conditional. You should also add at least one newcheck-expect
test torender
that tests this new functionality.
The following figure illustrates the ideas behind both distance
and
withing-radius?
:
Originally written by @elenam, with subsequent modifications by @NicMcPhee