// bouncing ball within a box float boxX=80, boxY=38, boxW=445, boxH=398; float ballX=30, ballY=10, ballDX=5, ballDY=9; float ball0X=50, ball0Y=20, ball0DX=3, ball0DY=2; void setup() { // size size( 640, 480 ); reset(); } void draw() { scene(); action(); ball(); ball0(); } void scene(){ background( 200, 255, 200); } void action() { stroke(255); strokeWeight(10); rectMode( CORNER ); fill(100,100,255); //-- rect(40, 40, 450, 400); rect( boxX, boxY, 450, 400); } void ball(){ ballX += ballDX; ballY += ballDY; if (ballX < 0 || ballX > boxW) ballDX= -ballDX; if (ballY < 0 || ballY > boxH) ballDY= -ballDY; fill( 0,0,0 ); strokeWeight(2); noStroke(); ellipseMode(CENTER); ellipse( boxX+10+ballX, boxY+ballY, 15,15 ); } void ball0(){ ball0X += ball0DX; ball0Y += ball0DY; if (ball0X < 0 || ball0X > boxW) ball0DX= -ball0DX; if (ball0Y < 0 || ball0Y > boxH) ball0DY= -ball0DY; fill( 150,0,150 ); strokeWeight(3); noStroke(); ellipseMode(CENTER); ellipse( boxX-20+ball0X, boxY+ball0Y, 15,15 ); } void reset() { ballDX= random(10); ballDY= random(4); }