For each of the following word problems, extract structure definitions for the data involved. You don't need to write tests for them, but be sure to test them informally by trying them out.
Remember structure definitions from class?
(define-struct name
(field ...))
Switch Roles! 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.
Use the design recipe when designing functions!
Create a function named which-chip that takes a number, and returns the corresponding image from the sequence above.
This can be done several ways... which one is best?
You should be able to drag the images from the webpage into DrRacket, once they are there you should know what to do. If not save them to the desktop, and use "Insert">"Insert Image..." from the DrRacket menu bar.
Write a data and structure definition for your world. You'll want to be able to know Chip's coordinates, the coordinates he is running to, and which frame of the animation he is currently on.
While you're at it, write a template for functions that take a world, and define a variable named world0 that is your initial world (start by assuming the user clicked in the center, and Chip is just offscreen).Write a function named move-chip that takes a world and returns a world; moving Chip to the left by some amount. (It looks like he's going pretty fast!)
This assumes he starts at the y-coordinate of his destination, and only has to go left.
If he gets to his destination, have him stop moving.
Switch Roles! Now, make Chip respond to mouse clicks. Write a clack function that takes a world, x and y coordinates, and a mouse event, and returns 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 ignore all mouse events except "button-down".
Write a function named draw-chip that takes a world and places the correct image (frame) of Chip at the correct position into an empty-scene of size 400x400 (remember the function you made before?)
Write a function named next-chip that takes a world and returns a world; incrementing the frame field and wrapping the number so it cannot be greater than 3.
The remainder function can be used for this:(remainder 1 4) ;; ==> 1 (remainder 2 4) ;; ==> 2 (remainder 5 4) ;; ==> 1 (remainder 6 4) ;; ==> 2
Write a function named tock that takes your world and calls move-chip and next-chip to return a completely updated world.
Hint: You want to nest the calls... how should it be done?Put all this together with a big-bang and see Chip the Cheap sheep run!
(big-bang world0 (on-draw draw-chip) (on-tick tock) (on-mouse clack))
If you still have extra time, try variations: maybe make a bouncing ball for Chip to chase.