The purpose of this lab is to give you practice:
- working with lists of data,
- using reactors to make interactive programs, and
- playing with virtual balloons.
The purpose of this lab is to give you practice:
In this lab, you will be completing a world program simulating releasing an arbitrary number of balloons. These are represented as a list of positions, using the Posn data type.
When a balloon is released (by clicking the mouse), it floats away, and when it has floated out of our field of view, we can remove it from the list that represents the state of the world, since we no longer need to keep track of its position.
Task: To get started, save a copy of lab06.arr in CPO.
If you run the reactor by entering interact(balloons)
in
the Interactions Pane, you’ll see that parts of the program
are complete: You can click the mouse and a balloon is added to the list
of positions and displayed. However, the balloons just hang there in the
sky, which isn’t very realistic.
To make the program work, you’ll need to finish designing three
functions that are currently just entries in the wish list near the
bottom of the program: next-posns
, tick-posns
, and
onscreen-only
.
next-posns
The next-posns
function is called by the reactor
every time the clock ticks – 28 times per second. It’s
responsible for updating the state of the world – a list of
positions – by making every balloon float up SPEED
pixels and removing from the list any balloons that are now off
screen.
Task:
Write a single test case for next-posns
that will only pass
if both parts work:
SPEED
pixels higher
(that is, its y coordinate is decreased by
SPEED
, since y is measured from the top of the
window), and0
) “floats away” and is removed
from the list.Note: This test won’t pass until the very end of the lab.
Task:
Define the body of next-posns
.
This function’s body can be a single line. It combines calls to
tick-posns
(Exercise 2)
and onscreen-only
(Exercise 3),
which will do all the work.
That’s all there is to writing next-posns
. Now to make
it work, you’ll need to finish the other two functions on the wish
list.
tick-posns
The tick-posns
function goes through a list of positions and
moves each to be SPEED
pixels higher.
Task:
Write two test cases for tick-posns
: One for an empty list
and one for a list containing one or more Posn
s.
Task:
Complete the function body to make tick-posns
work. Your
solution can go through the list recursively or use a higher-order
function to do the recursion for you.
At this point, if you run interact(balloons)
and release some
balloons, they will float away. However, the balloon count will not
decrease after they go off screen, and the test case for
next-posns
will still fail. The off-screen balloons are still
cluttering up the list representing the state of the world.
This is a more serious problem than it may sound
like! If a user plays with the program long enough, the computer will run out
memory and the program will crash.
onscreen-only
The onscreen-only
function goes through a list of positions
and removes those that are no longer visible in our window.
Task:
Write two test cases for onscreen-only
: One for an empty
list and one for a list containing one or
more Posn
s.
Task:
Complete the function body to make onscreen-only
work. Your
solution can go through the list recursively or use a higher-order
function to do the recursion for you.
At this point, all of the test cases should pass, and when you run
interact(balloons)
and balloons float off screen, the
balloon count will decrease. If this isn’t the case, click on the
test cases that fail and think about why: Is the test case right? Is the
code right?
Relaxing as it is to watch our balloons float away, it may be more entertaining to make them pop!
Task:
Update the handle-mouse
function so
that if you click on a balloon it’s removed from the list.
Hints:
Then upload your lab06.arr
file to the Lab 6
assignment
on Gradescope.
If you worked with a partner, you should submit a single copy of the lab
with both your names – see these
instructions.