home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)
lists
handling lists
frogger
Due: Oct.27 (Fri) 14:00, for 10% additional credit
Accepted until Oct.28 (Sat)(!) 23:59
This homework uses racket to complete the frogger program
started in
frogger.
You'll presumably include parts of your hw04
(or, the posted solution);
please clearly mark where the new portion
of your solution begins.
Follow all steps of the-design-recipe—the design recipe: final version, including templates and unit-tests.
For a function's purpose-statement, you might be able to copy/paste from this hw assignment.
Tests are worth approximately half the points
List-processing functions should have at least three tests (lists of length 0, 1, and many).
However, if a struct merely contains a list (like world below),
and it calls a helper function on that field,
then you don't need to test different structs containing lists of varying lengths;
your unit-tests for the helper already exercised that aspect.
- (8pts) Our frogger program will need to deal with a multiple trucks.
-
Give a data definition for a list-of-trucks,
examples of the data,
and a template
for any function which takes a list-of-trucks as input
-
Write the function
move-trucks (note the plural “s”),
which maps move-truck onto a list of trucks:
; move-trucks: list-of-trucks -> list-of-trucks
; Return a list of all the trucks, where each has been moved.
|
Test cases are required, of course.
-
Write the function
draw-truck,
which takes in one, single truck and a background image
,
and returns that image with an image representing the truck on placed appropriately top of it.
Note:A rectangle is a perfectly fine way to draw a truck! You're certainly free to make it look
fancier, but it's not worth more credit.
And of course, if you use an image-file from somewhere, make sure you have the right to do so, and cite the source.
Use place-image,
since that crops the result appropriately.
-
Write the function
draw-trucks,
which takes in a list of trucks and a scene,
and returns a new scene with every truck in the list overlayed onto it.
-
(7pts) Hmm, I guess we might also need to represent a frog.
-
Give a data-definition / examples / template for a frog.
Hint:
Don't include an image in the structure.
Instead, just include some way to indicate which way the frog is facing.
I suggest using
"left", "up", etc.;
since these strings also happen to be key-events, this will make
for some convenient code
for frog-handle-key below.
- Write
draw-frog : frog, image → image
which draws a frog onto the provided image.
I recommend having
a helper-function
get-frog-image : string -> image
which, given a string indicating a frog’s direction,
returns an appropriate image.
(If you do, include tests for it.)
The image library provides
rotate
and
scale.
Performance note: if you are using a bitmap for the frog-image,
rotateing it every time you draw can be expensive.
You may want to pre-compute the four images you'll need.
-
Write draw-prince(ss), similar to draw-truck and draw-frog.
- Write
frog-handle-key : frog, key-event
→ frog.
(You'll need to (require 2htdp/universe), to get the function
key=?.)
This function captures how the frog should react when a key is pressed.
Note that it does not deal with any time passing (tick-events) — only key-events.
(Keypresses can cause the frog to re-face, or to move — or both, depending on what you
want to do.)
-
(9pts)
We need to represent an entire state of the game; we'll call this a “world”.
-
Give the data-definition, examples, and template for worlds.
-
Write the function update-world : world → world
which returns a new world one “tick” later from the input world.
-
Then, write world-handle-key : world, keypress → world
which returns a new world based on handling a single keypress event.
-
Finally, write draw-world : world → image.
(You might want to use
empty-scene
to create the initial background-image that will get passed to a helper function.)
Of course, each of these functions will be straightforward,
since they'll mostly call the helpers already written.
(including test cases).
- (20pts) More trucks, and miscellania:
-
Write the function
truck-off-screen?,
which returns whether or not a given truck is entirely off
the size of the image you use in draw-world.
Note that this function neither consumes nor returns images;
however it may use numbers (or better: named constants)
which are also used inside draw-world.
To help you, you can use
the following code
for
overlap?,
which returns whether or not two rectangles overlap.
(There's no reason you couldn't write
overlap?
yourself;
after careful test cases the code becomes straightforward.
Howewver, this homework is long enough as-is.
-
Write the function
; remove-off-screen-trucks : list-of-trucks -> list-of-trucks
; Return a list containing only those trucks are not off-screen.
|
Write the function replenish-truck : list-of-trucks -> list-of-trucks,
which sometimes adds new trucks to the given list.
You can use whatever criterion you like,
but I recommend:
If a list's length falls below some threshold,
then add (exactly one) new truck.
(You can call length : list -> number;
we wrote “my-length” in class,
but “length” is also built-in.)
Note that you can, if you want, create new trucks with some random attributes;
see the built-in function random.
…
However, this makes unit-testing problematic, since you don't
exactly know the expected result.
So for this problem, your tests might instead sometimes only check that
the length of the result is as expected.
(Of course, if you do know the exact expected
result for certain inputs -- e.g. when
the list is already longer than some threshold --
then do check for that full result.)
-
Modify update-world
so that it
(a) moves trucks,
then
(b) removes trucks which are off the screen,
and finally
(c) replenishes trucks.
Add one test case for this.
-
Write truck-hitting-frog? : truck frog -> boolean.
You can use overlap? on the truck's and frog's “bounding box”.
Note that this function neither consumes nor returns images;
test cases are required.
-
Write trucks-hitting-frog? : list-of-trucks frog -> boolean,
which returns whether any truck in the list is hitting the given frog.
-
Write game-over? : world -> boolean.
The game is over when the frog is hit by a truck,
or the frog has reached the princess.
-
Modify our old update-prince(ss) so that s/he teleports to a random location,
after being in the same spot long enough.
- [1pt] If you evaluate
(big-bang some-initial-world
[on-tick update-world]
[on-draw draw-world]
[on-key update-world-handle-key]
[stop-when game-over?])
|
you'll be playing frogger.
(Only do this after you pass all your tests for individual functions;
no debugging is necessary here, if you've properly tested your functions.)
Optional / for fun only:
If you download and then (require "student-extras.rkt"),
then you get the function play-sound-then : string, any/c → any/c
which takes in the name of a sound file and any other value,
starts that sound-file playing, and then just returns the other value.
E.g. (play-sound-then "/full/path/to/some-file.wav" (sqrt 16))
will play a sound, then return 4.
(Some free-to-use sound files can be found at, say,
mediacollege.com
or
partnersinrhyme.com;
be sure to site your sources, as per their licenses, and common courtesy.)
home—lectures—recipe—exams—hws—D2L—breeze (snow day; distance)