CS 1102 (A12): Lab 1

To get your participation credit, make sure you
upload your Definitions file to turnin before you leave the lab!
(This work will not be graded---full credit just for submitting.)

Lab Motivation and Goals

By the end of this lab, you should be able to:

If you don't finish up to the first red sentence below, finish the lab at home---otherwise you risk falling behind in the course. Also, please consider finishing the rest for fun and learning :-).

Exercises: Getting to Know DrRacket (10 minutes)

Programming Exercises: A Simple Flight Simulator

A flight simulator tracks the location and travel velocity of an airplane. Assuming a 2-dimensional simulation, the location is just an x-y coordinate, and the velocity is a pair of numbers giving the change in the horizontal (delta-x) and vertical (delta-y) components of location per unit of time.

DrRacket provides a built-in define-struct for x-y coordinates:

;; a posn is (make-posn number number)
(define-struct posn (x y))

Do not type this into your definitions window -- it is built in!

  1. Create a data definition for a delta (two numbers as described above).
  2. Develop a data definition for planes that contains an image of the plane, a location (posn) and a velocity (delta).

  3. Define a constant PLANE-IMAGE that names the following image:

    plane image [Credit: www.free-clipart-pictures.net/airplane_clipart.html]

    Your code is more readable if you define a constant instead of pasting the image value throughout the definitions. Download the plane image above (e.g., by right-clicking on the image and "Save Image As" to desktop) and then insert it into your file using Insert > Insert Image from the DrRacket toolbar (if you don't see the image in the file menu, make sure the "All Files" option is selected instead of "Racket Source").

  4. Create an example of a plane; use define to name it PLANE1.

  5. Develop a function move-plane that consumes a plane and produces a plane. The produced plane has the same image and velocity as the given plane, but the location coordinates are changed by the respective amounts indicated in the velocity delta.

  6. DrRacket includes two teachpacks used for creating interactive animations. Using the Language > Add Teach Pack option in the toolbar, load the preinstalled image.ss and universe.ss from the middle (HtDP/2e) column. Then press Run.

  7. Put the following two definitions at the top of your file:
    (define WIDTH 500)
    (define HEIGHT 200)

    Develop a function draw-plane that consumes a plane and produces a scene showing the plane image located at its position coordinates. To produce a scene, use the place-image function defined in the image.ss teachpack as follows

      ;; place-image : image number number scene -> scene
      ;; draws a scene by adding given image to given scene at given x and y location
    replacing image, x and y below with the correct expressions that get these values from your plane structure:
      (place-image ...image... ...x... ...y... (empty-scene WIDTH HEIGHT))
    The top-left corner of the window is the origin. The x coordinate increases to the right and the y coordinate down.

  8. Put the following lines of code at the bottom of your file:

    (big-bang PLANE1
              (on-tick move-plane)
              (on-draw draw-plane))
    When you press Run, DrRacket will pop up a window containing your plane at its starting location (make sure the location is within the WIDTH and HEIGHT boundaries defined by the constants above). You should then see your plane move across the window. The code lines you copied are telling DrRacket to repeatedly (every fraction of a second) call move-plane, using draw-plane to display the result. After each call to move-plane, DrRacket stores the resulting plane and uses it as the input to next call of move-plane.

    You can stop the animation by closing the animation window. If you leave it open, you will eventually get an error from DrRacket when the plane's coordinates are overly large. Ignore this error if you see it.

Everybody should be able to finish up to this point during the lab period.

Now, we want to let the user control the plane by pressing the arrow keys to increase or decrease the angle at which the plane is coming down.

  1. Develop a function change-velocity that consumes a plane and a string (the order of parameters is important) and produces a plane. The produced plane has the same image and location as the given plane, but possibly a different velocity delta. If the input string is "down", increase the vertical component of the velocity delta by 0.5. If the input string is "up", decrease the vertical component by 0.5. Given any other input string, just produce the original plane unchanged (DrRacket may pass in other strings, so you do need this else case here).

    Edit the big-bang call as follows, making sure it stays at the very end of your Definitions window:

    (big-bang PLANE1
              (on-tick move-plane)
              (on-draw draw-plane)
              (on-key change-velocity))
    Now when you press Run to start the animation, your plane should move in response to presses of the up and down arrow keys.

Our current animation lets the plane fly right through the ground. Now, we want to (a) stop the animation when the plane touches the ground and (b) report either a crash or a safe landing depending on the rate of vertical descent at the time of landing.

  1. Write a function at-ground? that consumes a plane and produces a boolean indicating whether the bottom of the plane image is at the lower edge of the animation window. The built-in function image-height produces the height of an image in pixels (the same units as the HEIGHT constant already in your file).

    Edit the big-bang call again as follows:

    (big-bang PLANE1
              (on-tick move-plane)
              (on-draw draw-plane)
              (on-key change-velocity)
              (stop-when at-ground?))

If you are doing very well, you will finish up to this point

The rest of these steps are here for those going extremely fast or who want to see how to finish the animation.

  1. Develop a function steep-slope? that consumes a plane and determines whether the plane is descending more than 3 times faster in the vertical direction than in the horizontal.

  2. Edit your draw-plane function to put a large text message in the animation window when the plane touches the ground, using the text function defined in the image.ss teachpack as follows

    ;; text : string number string -> image
    ;; create an text image containing given string at given font size and color
    as in
    (place-image (text ...message... ...font-size... ...color...)
                  ...x... ...y... (empty-scene WIDTH HEIGHT))
    The message should be different depending on whether the plane landed steeply or not (such as "CRASH!" versus "Safe landing"). For color you can provide a string such as "red".

Obviously, there's much more you can do with this, such as stopping the animation when the plane flies out of the window, or wrapping the plane around the left/right edges, or giving the user more controls.

For more about using the universe.ss teachpack for graphical simulations, see Worlds and the Universe.