Download presentation
Presentation is loading. Please wait.
1
Exercise 1.3.1 A) Find out how tock in that figure works manually. Enter expression expressions such as (tock 0) and (tock 1) into DrScheme's Interactions window and see what their results and their effects on the canvas are. Hint: Execute the animation but comment out the expression (on-tick-event tock). B) Make the UFO animation slower and/or faster. C) Render the UFO as a yellow shape. Add an antenna. Make the background blue. D) Swap the two drawing actions in tock. Does this make a difference? Why? Why not?
2
1.3.1.A: Solution / Explanation Type in some example of tock manually: (tock 0) ;erases the 0 th UFO and draws the 1 st UFO. (tock 1) ;erases the 1 st UFO and draws the 2 nd UFO. In general, tock erases the current UFO and draws the next one. Since we do not want to sit at the computer and keep typing (tock 2), (tock 3), (tock 4), etc., we use a predefined function called on-tick-event. The on-tick-event function takes another function (like tock) and automatically runs it after every clock tick.
3
1.3.1B,C: Solution / Explanation To slow down the animation, increase the time between clock ticks. For example, (big-bang 1 0). To speed up the animation, decrease the time between clock ticks. For example, (big-bang.3 0). To make the UFO yellow, change the color in the ufo-draw function (from ‘green) to ‘yellow. To make the background blue, we will simply draw a solid blue rectangle the same size as the canvas: (draw-solid-rect0 0 0 100 200 ‘blue)
4
1.3.1C: Solution / Explanation To add an antenna, we add a line (say, 15 pixels long) that extends from the top of the circle. Since the center is (X, Y) and the radius is 10, the top point on the circle is (X, Y – 10). ---In Scheme, that is (X t) and (- (Y t) 10). Since we draw the line 15 pixels up from that point, the line goes up to (X, Y – 25). ---In Scheme, that is (X t) and (- (Y t) 25). So we have, (draw-solid-line0 (X t) (- (Y t) 10) (X t) (- (Y t) 25) ‘yellow)
5
1.3.1D: Solution / Explanation (define (tock t) (draw (ufo-draw (+ t 1)) (ufo-erase t) produce(+ t 1))) Swapping the 2 drawing actions causes: --part of the antenna may get erased --if the clock ticks are slow enough, you will be able to (incorrectly) have 2 UFOs on the screen at the same time There is no other difference as the 2 actions occur simultaneously, at least to human eyes.
6
In conclusion… We have written an animation program for moving the UFO across the screen. This program includes several Scheme functions that we wrote, like X, Y, ufo-paint, and tock. It also includes several predefined functions like +, *, draw-solid-rect0, big- bang, and on-tick-event. In HW4, you will write an animation for moving the shots across the screen. Then, you will combine the two animations, and complete the UFO game. For the rest of the semester, we will write shorter programs (each with only one, or a few, Scheme functions).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.