Project 2.6: Gravity
Count the bounces a ball makes as it crosses the screen


  • Your program will have a ball that moves across the screen as is affected by gravity

    • The ball must always start in the top left corner

    • The ball should have a random x speed

    • When the ball hits the bottom of the screen, it should reverse direction

      • When bouncing, it the y speed of the ball will randomly be reduced to between 70% and 90% of its previous y speed due to friction.

    • When the ball hits the right side of the screen, it should "reset" to the top left with a new random starting speed

  • Your program will display the number of bounces

    • Show how many bounces the current ball has on the screen and update it in real time

    • Show how many bounces the record ball has had.

FAQ / Tips

  • Progression Advice

    • Make a ball that moves to the rightby a random speed, starting in the top left

    • Add in "gravity" to accelerate the ball each frame

    • Make the ball reverse direction when it hits the bottom of the screen

    • Make the ball slow down from friction

    • Reset the ball when it goes off the right side of the screen

    • Track and display the current score

    • Track and display the record score

  • How do I slow the ball down to 70% to 90% of it's previous speed.

    • Multiply the ySpeed by a random number between 0.7 and 0.9.

  • My ball is getting stuck once I slow it down! HELP!

    • You'll want to use this line of code to reverse the ball, rather than simply mulitplying by negative one.

ySpeed = abs(ySpeed) * -1;

    • This ensures the ball will always move upward, even if it's moving slower and can't get back above zero.

  • Wait, how do I draw text? We didn't learn that yet!

    • You can read the Coder's Handbook Text entry for full information. This is actual something we'll fully dig into as part of our next project. For your convinence, below is some sample code to draw text. Note: You'll need to create and assign values to these variables:



textAlign(CENTER, CENTER);

text("Current", width * 1 / 3, 75);

text(current, width * 1 / 3, 125);

text("Record", width * 2 / 3, 75);

text(record, width * 2 / 3, 125);