//Adam Kochen //Project 8 //classes float buttonX = 10; float buttonS = 120; int table = 0; Ball redKochen, greenKochen, blueKochen; Button redButton,blueButton,greenButton,tableColour; void setup() { size (800,600); redKochen = new Ball(255,0,0); redKochen.y = 100; greenKochen = new Ball(0,255,0); greenKochen.y = 200; blueKochen = new Ball(0,0,255); blueKochen.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"); tableColour = new Button(buttonX+buttonS*3,height-40,80,40,"Table"); } void draw() { noStroke(); background(150,110,80); if(table%2==0){ fill(0,100,0); } else{ fill(255,120,170); } rect(50,50,700,500); moveBalls(); makeButtons(); } void makeButtons(){ redButton.draw(); blueButton.draw(); greenButton.draw(); tableColour.draw(); } void moveBalls() { redKochen.move(); greenKochen.move(); blueKochen.move(); bounceIf(); } void mousePressed(){ if(redButton.mouseOn()){ redReset(); } if(greenButton.mouseOn()){ greenReset(); } if(blueButton.mouseOn()){ blueReset(); } if(tableColour.mouseOn()){ table+=1; } } void keyPressed(){ if(key == '1'){redReset();} if(key == '2'){greenReset();} if(key == '3'){blueReset();} if(key == '4'){collision();} if(key == '5'){collisionUp();} } void collision(){ redKochen.x = 100; redKochen.y = 300; redKochen.mX = 2; redKochen.mY = 0; greenKochen.x = 200; greenKochen.y = 300; greenKochen.mX = -2; greenKochen.mY = 0; } void collisionUp(){ redKochen.x = 100; redKochen.y = 300; redKochen.mX = 2; redKochen.mY = -2; greenKochen.x = 200; greenKochen.y = 300; greenKochen.mX = -2; greenKochen.mY = -2; } void redReset(){ redKochen.x = 100; redKochen.y = 150; } void greenReset(){ greenKochen.x = 100; greenKochen.y = 250; } void blueReset(){ blueKochen.x = 100; blueKochen.y = 350; } void bounceIf(){ bounce( redKochen, greenKochen ); bounce( redKochen, blueKochen ); bounce( greenKochen, blueKochen ); } void bounce( Ball h, Ball b) { //bounce off 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,4)-.5 , mY =random(3,5)-.5; int w = 30, h = 30; float r = 0, g = 0, b = 0; //constructor Ball(int rr, int gg, int bb) { colours(rr,gg,bb); } void colours(int rr, int gg, int bb) { r=rr; g=gg; b=bb; } //methods void move() { roll(); make(); } void roll() { if (x<65 || x>width-65) mX= -mX; x += mX; if (y<65 || 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; //-Constructor-// Button(float x, float y, float w, float h,String text){ this.x = x; this.y = y; this.text = text; this.w = w; this.h = h; } //-Methods-// void draw(){ fill(255); rect(x,y,w,h,15,15,0,0); fill(0); text(text,x+h/2,y+w/3); } boolean mouseOn(){ if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { return true; } else { return false; } } }