//Jason Martin //Project 8 float buttonX = 15; float buttonS = 230; int table = 0; Ball redMartin, greenMartin, blueMartin; Button redButton,blueButton,greenButton,tableColor; void setup() { size (800,600); redMartin = new Ball(255,0,0); redMartin.y = 100; greenMartin = new Ball(0,255,0); greenMartin.y = 200; blueMartin = new Ball(0,0,255); blueMartin.y = 300; redButton = new Button(buttonX,height-40,80,40,"Red"); blueButton = new Button(buttonX+buttonS,height-40,80,40,"Blue"); greenButton = new Button(buttonX+buttonS*2,height-40,80,40,"Green"); tableColor = new Button(buttonX+buttonS*3,height-40,80,40,"Table"); } void draw() { noStroke(); background(150,110,80); if(table%2==0){ fill(5,25,255); rect(50,50,700,500); noStroke(); fill(0); ellipse(73,73,45,45); ellipse(389,73,45,45); ellipse(727,73,45,45); ellipse(389,527,45,45); ellipse(73,527,45,45); ellipse(727,527,45,45); } else{ fill(0); rect(50,50,700,500); noStroke(); fill(255); ellipse(72,72,45,45); ellipse(390,72,45,45); ellipse(728,72,45,45); ellipse(390,528,45,45); ellipse(72,528,45,45); ellipse(728,528,45,45); } spinBalls(); makeButtons(); } void makeButtons(){ redButton.draw(); blueButton.draw(); greenButton.draw(); tableColor.draw(); } void spinBalls() { redMartin.spin(); greenMartin.spin(); blueMartin.spin(); } void mousePressed(){ if(redButton.mouseOn()){ redReset(); } if(greenButton.mouseOn()){ greenReset(); } if(blueButton.mouseOn()){ blueReset(); } if(tableColor.mouseOn()){ table+=1; } } void keyPressed(){ if(key=='1'){redReset();} if(key=='2'){greenReset();} if(key=='3'){blueReset();} } void redReset(){ redMartin.x=75; redMartin.y=150; } void greenReset(){ greenMartin.x=150; greenMartin.y=250; } void blueReset(){ blueMartin.x=200; blueMartin.y=300; } void bounce(Ball h,Ball b) { //bouncing if (dist(h.x,h.y,b.x,b.y)<20){ h.mX=-h.mX; h.mY=-h.mY; b.mX=-b.mX; b.mY=-b.mY; } } class Ball { //Ball float x =360,y =60; float mX =random(2,5)-.5,mY=random(3,5)-.5; int w=40,h=40; float r=0,g=0,b=0; Ball(int rojo,int gren,int blu) { Colors(rojo,gren,blu); } void Colors(int rojo,int gren,int blu) { r=rojo; g=gren; b=blu; } Ball( float xx, float yy, int rojo, int gren, int blu) { x=xx; y=yy; Colors(rojo,gren,blu); } Ball( float xx, float yy, float mXmX, float mYmY, int rojo, int gren, int blu) { x=xx; y=yy; mX=mXmX; mY=mYmY; Colors(rojo,gren,blu); } //methods void spin() { roll(); make(); } void roll() { if (x<60||x>width-65) mX=-mX; x += mX; if (y<60||y>height-65) mY=-mY; y += mY; if(mY==0 ){mY=3;} } void make() { noStroke(); fill(r,g,b); ellipseMode(CENTER); ellipse(x,y,w,h); } } class Button { float x,y,w,h; String text; Button(float x,float y,float w,float h,String text){ this.x=x; this.y=y; this.w=w; this.h=h; this.text=text; } void draw(){ fill(255,0,255); rect(x,y,w,h,0); fill(0); text(text,x+h/2,y+w/3); } boolean mouseOn(){ if(mouseX>x&&mouseXy&&mouseY