CS 1101: Lab 2 - Part 1

Extending the Soccer Animation from Lab 1

Now that we have learned about structs, we can create a single game with both the ball and the goalie. The information that defines the "state" of the animation now has two pieces: the x-coordinate of the ball and the y-coordinate of the goalie. We need to create a struct to hold these values, and we need to extend the functions for drawing, moving, and reacting to events so that the functions consume and return a struct instead of consuming and returning a single coordinate.

You may copy any code you need from your Lab 1 file as you work on these exercises. Name your file lab2.

  1. Write a data definition and examples of data for a struct for a game, which has both a ball and a goalie. (Your struct needs to represent the x-coordinate of the ball and the y-coordinate of the goalie.)

  2. Write a function draw-game that consumes a game struct and produces a scene containing both a ball and a goalie. Reuse your old functions from Lab 1 wherever possible.

  3. Write a function update-game that consumes a game struct and produces a new game struct. In the returned game, the position of the ball should change using your old update-ball-x function, and the goalie should be unchanged. Write test cases for your function.

  4. Write a function react-game that consumes a game struct and a string (representing a key press) and produces a game struct. In the returned game, the ball should be unchanged and the goalie should change using your old goalie-react function. Write test cases for your function.

  5. Put it all together using the following commands, substituting a starting value for an-initial-game-structure (your starting value should be a struct):

          (big-bang an-initial-game-structure
                    (on-tick update-game)
                    (to-draw draw-game)
                    (on-key react-game))
    
    You should now get the combined effects of the ball and goalie in a single game.
Use turnin to turn in your animation. Proceed to part 2 of the lab by choosing one of the options on programming with structs.