# Exam 1 Lab: Bouncing Ball¶

This problem is part of your first real exam in this course. It is worth 50 points out of a total of 150 points for the exam.

Since we are learning how to program, not just play games with Scratch, the following exercise will limit you to just those controls that act like real things you can do in programming languages. That means some of the more advanced controls provided with Scratch are off limits for this exercise!

## Create the Ball¶

First, change your sprite from the silly cat to a ball. That will look better for this problem.

## Create Variables¶

Create three variables, one named “Xpos”, one named “Ypos”, and one named “Xstep”.

Set up your program so it sets “Xpos” and “Ypos” to zero, and sets “Xstep” to one for now.

In your program, add a control that moves the ball to the center of the screen using your “Xpos” and “Ypos” variables. (Drag the variable names into the spot where you can enter a number, and the system will use the value in those variables to control where the system places the ball.

## Get Moving¶

Now, set up a “forever” loop that will make the ball move.

Inside the loop, add a control that changes the value stored in “Xpos” by the amount in the “Xstep” variable. Add a control from the “Motion” menu that makes the ball move to the new “Xpos”,Ypos” location.

Run this program and watch the ball slide off the screen!

## Bouncing!¶

Now, add a control that will figure out if you are “touching” the “edge” of the window. If so, change the value of “Xstep” By multiplying it by -1. That will make the value in “Xpos” get smaller each time we go through the loop, not bigger, and the ball will move in the opposite direction (bouncing off the wall)!

Run this version, and the ball should bounce when it hits the right side of your window. If you let it go, it will eventually hit the left edge and do the same thing! Cool!

## Move at an Angle¶

Now, create a new variable, “Ystep”, and set it to one as well. Add code to modify the value of “Yos” using “Ystep” and make your ball move to the new position with these new values.

Run your program now, the ball should move at an angle until it hits an edge. What happens now?

## Houston, We have a Problem¶

The problem we have now, is that we cannot figure out which “edge” we hit with the simple Scratch “touching edge” control. We need to find another way to solve this problem.

The solution involves checking the value stored in “Xpos” and “Ypos”, and then watching the ball as it moves.

Create four new variables: “Xmax”, “Xmin”, “Ymax”, and “Ymin” and set them to some suitable values (200, -200, 140, and -140 was what I used on my Mac).

If the ball gets greater or equal to the biggest X value we want to let it reach (“Xmax”), we need to modify “Xstep” to make it bounce as we did before. When we get to a value of “Xmax”, we say we have hit the right wall, and we want to bounce off of that wall by changine the number stored in “Xstep”.

We do the same thing as we watch the value stored in “Ypos”. If it gets bigger that “Ymax” we have hit the top wall, and we bounce again, this time by modifying the value stored in “Ystep”.

You should be able to handle the left and bottom walls by checking “Xmin” and “Ymin” the same way

It will take four IF blocks, each checking one wall, and “bouncing” by adjusting a step value to make the ball bounce off all four walls!

## Setting the Edges¶

Before you run your final code, slide the ball to the edges of the window to see what values to set in your min and max variables in both directions. When you run the code, it should look pretty cool!

## Extra Credit¶

I will give ten extra points if you figure out how to mke the ball bounce off of all four walls using only two IF tests. Look at your code and see if you see the way to do this!

## What to Turn In¶

There will be a link under the Lab Assignments menu item on Blackboard where you can upload your final Scratch program. Have fun with this!