Homework 3a
MUST BE DONE WITH YOUR PARTNER
Due Date Tues at 9:00pm (Week 3)
Purpose Practice designing programs
Exercises
A Posn Aside
; A Posn is a (make-posn Number Number) ; and represents a 2d coordinate (define POSN-20 (make-posn 20 20)) ; posn-temp : Posn -> ? (define (posn-temp p) (... (posn-x p) ... (posn-y p)))
Exercise 1 Design a function which, given two Posns, makes a new Posn with their x and y values added to each other.
Photo Shoot
We are going to develop a game which simulates a remote controlled photo shoot. In it, a stationary photographer begins the photo shoot on the ground, with some part of the ground as their goal to capture. They will then launch a drone with a camera and fly it. The shoot will end when the drone can capture the whole surface area of the goal... or it crashes!
; A PS (Photo Shoot) is one of: ; - (make-launch Number Interval) (define-struct launch [photographer goal]) ; - (make-flight Number Interval Posn) (define-struct flight [photographer goal drone]) ; Where: ; - photographer represents a photographer's x-coordinate on the ground ; - goal represents the range of the desired image to to be captured at the ground-level ; - drone (if any) represents the drone's x/y position (with 0, 0 at the bottom left) ; All numbers are measured in pixels. ; An Interval is a (make-interval Number Number) (define-struct interval [left right]) ; and represents the leftmost and rightmost range of an interval in pixel coordinates (inclusive)
Exercise 2 Provide examples and templates for the above data definitions. Recall that when data definitions refer to other complex data types, the template must reflect this by calling the appropriate template.
Exercise 3 Design a function falling-drone which, given a PS, moves the drone (if any) down by 1 pixel.
Exercise 4 Design a function launch-drone, which given a PS without a drone in flight, puts the drone in flight at 20 pixels in the air and 15 pixels to the right of the photographer. If the drone is in flight, nothing should change.
Exercise 5 Design a function shoot-over?, which determines if the shoot is over. The shoot is over if the drone has crashed (height = 0), or if the drone has captured the goal. Assume the drone has a 53.14 degree camera, which essentially means that if the drone is y pixels above the ground, it can see the ground y/2 pixels to the left and right of its position. It does not matter if the drone captures more than the goal, so long as the whole goal is captured.
Exercise 6 Design a function which draws the photo shoot. The photographer can be represented by a vertical rectangle of any color. The goal should be visually distinct from the rest of the ground-level. The drone, if any, should be represented by a black horizontal rectangle. The background on which all these images are placed should be a fixed size.
Be sure to use constants (and helper functions!) to keep your code as clean as possible. Also, remember that in our data definition, (0, 0) is at the bottom left, but the image library has (0, 0) at the top left!
Exercise 7 Design a function handle-keyevent which given a PS and a KeyEvent launches the drone if it is not already in flight and the given key is "l". This should put the drone 20 pixels in the air and 15 pixels to the right of the photographer. If the drone is in flight and the given key is either "up", "right", or "left", it should move the drone 5 pixels in that direction.
Exercise 8 Compose your above functions into a big-bang program, wrapped inside a main function. The game should stop when the shoot is over, and the drone should fall 1 pixel every 1/10 of a second (see and use your code from last week’s assignment).
The function should take in an initial PS and output whether or not the shoot was a success.