Draw and Erase UFOs Program Tutorial
100 likes | 127 Vues
Learn to write a program to draw and erase UFOs on the screen using simple functions and color changes. Practice locating UFOs and creating an animation step by step.
Draw and Erase UFOs Program Tutorial
E N D
Presentation Transcript
Writing a program to draw UFOs Remember that the header includes: • define • program’s name (let’s call it UFO-draw) • program’s variables (t, for the time) (define (ufo-draw t) ... will fill in later …)
Writing a program to draw UFOs (define (ufo-draw t) (and (draw-solid-disk0 ... ...) (draw-solid-rect0 ... ...))) • Since we drew a disk and a rectangle to draw the UFO, the function says that to draw a UFO first draw a disk and then a rectangle.
Writing a program to draw UFOs (define (ufo-draw t) (and (draw-solid-disk0 ... ... 10 'green) (draw-solid-rect0 ... ... 40 3 'green))) • To keep the program relatively simple, we will assume that all UFO’s are green and exactly the same size.
Writing a program to draw UFOs • Of course, the (…) tells us that the function is still incomplete. • More precisely, the dots in draw-solid-disk0 (and draw-solid-rect0) say we don't know the X and Y coordinates of the UFO. • Recall that the UFO’s location is expressed by X and Y. So if we know t, we can use the X and Y programs that we wrote in Section 1.1: (define (X t) (+ (* 10 t) 20)) [We used the + and * functions (define (Y t) to write X (and Y).] (+ (* 20 t) 60))
Writing a program to draw UFOs For draw-solid-disk0, the location is the center: (X, Y). (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 … 40 3 'green)))
Writing a program to draw UFOs However, for draw-solid-rect0, the location is the top-left corner: (X – 20, Y). (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 'green))) [We used the draw-solid-disk0, draw-solid-rect0, X, and Y functions to write ufo-draw.]
Writing a program to erase UFOs • Notice that the screen is (almost) white. • To make it appear that we are erasing the UFO, we simply draw a white one. • Let’s call this function erase-UFO. • All we need to do is make 2 small changes to draw-UFO: -change the program’s name -change each of the colors to white
Writing a program to erase UFOs (define (ufo-draw t) (and (draw-solid-disk0 (X t) (Y t) 10 'green) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 'green))) BECOMES (define (ufo-erase t) (and (draw-solid-disk0 (X t) (Y t) 10 ‘white) (draw-solid-rect0 (- (X t) 20) (Y t) 40 3 ‘white)))
In summary… • 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). Next time… • In programming, one should not have two functions that both have similar code. • We will see another way to write UFO-draw and UFO-erase. • Then, finally on to the ANIMATION!