Make a Bouncing Ball
Copy this code as a sample. It also has instructions embedded in the code
int add_to_x = 3; // set integer variable for 'x' velocity
float add_to_y = 3; // set float variable for 'y' velocity
int ball_x = 20; // set integer variable for initial ball 'x' placement
float ball_y = 320; // set float variable for initial ball 'y' placement
int ball = 50; // set integer variable for ball size
void setup() {
size(940, 320);
}
void draw() {
// ball = mouseY/2; // variable ball size
background(0);
fill(0, 0, 255); // set ball color
ellipse(ball_x, ball_y, ball, ball); // this draws the ball
if (ball_y>height-ball/2) { // check if the ball is at the bottom
add_to_y = -3; // change 'add_to_y' so the ball goes up
}
float p = add_to_y;
if (ball_y<ball/2) { // check if the ball is at the top
add_to_y = -p; // change 'add_to_y' so the ball goes down
}
if (ball_x>width-ball/2) { // check if the ball is at the right edge
add_to_x = -3; // change 'add_to_x' so the ball goes to the left
}
if (ball_x<ball/2) { // check if the ball is at the left edge
add_to_x = +3; // change 'add_to_x' so the ball goes to the right
}
// add_to_y = add_to_y +.03; // this line adds a little bit of gravity effect
ball_x = ball_x + add_to_x; // reset the 'x' value for ball position
ball_y = ball_y + add_to_y; // reset the 'y' value for ball position
}