Animations
Thanks to Kathi Fisler for much of these exercises!
Make sure you are using the design recipe! This will make your life much easier. Don’t forget check-expects.
Cond practice. Write a function fire-hazard which consumes the current temperature and the number of days since it last rained and produces one of the strings “high”, “medium”, or “low”. The hazard is “high” if the temperature is over 100 and if last rained more than 3 days ago. The hazard is “low” if the temperature is under 70 or if it rained 1 day ago. Otherwise, the hazard is “medium”.
A Simple Soccer Game
The essence of a soccer game is a ball on a field. For the ball, download the following image to your computer and import it into your file using the “Insert image” option under the “Insert” menu. Use define to create a constant name for this image.
![[image of ball]](http://web.cs.wpi.edu/~kfisler/tsrj/2010/Labs/ball.gif)
(from www.hscripts.com)
Load the universe and image teachpacks from the “Language -> Add Teachpack” menus.
Using the following expression as a template, experiment with several expressions that place a ball into an empty animation scene (creating a new scene). You should replace the terms enclosed with<> with Scheme expressions.
(place-image <BALL-IMAGE> <X-POS> <Y-POS>
(empty-scene 200 200))
Write a function render-ball that consumes an x-coordinate and produces a scene with the ball drawn at that x-coordinate and some fixed y-coordinate (create a constant for the y-coordinate and choose your own value for it).
Write a function next-ball that consumes an x-coordinate and produces a new x-coordinate (the desired position of the ball in the next frame of the animation). Adding a a small number (3 or 4) to the given x-coordinate works well.
You are now ready to create your first animation. Place the following lines in your file without worrying too much about what’s going on… it will make more sense soon!
(define (ball-sim init-ball-x)
(big-bang init-ball-x
(on-draw render-ball)
(on-tick next-ball 1/28)))
The init-ball-x argument is the initial position for the ball. The 1/28 is the time in seconds that should pass between frames. When you call this function with an initial x-cordinate for the ball, you should see the ball move across the screen starting from the given initial coordinate.
Stopping the Game
Right now, the animation keeps running after the ball has left the screen. We want to stop the animation when the ball reaches the right edge of the screen.
Write a function at-right-edge? that consumes an x-coordinate and determines whether the right edge of the ball is touching (or beyond) the right edge of the screen. Introducing a constant for the screen width (and updating old uses of this value) would be a good idea at this point.
Add the expression (stop-when at-right-edge?) as the last argument to big-bang. When you run the animation, the ball should stop moving when it reaches the right edge of the screen (indicating that the animation has stopped).
YOU ARE RESPONSIBLE FOR COMPLETING WORK UP TO HERE AT LEAST!
The rest is really fun, so you should do it, too.
Moving a Goalie
Now we need to learn how to animate characters who move according to user input. Download and insert the following goalie image (as you did previously for the ball).
(from www.fotosearch.com)
Write a function render-goalie that consumes a y-coordinate and produces a scene with the goalie drawn at that y-coordinate and some fixed x-coordinate (create a constant for the x-coordinate and choose your own value for it).
Write a function react-goalie that consumes the goalie’s current y-coordinate and a string (for which key was pressed) and produces a new y-coordinate for the goalie. If the given key is “up” subtract 3 from the current y-coordinate. If it is “down”, add 3 to the current y-coordinate. Otherwise, produce the same coordinate. Use the built-in function key=? to compare the input string to the possible key values.
You are now ready to create your second animation. Place the following lines in your file:
(define (goalie-sim init-goalie-y)
(big-bang init-goalie-y
(on-key react-goalie)
(on-draw render-goalie)))
When you run this function, you should get an animation in which the goalie moves as the user presses the up and down arrow keys.
Adding and Detecting Goals
From here, you can make all sorts of extensions: add grass to the field, add a goal area, stop the animation early if the ball enters the goal area, etc. Get creative by trying some of these extensions with any remaining time you have




