1 Enabling world.ss
2 Animation overview
3 Animation helper functions
4 Wiring up the animation
5 Making it interactive
6 Edge cases
6.1 Rocketship landing
6.2 Negative tick counts
7 Submitting your work
Version: 4.1.1

CMSC 15100: Lab 1

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

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

2 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’ll use this count to calculate the rocket’s vertical displacement as a function of time, to simulate something like the effect of gravity.

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

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

where world-width and world-height are the dimensions of the scene produced by your render-world function. Use define to give names to these values, rather than repeating the values here and in the render-world function; e.g.,

  (define world-width 300)

5 Making it interactive

Next, we’ll make the rocketship respond to keyboard input by accelerating upward, which we’ll model by subtracting ten from the tick count representing the world. To transform the world according to key presses, include the line

  (on-key-event handler)

where handler 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. 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, but nevertheless, any function you register with on-key-event must include the unused key-event parameter in its contract and header, even if your function ignores its key-event parameter.

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

6 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 that, given the world’s tick count, 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?)

at the end of your program to instruct the teachpack to end the simulation when the rocket reaches the ground.

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!

7 Submitting your work

If you haven’t done so already, follow the instructions in the Software section of the CMSC 15100 webpage to install the handin software and create a handin account.

The handin process checks that the program you submit includes definitions for all the functions that the exercise asks you to write and, furthermore, that your definitions have the correct number of parameters. Before submitting your working rocketship program, take a few minutes to submit a few programs that you know are faulty, so you’ll recognize handin’s cryptic error messages if you encounter them in the wild:

Finally, handin your working program. If you completed this exercise with a partner, be sure to include both your names in a comment appearing at the top of your program.