xxxxxxxxxx
int x;
int y;
int w = 20;
int h = 20;
int speed = 3;
void setup (){
size (200, 200);
x = 10; // starting it at 10 since my if statement below wants the ball to never be < 10,
// if we started this at 9, or 8 it would jitter and not move
y = height/2;
}
void draw() {
background (255, 255, 0);
//guide lines so you can see the edge of screen
line (0,0, 0, height);
line (width-5, 0, width-5, height);
ellipse (x, y, w, h);
x = x+speed; // give the ellipse some movement.
// I'm using width-15 and 10 instead of full width or 0 so that the ball bounces off it's sides, not it's center. It's an illusion.
if ((x > width-15) || (x < 10) ) {
speed = speed * -1; // when the ball touches a side, make it move in the opposit direction
}
}