These teachpacks provide a variety of functions for manipulating and animating graphical images. To use them, you'll need to
This should be done once on your home computer, and you'll never have to do it again. You shouldn't need to do this at all on a classroom computer.
Welcome to DrScheme, version 4.1 [3m].If the two teachpacks are listed in the other order, that doesn't matter.
Language: Beginning Student custom.
Teachpacks: 371-tiles.ss and 371-sb-world.ss
When you start DrScheme, you can tell it you want to use these added functions, and it'll remember that for the next time you start DrScheme on the same computer... unless somebody else using the same computer tells it not to use this teachpack, in which case you'll need to go through these steps again.
Go to the "Language" menu in DrScheme and choose "Add
Teachpack", and you should see both 371-tiles.ss
and
371-sb-world.ss
on the right-hand side. Click one of
these, then "OK". Repeat with the other teachpack.
Click the "Run" button. You should see a message saying
"Teachpack: 371-tiles.ss and 371-sb-world.ss" in the
interactions window. The next time you start DrScheme,
it should remember this unless you choose "Language->Clear Teachpack"
to remove the teachpack.
You can copy-and-paste images from a Web browser into DrScheme and use them just like any other value: you can store them in a variable, etc.
includes several useful functions for manipulating images:
image-above: image image ... -> imageCombines two or more images into one, putting the first above the second (above the third, ...).
image-above-align-right: image image ... -> imageJust like
image-above
, but lines up the right-hand edges of
all the images.image-above-align-left: image image ... -> imageJust like
image-above
, but lines up the left-hand edges of
all the images.image-beside: image image ... -> imageCombines two or more images into one, putting the first to the left of the second (to the left of the third, ...).
image-beside-align-top: image image ... -> imageJust like
image-beside
, but lines up the top edges of
all the images.image-beside-align-bottom: image image ... -> imageJust like
image-beside
, but lines up the bottom edges of
all the images.reflect-vert : image -> imageflips an image top-to-bottom.
reflect-horiz : image -> imageflips an image left-to-right.
reflect-main-diag : image -> imageflips an image top-right to bottom-left.
reflect-other-diag : image -> imageflips an image top-left to bottom-right.
rotate-cw : image -> imagerotates an image 90 degrees clockwise.
rotate-ccw : image -> imagerotates an image 90 degrees counter-clockwise.
rotate-180 : image -> imagerotates an image 180 degrees.
The 371-tiles.ss
teachpack also includes the
image.ss
teachpack (which comes standard with DrScheme, so
you don't need to worry about installing it).
This provides a variety of other functions useful
for building and manipulating images in DrScheme. To learn about them,
start DrScheme, choose "Help -> Help Desk", and search for
"image.ss".
371-sb-world.ss
teachpackprovides several functions related to animation:
run-animation : number(width) number(height) world(init) number(tick interval) handler ... -> true
This is the most important function in the teachpack. You give it the width and height of your desired animation window, the initial world, how many seconds between "ticks", and possibly some "handlers", and it will run an animation for you. For details, see Chapter 7 of my textbook.
run-saveable-animation: ...
Just like run-animation
, except that it records the
sequence of events and allows you to save it as an animated GIF picture.
place-image : image number(x) number(y) scene -> scene
Puts the specified image at the specified location on the background scene. Note that a "scene" is a special kind of image: any scene can be used as an image, but not every image can be used as a scene. The following two functions provide scenes easily.
nw:rectangle : number(width) number(height) string("outline" or "solid") string(color-name) -> scene
Just like rectangle
, but the result is a scene
(i.e. its "pinhole" is in the top left corner).
empty-scene : number(width) number(height) -> scene
Equivalent to calling (nw:rectangle width height "outline"
"black")
: it creates a white box, outlined in black, of the
specified width and height. The easiest way to create a scene.
When you run an animation, you can provide handler functions
to tell the animation how to do various things: how to redraw the screen
from the model, how to change the model at every clock tick, how to
change the model when the user uses the mouse or keyboard, etc.
Once you've written a function to be used as a handler, you specify what
kind of handler it is by calling on-redraw
,
on-tick
, on-mouse
, on-key
, or
stop-when
, and passing the result to
run-animation
.
You need to write a function with the contract
model -> image
and install it with on-redraw
.
This function will be called every time the model changes.
You need to write a function with the contract
model -> model
and install it with on-tick
.
This function will be called every time the clock ticks (at
the interval you specified in the fourth argument to
run-animation
).
You need to write a function with the contract
model number(x) number(y) symbol(event) -> model
and install it with on-mouse
.
This function will be called every time the user clicks, releases,
or moves the mouse.
You need to write a function with the contract
model char-or-symbol -> model
and install it with on-key
.
This function will be called every time the user presses or releases
a key on the keyboard.
An animation can choose when to stop itself. In the 371-sb-world
teachpack, this job is up to the tick, mouse, and key handlers: any one
of these can (presumably inside a conditional) call the
end-of-time
function with a value (typically a string); the
animation will end, and that value will be printed out.
(define (dot-at-y y) (place-image (circle 3 "solid" "red") 50 y (empty-scene 100 100))) (define (sub1-or-bust y) (cond [(> y 0) (sub1 y)] [else (end-of-time "That's all, folks!")])) (run-animation 100 100 100 .1 (on-redraw dot-at-y) (on-tick sub1-or-bust))