1 / 12

Section 1.2: Drawing a UFO (conclusion)

Section 1.2: Drawing a UFO (conclusion). Last time…. Using figure 1, we can now find a UFO’s location (using the X and Y functions). And, we can now draw and erase the UFO on the screen (using the UFO-draw and UFO-erase functions). Today…

dsheley
Download Presentation

Section 1.2: Drawing a UFO (conclusion)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 1.2: Drawing a UFO(conclusion)

  2. Last time… • Using figure 1, we can now find a UFO’s location (using the X and Y functions). • And, we can now draw and erase the UFO on the screen (using the UFO-draw and UFO-erase functions). Today… • In programming, one should not have two functions that both have similar code. So, we will see another way to write UFO-draw and UFO-erase. • Then, a practice exercise to introduce HW3.

  3. UFO-draw vs. UFO-erase • The UFO-draw program and the UFO-erase program are extremely similar. • We are wasting time doing the same thing twice. • What do both programs have in common? (define (ufo-… t) (and (draw-solid-disk0 (X t) (Y t) 10 …) (draw-solid-rect0 ( - (X t) 20) (Y t) 40 3 …))) • What is different? The color (‘green or ‘white)

  4. Another way to write the program… • What if we had a program that allowed the user to pick the color for the UFO to be drawn? • Let’s call it UFO-paint. (if we didn’t already have a UFO-draw program, that name would be fine.) • The trick to avoiding duplicate work is abstraction, a fancy word for writing functions that do as many different things as possible. • In our case, we want to define a function that is like ufo-draw and ufo-erase but it can do both.

  5. Another way to write the program… • Since the difference between the two functions is the color that they use, we merge them into one function by adding a second variable for the color. • The parts that change from ufo-draw and ufo-erase are underlined: (define (ufo-paint t c) (and (draw-solid-disk0 (X t) (Y t) 10 c) (draw-solid-rect0 (X t) (Y t) 40 3 c)))

  6. Now a shorter ufo-draw… (define (ufo-draw t) (ufo-paint t ‘green)) …and a shorter UFO-erase (define (ufo-erase t) (ufo-paint t ‘white))

  7. Exercise 1.2.2: Drawing Shots • We can draw the UFO. However, to play the game, we also have to draw the shots. • Let’s display a shot as a thin, upward pointing, equilateral triangle. • Step 1: Draw a canvas of 100 by 100 pixels, and draw a small equilateral triangle into it with the points: (50, 30), (45, 40), and (55, 40). --Connect the base points with three lines. --What is the height of the triangle? --What is the width of its base?

  8. Exercise 1.2.2: Drawing Shots • Step 2: Next, draw a triangle of the same width and height but with the highest point at (70,10). --What are the other two points? • Step 3: Draw another shot at (20, 30). • Step 4: Draw another shot at (80, 30). --You can think of the shots in Steps 1, 3, and 4 as one and the same shot moving across the screen.

  9. Exercise 1.2.2: Drawing Shots • Step 5: Use your experience with drawing these four triangles to define a Scheme function that can draw a triangle of this size, given the coordinates of the top point: • ;; given: xtop is the x coordinate of the top-most point, • ;; ytop is the y coordinate • (define (triangle-draw xtop ytop) (and ... ... ...)) • The first two lines are called comments, and they remind you what xtop and ytop are, when you will read this program in two months or years from now.

  10. Homework 3 (due Sunday) Do Exercise 1.2.1 • A) Create a canvas of 100 by 200 pixels. • B) Draw a shot close to the bottom of the canvas. Use thin, tall rectangles to depict shots. • C) Draw a second shot. • D) Define the function shot-draw, which -- like ufo-draw -- consumes t and draws a shot. It computes the X and Y coordinates of the shot using the shot-X and shot-Y functions from HW2.

  11. Homework 3 (due Sunday) Finish Exercise 1.2.2 • Define the function shot-draw, which draws a shot as a triangle. It consumes a single number, the time t, and draws a triangle on a canvas of size 100 by 200: ;; given: t (define (shot-draw t) ...) • Again, use the functions shot-X and shot-Y from HW2 to determine where the base point is. • Hint: You should also use triangle-draw.

  12. In summary… • Just as we can draw and erase UFOs, we can draw and erase shots. • We will create a function to do the repeated drawing and erasing, and from there, we write the animation for the UFO game. Next time…

More Related