Read this page to learn how to submit your assignments electronically. Assignments will only be accepted electronically from this day forward! Repeat this from your laptop or desktop at home if you wish to submit from it.
In this lab, we will practice pair programming again. You will be working with your partner from last week. Like last time, each pair should only be working on one machine. Remember, in pair programming, one member of the team is the pilot and the other is the co-pilot. The pilot does the typing but the co-pilot drives the process. Even though the pilot is the only one typing, both partners should be active in trying to come up with solutions to the exercises. Make sure to switch roles when indicated in the lab!
In previous labs some students deleted their solutions for exercises after completing them—don't do this! It is common for exercises to make use of functions or templates defined in earlier exercises.
For each of the following problems, extract structure definitions for the data involved. You don't need to write tests for them, but do test them informally by trying them out.
Remember structure definitions from class?
(define-struct name
(field ...))
1
to it. (This will be useful on the animal's birthday).
From now on, we will always use the design recipe when designing programs.
In the following exercises, give names to each example so you can reuse them later.
mouse-click
to react to mouse
events. It consumes a World, two numbers (x and y
coordinates), and a MouseEvent as described in on-mouse
. When the MouseEvent is "button-down"
, this function should return a new World
where the second posn (the red circle) has been moved to the position of
the mouse click, and the first posn (the blue circle) remains unchanged. On
any other mouse event, the original World is returned unchanged.
Have you been writing tests?
tick
to react to clock events.
The purpose of the function is to gradually bring the two circles together.
The function consumes a World and produces a new World where each of the
coordinates of the first posn are increased or decreased by one so that
they get closer to the coordinates of the second posn. For example if the
original World is (1,3),(5,1) then your function should return the new
World (2,2),(5,1).
world-draw
that consumes a World
and returns a 300 × 300 scene with a solid blue circle of radius 15
at the position represented by the first posn and a solid red circle of
radius 10 at the position represented by the second posn. The red circle
should appear on top of the blue circle if they overlap.
"red"
, "yellow"
, "green"
. Modify world-draw
to
use this symbol when drawing the circle for the mouse. Write a function
cycle-color
to cycle through the colors ("red"
→ "yellow"
, "yellow"
→ "green"
, "green"
→ "red"
). Use this
function and modify mouse-click
to cycle the color
when the user clicks.
Some of the TAs (oddly, they've asked to remain anonymous) are working on a game that they call Chip, the Cheap Sheep. So far, they've put together a few frames of animation for it:
0 | 1 | 2 | 3 | |||
Your goal is to create a simple proof-of-concept game engine: Chip will run from offscreen to the point the user clicks on.
which-chip
that takes a number
and returns the corresponding image from the sequence above. (You can
either drag the images from this page into DrRacket, or save them and use
Insert > Insert Image... from the DrRacket menu bar.)
This can be done several ways... which do you think is best?
world0
that is your initial
world. Start as if the user clicked in the center and Chip is just
offscreen.
move-chip
that takes a World and
returns a World, moving Chip to the left by some amount. (It looks like
he's running pretty fast!)
This assumes he starts at the y coordinate of his destination, and
only has to go left. If gets to his destination, have him stop moving.
mouse-click
function that, in response to a mouse event,
creates a new World, with the mouse coordinates as Chip's new destination,
and with Chip teleported offscreen (presumably at the same y
coordinate as his destination), so that he can run to the point the user
clicked on. Be sure to only react to the mouse events "button-down"
.
draw-chip
that draws the correct
frame of Chip into an empty scene of size 400 × 400.
next-chip
that increments the
current frame in the world, wrapping the number so it stays within the
range 0–3:
remainder
function helpful.
tick
that takes your World and
calls both move-chip
and next-chip
to update the World.
big-bang
and see Chip
the Cheap Sheep run!
(big-bang world0 (on-tick tick .1) (to-draw draw-chip) (on-mouse mouse-click))
If you have extra time, play around. Make a bouncing ball for Chip to chase. Be creative.