Student Project

Bouncing Ball
Ability Level: Intermediate
Estimated Time: 3-4 hours
Objectives:
  • Use timers
 
Materials and Resources:
  • Visual Basic Software
  • Textbook
Overview: Create a colored ball which bounces inside of a box. It should pong off of the sides, top, or bottom. Add a button to start and stop the ball.

Instructions:
  1. Use 4 line objects to create a box in the frame.
  2. Somewhere inside the box, create a circle using the shape object.
  3. On the side, create a button and label it "Start/Stop"
  4. Create a timer object, and double click it to get to the timer code.
  5. Write the code that will be run each time the timer goes off. This code should move the ball, then use if/then statements to detect whether the ball has hit the lines or not. If it has, the direction of the ball should change. 
  6. Double click the button to get the the button click code. Here you should enable or disable the timer to make the ball move or stop.

 

Hints:

One possible method is to create 2 global integer variables x and y that contain the amount that the ball moves horizontally and vertically each timer call. To move the ball, add these variables to the ball's Top and Left attributes. Look at the ball's Top, Left, Height and Width along with the lines' X1 and Y1 attributes to see if the ball has hit a line. If the ball hits a horizontal line, it's vertical movement variable should change sign from positive to negative (ball changes direction) and if the ball hits a vertical line, it's horizontal movement should change sign.

PROJECT EXTRAS
  • Add a button to increase or decrease the speed of the bouncing ball.