home—lectures—exams—hws—breeze (snow day)
hw-ec-astr-finish
finishing asteroids
higher order functions
Due ...
In this assignment, we'll finish off the asteroids game (by adding bullets
and detecting collisions), using higher-order functions like
map, filter, and foldl as
appropriate.
All non-drawing functions require at least two test cases!
Good test cases and signature are worth about half the points for a function.
As mentioned on the discussion board:
- Include a signature for every function you write, as well
as a short description of what it does
(which can be just a couple of words, and/or taken from the hw).
Note that using self-documenting names goes a long way towards
not needing other comments.
(This shouldn't really need saying; it's been required
of every function you've written since ITEC120!)
-
You may use code from the posted solutions,
but (as with all code1 you don't write)
you should cite your source.
-
Please put a clear dividing point between code for requirements from previous homeworks,
and any changed/updated code that is required for this assignment.
-
You may want to remind yourself of
how the original game
plays
(keeping in mind that we
don't need to implement most inessential features).
- (4pts)
Read the documentation for foldl and foldr
(in DrRacket, hit F1 while the cursor is on each of those words;
look at the version provided by racket3).
-
What is the difference between what each function does?
-
What is a difference in the resources used internally,
when calling foldl or foldr?
-
What term from lecture presumably accounts for the difference?
-
If you have a task where either function will provide an acceptable answer,
which version should you prefer?
- (0pts)
Copy your previous solution to a new directory.
Make sure your language level is “Intermediate plus lambda”.
- (6pts)
Update move-asteroids so that it is a one-line call to map.
Update draw-asteroids so that it is a one-line call to foldl.
- (3pts)
Write a structure to represent a bullet.
Note that in the original game, bullets time out after a while;
what does that require as far as the information needed to
represent a bullet?
- (2pts)
Write move-bullet,
which simply updates a bullet by one tick passing
(but does not worry about collisions).
Include test cases.
- (2pts)
Write move-bullets.
Include test cases.
- (3pts)
Write a function representing the ship firing.
It should return just one bullet.
Include test cases.
- (1pt)
Write draw-bullet, similar to how draw-astr worked.
- (2pts)
Write draw-bullets.
- (3pts)
Update your world structure,
as well as move-world,
draw-world
and
handle-keypress
(or whatever you called the functions that did these tasks),
to handle bullets.
First just update the code and test to handle the extra field;
we'll worry about all of firing, collisions, and bullets timing out later.
Now we will start to handle collisions of bullets and asteroids.
- (3pts)
Update your move-world
so that it removes bullets that have timed out.
(Use filter and lambda.
I recommend make a separate function for removing bullets,
but you don't need to.
Regardless, you should have a couple tests.)
- (4pts)
Update your move-world
so that it removes asteroids that have been shot.
This involves two steps:
First write code that
takes in a list of asteroids and a single bullet,
and removes all the asteroids which overlap the bullet.
Then write a function that takes in a list of bullets
(You can use the list-processing template, or foldl.)
- (2pts)
Add a keypress-handler, to let a ship fire.
It' be nice (but not required) to have a way to slow the rate
at which bullets are released.
You might have the ship require a “recharge time”,
or just not allow new bullets while there are too many
existing bullets.4
-
Extra credit:
Have large asteroids break into smaller asteroids.
-
Extra credit:
Have bullets disappear, after they blow up an asteroid.
-
Extra credit:
add sounds.
See play-sound;
credit all sound files used, and do not use sound files
that you don't have the copyright to.
The only time you are allowed to call begin
is when calling play-sound.
1Or at the least,
all code except for possibly one-to-two line snippets
that are straightforward. ↩
3except that the intermediate-student
version only folds a single list at a time, which is the only way we've
seen it in class. ↩
2
The version functions are provided by the intermediate-language
is actually the same3,
but the documentation/explanation is better for the racket
version.
↩
4
I was also going to mention just the simple hack of using
on-release (rather than on-key)
to slow the player's rate of firing.
Alas, on-release isn't working;
a bug report has been submitted, and this has been fixed
in the experimental nightly-build.
↩
home—lectures—exams—hws—breeze (snow day)