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
mainto see what the world program is supposed to do and what the world state is. - Change
renderto place the marble ball at the x coordinate specified in the constantball-x-coordand the y coordinate given by the world state (it starts at the top of the canvas). Note that thecheck-expecttests 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
fallfunction to make the ball falling at the number of pixels per clock tick that’s given in the variablespeed. Uncomment thecheck-expecttests, 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
fallfunction so that once the ball reaches the ground (theground-sizeconstant might be useful), it stops falling and remains at the same position. That requires makingfallinto anifor acond. Add one or two newcheck-expecttests forfallthat 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,
distanceandwithin-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-startfunction usingwithin-radius?as a helper function. Use the comments and tests formove-to-startas a guide for what needs to be done. Make sure you uncomment thecheck-expecttests. Also make sure you uncomment the line in the definition ofmain(wherebig-bangis called) that uses registersmove-to-startas a mouse handler. Test your program. - Change the rendering so that once the ball
is on the ground, it displays the
broken-ballimage instead of theballimage; this will also require a conditional. You should also add at least one newcheck-expecttest torenderthat 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