In this exercise, you'll learn how to use DrScheme's world.ss teachpack to create interactive animations like this rocketship simulation.
Before you begin, enable the world.ss teachpack by clicking Add Teachpack... in DrScheme's Languages menu and selecting world.ss from the list of pre-installed teachpacks.
An animation comprises several parts:
A function world->scene that produces a graphical depiction of the world. The teachpack calls this function to redraw the world whenever it changes.
For this exercise, we choose a simple data definition for world:
a world is a number
This value will count the number of clock ticks since the animation's beginning. We will use this count to calculate the rocket's vertical displacement as a function of time, to simulate something like the effect of gravity.
Following the design recipe, write the following functions for use in your animation:
tock: number -> number. Given a number t, produces the successor of t (i.e., t+1). The teachpack will periodically call tock with the current world (the current tick count). tock's role in the animation is to produce the representation of "next" world, in which t+1 ticks have occurred.
displacement: number -> number. Calculates the rocketship's vertical position as a function of t clock ticks, using the formula 0.01*t2.
The constants world-width and world-height, which set the dimensions of the world.
render-world: number -> scene. Given the world, produces a scene (an image) in which appears at the vertical position given by the displacement function.
For render-world, use the teachpack functions empty-scene and place-image.
Configure the teachpack to call your tock and render-world functions by placing the following lines at the end of your program:
(on-tick-event tock)
(on-redraw render-world)
Now, the teachpark will transform the world according to tock each time its internal timer fires, using render-world to refresh the animation.
Finally, to see your animation in action, precede the lines above with this line:
(big-bang world-width world-height 1/30 0)
Next, we will make the rocketship respond to keyboard input by accelerating upward, which we'll model by subtracting 10 from the tick count representing the world. To transform the world according to key presses, we will make use the function
(on-key-event accel)
where accel is the name of a function with the contract
world key-event -> world
The teachpack calls the supplied function with the current world and a value representing the particular key pressed. This function's job is to produce the "next" world in response. (NB: For this exercise, the particular key pressed is irrelevant -- any key causes the rocket to accelerate, so the concrete definition of key-event is unimportant. Nevertheless, any function (in this case, accel) that you register with on-key-event must include the key-event parameter in its contract and header, even if your function ignores its value.)
Following the design recipe, write the function accel: number key-event -> number, which, given a world, produces a world in which the rocket has accelerated upward.
Next, wire it into your animation with (on-key-event accel).
At this point, the rocketship simulation exhibits two strange behaviors:
To fix the first behavior, write a function landed?: number -> boolean. Given the world's tick count, landed? determines whether the rocketship's position exceeds the height of the scene.
After landed? passes the tests you wrote for it, include the line
(stop-when landed?)
The second behavior occurs when rapid keyboard input pushes the tick count far below zero. To correct this, rewrite your accel function so that (accel t) evaluates to 0 for t < 10. Remember to update accel's purpose statement and examples/tests!
If you haven't already, follow the instructions in the couse webpage to install the handin software and create a handin account.
Hand in your working program as 'lab2' through the handin interface. Include your full name and userid in a comment at the top.
(Optional, but would be highly appreciated.) Include as a comment your feedback on this lab. Was it useful? Relevant to what you learnt in class? What would you change about it? Is there anything in particular that you would like to be included? Was the use of the animation teachpack fun, or confusing (or both)? Any feedback you can offer will help us in designing the next few labs. You can also e-mail one of us (sravana@cs. or gabri@cs.) with this information.