CMSC 15100: Lab 2

Due Friday, Oct 9 at 10pm.

In this exercise, you'll learn how to use DrScheme's world.ss teachpack to create interactive animations like this rocketship simulation.

Enabling world.ss

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.

Animation overview

An animation comprises several parts:

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.

Animation helper functions

Following the design recipe, write the following functions for use in your animation:

For render-world, use the teachpack functions empty-scene and place-image.

Wiring up the animation

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)

Making it interactive

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).

Edge cases

At this point, the rocketship simulation exhibits two strange behaviors:

6.1 Rocketship landing

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?)

6.2 Negative tick counts

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!

Submitting your work

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.


This lab was adapated from previous assignments designed by Casey Klein and Robby Findler.