Chapter 4

Extending the stage Data Definition, i

Now that you have some working fireworks, it is time to make the animations a little richer. The goal of this chapter is to extend the stage data definition to support a flying ball going to the position of the explosion, and then adapt the rest of the program to properly handle and to exploit the change to stage. To begin, here is the new definition for stage:

;; a stage is either:
;;   - (make-explosion number number color)
;;   - (make-ball color)

(define-struct explosion (iradius oradius color))
(define-struct ball (color))

It has one more alternative, meant to represent a little ball with a color. This is then going to be used to show the (unexploded) firework flying across the sky.

Next, design the function shooting-animation:

(define start-x (/ world-size 2))
(define start-y world-size)

;; shooting-animation : number number number number -> animation
;; to produce a moving ball animation that starts at (start-x, start-y)
;; and goes to (end-x, end-y) in `total-steps', but is currently on `step'.
(define (shooting-animation step total-steps end-x end-y) ...)

Once you have finished shooting-animation, extend all of the existing functions that consume stage or frame values to cope with the new kind of stage. Note that the data definition for stage changed in a specific way. Accordingly, your templates should only change in a specific way, and thus you know how to change your functions.

Finally, extend the handle-key function so that all of the fireworks begin with a ball that moves to the appropriate position before the explosion starts.