//variables for the ball float posX = 100; float posY = 50; float speed= 0; float speedX = .75; //variables for the whole system float grav = 0.04; float time = 0; float red = 255; float blue = 131; float green = 0; PFont f; //variables for the paddle float paddleX = 200; float paddleY = 200; float paddleWidth = 50; float paddleHeight = 10; int score = 0; void setup() { size(400, 400); ellipseMode(CENTER); rectMode(CENTER); frameRate(120); // set the font type f = createFont("Arial", 25, true); textFont(f); textAlign(CENTER); } void draw() { //remove the mouse Cursor noCursor(); background(175); //Draw background drawBalls(); //draw score box fill(25, 5, 5); rect( 0, 0, 210, 50); fill(255); text( "Score :", 40, 20); text( score, 90, 20); // draw functions drawPaddle(); movePaddle(); drawBall(); ballCollision(); ballXSpeed(); paddleShrink(); } //make the paddle follow the mouse position void movePaddle() { paddleX = mouseX; paddleY = mouseY; } //Draw the Paddle void drawPaddle() { rectMode(CENTER); noStroke(); fill(255); rect(paddleX, paddleY, paddleWidth, paddleHeight); } //Draw the ball void drawBall() { fill(255); ellipseMode(CENTER); ellipse(posX, posY, 15, 15); } //Set the ball collision logic void ballCollision() { //Gravity for the ball posY = posY + speed; speed = speed + grav; //collison for ball one, checks if its right on top or underneath the paddle if (posY > paddleY - (paddleHeight/2) && posY < paddleY + (paddleHeight/2) && //check if the ball in is the the middle of the paddle posX > paddleX - (paddleWidth/4) && posX < paddleX + (paddleWidth/4)) { //Reverse speed and add one to score speed = speed * -1; score += 1; //set the verticle speed to a random number between - 0.9 and 0.9 speedX = random ( -0.9, 0.9) ; //set a random colour for the balls in the background red = random(100, 255); blue = random(100, 255); green = random(100, 255); } if (posY > paddleY -10 && posY < paddleY + 10 && //checks if the ball in is the confinds of the paddle posX > paddleX - (paddleWidth/2) && posX < paddleX - (paddleWidth/4)) { speed = speed * -1; score += 1; //set the vertical speed to -0.9. speedX = -.9; //set a random colour for the balls in the background red = random(100, 255); blue = random(100, 255); green = random(100, 255); } if (posY > paddleY -10 && posY < paddleY + 10 && //checks if the ball in is the confinds of the paddle posX < paddleX + (paddleWidth/2) && posX > paddleX + (paddleWidth/4)) { speed = speed * -1; score += 1; speedX = .9; //set a random colour for the balls in the background red = random(100, 255); blue = random(100, 255); green = random(100, 255); } //collision for top and bottom of screen else if (posY > height) { speed = speed * -0.99 ; score = 0; time = 0; } else if (posY < -1) { posY = 1; speed *= -1 ; } } //Collision for the side of the Screen void ballXSpeed() { posX = posX + speedX; //checks if if (posX >= width-15 || posX < 0 + 15) { speedX = -speedX; } } //Shrink the paddle based on the score void paddleShrink() { if (score < 5) { paddleWidth = 50; } if (score >= 5) { paddleWidth = 45; } if (score >= 10) { paddleWidth = 40; } if (score >= 15) { paddleWidth = 35; } if (score >= 20) { paddleWidth = 30; } if (score >= 30) { paddleWidth = 25; } } //loop for the background void drawBalls() { //Columns for (int i = 7; i < 400; i = i+40) { //Row for (int j = 7; j < 400; j = j+20) { //first line of circles fill( red, blue, green); ellipse(i, j, 15, 15); //second line of circles fill(score*10, 0, 255); ellipse(i + 20, j + 5, 15, 15); } } }