//Valondine Jeudy //CST 112 //Prof. M //November 12, 2014 //declare variables String title= "Pool Table"; String author= "Valondine Jeudy"; String course = "CST 112"; Ball redJeudy, greenJeudy, blueJeudy; Button redButton, greenButton, blueButton, tableButton; int tableColor=0; void setup() { //setup the game size(500, 400); redJeudy= new Ball(255, 0, 0); redJeudy.x = random(50, 100); redJeudy.y = random(200, 300); greenJeudy= new Ball(0, 255, 0); greenJeudy.x =400; greenJeudy.y = 350; blueJeudy = new Ball (0, 0, 255); blueJeudy.x = 300; blueJeudy.y = 255; redButton = new Button(width-400, height-15, 25, 20, "Red"); blueButton = new Button(width-300, height-15, 27, 20, "Blue"); greenButton = new Button(width-200, height-15, 35, 20, "Green"); tableButton = new Button(width-100, height-15, 35, 20, "Table"); } void draw() { //draws the next frame background(255, 0, 0); //changes the color of the table. if (tableColor%2==0) { fill(135, 206, 250); drawpoolTable(); } else{ fill(225,20,147); drawpoolTable(); } // Title, etc. textSize(12); (0,25,145); text( title, width/2-35, 20 ); text( author, 5, height-10 ); text(course,width-50,15); } void drawpoolTable() { // draws the pool table with the balls. rectMode(CENTER); rect(width/2, height/2, width-55, height-55); redJeudy.drawMovement(); greenJeudy.drawMovement(); blueJeudy.drawMovement(); redButton.drawButton(); greenButton.drawButton(); blueButton.drawButton(); tableButton.drawButton(); } class Ball { // properties float x = 25, y = 20; float dx = 4, dy = 4; float w = 35, h=35; int r, g, b; // Constructors: set colors to the balls Ball(int r, int g, int b) { this.r = r; this.g = g; this.b = b; } // methods void drawMovement() { //call the drawBall,moveBall and bounceTable methods drawBall(); moveBall(); bounceTable(); } void drawBall() { // draw the balls and show. fill(r, g, b); ellipse(x, y, w, h); } void moveBall() { //make the ball moves x+= dx; y+= dy; } void bounceTable() { // bounce the ball of the pool table. // x axis if ( x < 42 || x> 465) { dx*=-1; x+=dx; } // y axis if ( y < 45 || y>355 ) { dy*=-1; y+=dy; } } } class Button { // PROPERTIES// float x, y, w, h; String name; // positions and name of buttons. CONSTRUCTOR Button(float x, float y, float w, float h, String name) { this.x = x; this.y = y; this.w = w; this.h = h; this.name = name; } void drawButton() { //template for the buttons textSize(10); fill(0, 155, 155); rectMode(CORNER); rect(x, y, w, h); fill(125, 0, 65); text(name, x+5, y+13); } boolean isHit() { //if buttons are clicked. if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { return true; }else { return false; } } } void mousePressed() { //handles mouse clicks if (redButton.isHit()) { redReset(); } if (greenButton.isHit()) { greenReset(); } if (blueButton.isHit()) { blueReset(); } if (tableButton.isHit()) { tableColor+=1; } } void keyPressed() { //handles keys if (key == '1') { redReset(); } if (key == '2') { greenReset(); } if (key == '3') { blueReset(); } } void redReset() { //changes position of red ball redJeudy.x = 75; redJeudy.y = 150; } void greenReset() { // changes position of green ball greenJeudy.x = 80; greenJeudy.y = 250; } void blueReset() { // changes position of blue ball blueJeudy.x = 95; blueJeudy.y = 320; }