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:
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 value 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 draw-chip that takes a world and places the image of Chip's current frame at his current coordinates into an empty-scene of size 400x400 (use your which-chip function!).
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.
Write a function named next-chip that takes a world and returns a new world incrementing the frame field and wrapping the number so it cannot be greater than 3.The remainder function can be used to wrap the number once it has been incremented. remainder takes two integers and returns the remainder of dividing the first by the second.
(remainder 1 4) ;; ==> 1 (remainder 2 4) ;; ==> 2 (remainder 3 4) ;; ==> 3 (remainder 4 4) ;; ==> 0
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. The new world will have the mouse's coordinates as Chip's new destination and Chip teleported offscreen (presumably at the same y-coordinate as his destination) so that he can run to the point the user clicked on.
Remember that mouse events are just strings. Be sure to ignore all mouse events except "button-down".
Put all this together with tock and big-bang to see Chip the Cheap Sheep run!
(define (tock world) (move-chip (next-chip world))) (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.