RU beehive logo ITEC dept promo banner
ITEC 380
2012fall
ibarland
tlewis32

homelecturesexamshwsbreeze (snow day)

hw05
continuing asteroids

Due Oct.12 (Fri) in class. (Do not wait until Wed. night to start this!)

Implement the functions below. Following those requirements is a discussion of ways our game can differ from the original game.

    All non-drawing functions require at least two test cases! (Point-values are subject to slight tweaking.)
  1. (2pts) Re-write move-astrs to involve a call to map. (It will be easy to check that this works, since you already have several test cases.)
  2. (4pts) Design a “world” structure, to represent an entire asteroids game (just the ship and asteroids, for now). Construct some examples.
  3. (6pts) Write move-world : world -> world and draw-world : world -> image.
  4. (3pts) Have your code call big-bang1, providing it on-tick and on-draw handlers.
    You can look over the file big-bang-example-balloon.rkt dicussed in class, for an example of using big-bang (slightly longer/commented than racket-lang.org's splash page “Start Quickly” examples include a balloon-inflating example).
  5. (9pts) Implement controls: Provide a function2 ship, key → ship. If the provided key are the right or left arrows, then the ship rotates; if the provided key is (say) the up-arrow then accelerate the ship in the direction it's facing.
    I had several helper functions for this (ranging from accelerate-ship to degreesToRadians). For all functions, write test cases (before the function body), as usual.
  6. We will approximate collision-detection by treating all our objects (ships, asteroids, and later bullets) as circles3. Half the points are for test cases.
    1. (4pts) Write overlap?, which takes info about two circles (two (x,y) centers and two radii), and returns whether they overlap.
    2. (3pts) Write collide-ship-astr?, which takes in a ship and an asteroid, and returns whether or not they are colliding. (You can approximate collision by whether their "bounding circles" overlap.)
    3. (3pts) Write collide-ship-astrs?, which takes in a ship and a list of asteroids, and returns whether or not the ship collides with any of them.
    4. (2pts) Provide a stop-when handler to big-bang, using lambda.

Features, Requirements, and Extras

We're looking for a asteroids-like game, but we don't need to implement every feature of the original. In particular, it's acceptable to punt on the following issues.

Post/ask on discussion boards if you have other questions/suggestions. The ultimate point of the exercise is to

Opportunities are rife for extra-credit, by adding additional features. Don't do this until you get the above functionality working, though!


  1. (2pts) What is the signature for map?                                                             
    (that is, the version of map we wrote in class, and that is in the student languages). Give your answer in a comment.

    Note: “function” is not a specific-enough type, although something like “number → boolean” is. Similarly, “list” is not specific enough6, but “(listof string)” is.

  2. (4pts) Write a function which takes in a list of strings (some of which might be numerals), and returns a list of just the numerals, in descending numeric order (with any leading zeroes removed7). For example, given (list "hello" "35" "009" "hmm" "-7" "43"), your function would return (list "43" "35" "9" "-7").

    Your function should consist of calls to map and filter (and sort), as well as routine functions like string->number. It should not need cond/if, nor need any auxilliary helper functions. It is possible to write the function-body in a single line (or an equivalent let* which uses four local identifiers).

  3. (4pts)
    1. Using just a call to filter and lambda, write an expression which returns all numbers in (list 7 13 9 10 11 19 20 21 99 15 0) that are in the half-open interval [10,20) (that is, numbers x in the range 10 ≤ x < 20).
    2. Write a function which takes in a list of real numbers and two real numbers a,b, and returns all elements in the list which are in the half-open interval [a,b) Your function-body should just be a call to filter, with an appropriate lambda.
  4. (4pts) Write filter-out, which does the opposite of filter: it takes in a predicate and a list, and returns a list of all elements which fail the filter. For example, (filter-out even? (list 2 3 4 5)) should return (list 3 5). Give at least two additional test cases.

    Write your function without using filter or other higher-order functions — instead just use the standard list-processing template.


1You must require the teachpack universe.ss from 2htdp, to import the function big-bang.      

2 Note that eventually, you'll want to provide big-bang an on-key handler; that handler will presumably call this ship-updating function. The overall handler itself might just be a lambda, since it's fairly lightweight — just unwrapping the world to extract the ship and call this ship-updating function, and the wrapping the result back with the unchanged asteroids, since they don't care about keypresses. Instead of an anonymous function, you could also name a function world, key → ship, which would also let you test the wrapping/unwrapping independently of calling big-bang. …that seems a bit cumbersome; lambda expressions are convenient for small adaptor functions like this.      

3 If you prefer to use rectangles for bounding-boxes, see me for advice. If you want to use some other collision-detection approach, I encourage you to run it by me first.      

4 If you did want to go the extra mile and handle both key-release events (in addition to key-press events), it would presumably requires adding state to the ship representing whether or not it is currently in the middle of acceleration.      

5Or at the least, all code except for possibly one-to-two line snippets that are straightforward.      

6 The reason is fairly solid: (map expt (list "hello" (list 2 3) true)) is certainly taking in a function and a list, but it is fraught with type errors.      

7 The removal-of-leading-zeroes is allowed, since (number->string (string->number "043")) returns "43", not "043". For a small amount of extra credit, you can provide a second version of this function which preserves leading zeroes. Don't work on this until completing the rest of the homework.      

homelecturesexamshwsbreeze (snow day)


©2012, Ian Barland, Radford University
Last modified 2012.Oct.19 (Fri)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme