//Made by Christopher Tristano //Last modified: 3/3/15 // // //Moving ball game Ball[] balls; //Array of balls Guy guy; sceneRect table; int totalBalls = 0; //totalBalls boolean left_ = false; boolean right_ = false; boolean down_ = false; boolean up_ = false; void setup() { size(800,800); smooth(); sceneInitialize(); actionInitialize(); } void draw() { ////Next Frame scene(); notes(); action(); } void scene() { background(255); table.display(100,255,100); } void action() { guyMethods(); ballMethods(); } void notes() { fill(0); text("You are on level " + totalBalls,100,100); text("A,S,D,W to move...Click to add Ball...don't hit the balls!!!",300,100); } void ballMethods() { for(int i = 0; i < totalBalls; i ++) { balls[i].move(); balls[i].display(); for(int j = 0; j < totalBalls-1; j ++) { if ( i != j) { balls[i].intersectBall(balls[j]); } } } } void guyMethods() { guy.display(); guy.move(); for(int i = 0; i < totalBalls; i ++) { guy.ballCollision(balls[i]); } } void actionInitialize() { balls = new Ball[20]; // Ball(float tempX, float tempY, float tempDX, float tempDY, float tempR, float tempC) for(int i = 0; i < balls.length; i ++) { balls[i] = new Ball(random(table.left + 40,table.right-40),random(table.top + 40,table.bottom-40),random(-5,5),random(-5,5),random(5,15),random(255)); } guy = new Guy(); } void sceneInitialize() { table = new sceneRect(width/2,height/2,600,500); } void mousePressed() { totalBalls ++; if(totalBalls > balls.length) { totalBalls = balls.length; } } void keyPressed() { if(key == 'w' || key == 'W') { up_ = true; } if(key == 'a' || key == 'A') { left_ = true; } if(key == 's' || key == 'S') { down_ = true; } if(key == 'd' || key == 'D') { right_ = true; } } void keyReleased() { if(key == 'w' || key == 'W') { up_ = false; } if(key == 'a' || key == 'A') { left_ = false; } if(key == 's' || key == 'S') { down_ = false; } if(key == 'd' || key == 'D') { right_ = false; } } //Ball Class class Ball { float x,y,r; //Position and Radius of Ball float dx, dy; //x and y velocity float c; //Color //Ball constructor Ball(float tempX, float tempY, float tempDX, float tempDY, float tempR, float tempC) { x = tempX; y = tempY; r = tempR; c = tempC; dx = tempDX; dy = tempDY; } void display() { stroke(0); ellipseMode(CENTER); fill(c,100,150); ellipse(x,y,r*2,r*2); } void move() { //Increment by delta x = x + dx; y = y + dy; if( x < table.left + r || x > table.right - r) { dx *= -1; } if ( y < table.top + r || y > table.bottom - r) { dy *= -1; } } void intersectBall(Ball b) { float distance = dist(x,y,b.x,b.y); //Calculate Distance if (distance < r + b.r) { dx *= -1; dy *= -1; } } } class Guy { float x,y,w,h; //Position and Dimension float dx, dy; //Velocity //constructor Guy() { x = width/2; y = height/2; w = 20; h = 20; dx = 7; dy = dx; } void display() { stroke(0); fill(255,255,0); ellipseMode(CENTER); ellipse(x,y,w,h); } void move() { if(up_ == true) { y = y - dy; if ( y table.bottom-h/2) { y = table.bottom - h/2; } } if(left_ == true) { x = x - dx; if(x table.right -w/2) { x = table.right - w/2; } } } void ballCollision(Ball b) { float distance = dist(x,y,b.x,b.y); if(distance < w/2 + b.r) { totalBalls = 0; } } } //Scene rectangles class sceneRect { float x,y,w,h; //position and dimension float left,right,top,bottom; sceneRect(float tempX,float tempY, float tempW, float tempH) { x = tempX; y = tempY; w = tempW; h = tempH; left = x- w/2; right = x + w/2; top = y - h/2; bottom = y + h/2; } void display( float r, float g, float b) { rectMode(CENTER); noStroke(); fill(r,g,b); rect(x,y,w,h); } }