//More Realistic Pong! void setup() { //Voids the initial parameters. size(400,400); //Screen Size } float playerX; //Player's horizontal paddle position. float playerY; //Player's vertical paddle position. float ballX = width/2; //Ball's horizontal position. float ballY = random(15,height-15); //Ball's vertical position. float ballDX = random(-10,10); //Ball's horizontal velocity. float ballDY = random(-10,10); //Player's vertical velocity. float computerX; //Computer's horizontal paddle position. float computerY; //Computer's vertical paddle position. int playerScore; //Player's score. int computerScore; //Computer's score. boolean xPlayerCollision; //Did the ball bounce on the player's paddle in the X direction? boolean yPlayerCollision; //Did the ball bounce on the player's paddle in the Y direction? boolean xComputerCollision; //Did the ball bounce on the computer's paddle in the X direction? boolean yComputerCollision; //Did the ball bounce on the computer's paddle in the Y direction? boolean playerBounce; //Did the ball bounce for the player? boolean computerBounce; //Did the ball bounce for the computer? void drawScene() { background(0); //A black backgound. stroke(255); //A white outline. strokeWeight(5); //A thicker outline. line(width/2,0,width/2,height); //A thick, white line down the center of the screen. } void drawPlayer() { rectMode(CENTER); //Center's the player's paddle around the X and Y variable. fill(255); //Makes the paddle black. noStroke(); //No outline. playerX = width/10; //Sets the player's paddle to move along a vertical straight line. playerY = constrain(mouseY,height/16,15*height/16); //Sets the player's paddle to move along a horizontal straight line. rect(playerX,playerY,15,height/8); //Draws the player's paddle. } void drawBall() { ellipseMode(CENTER); //Center's the ball around the X and Y variable. fill(150); //Makes the ball white. noStroke(); //No outline. ballX += ballDX; //Sets the ball to move in the X direction. ballY += ballDY; //Sets the ball to move in the Y direction. ellipse(ballX,ballY,15,15); //Draws the ball. } void drawComputer() { rectMode(CENTER); //Center's the computer's paddle around the X and Y variable. fill(255); //Makes the paddle black. noStroke(); //No outline. computerX = 9*width/10; //Sets the player's paddle to move along a vertical straight line. computerY = ballY; //Makes the paddle follow the ball (for now). constrain(computerY,height/16,15*height/16); //Constrains the player's paddle to move along a horizontal straight line. rect(computerX,computerY,15,height/8); //Draws the computer's paddle. } void drawScoreboards() { fill(255,0,0); //Makes the text RED. text("You: " + playerScore,width/5,height/5); //Draws the player's score. fill(0,0,255); //Makes the text GREEN. text("Computer: " +computerScore,5*width/8,height/5); //Draws the computer's score. } void ballConstraints() { if (ballY > height-15 || ballY < 15) { //If the ball bounces off the top or bottom ... ballDY = - ballDY; //Then the ball will change in vertical direction. } if (ballX > width-15) { //If the ball hits the the right side ... playerScore += 1; //Player gains one point. ballX = width/2; //Resets the ball's X position. ballY = random(15,height-15); //Resets the ball's Y position. ballDX = random(3,6); //Resets the ball's X velocity. ballDY = random(3,6); //Resets the ball's Y velocity. } if (ballX < 15) { //If the ball hits the the left side ... computerScore += 1; //Computer gains one point. ballX = width/2; //Resets the ball's X position. ballY = random(15,height-15); //Resets the ball's Y position. ballDX = random(-6,-3); //Resets the ball's X velocity. ballDY = random(-6,-3); //Resets the ball's Y velocity. } } void paddleBounce() { xPlayerCollision = abs(playerX - ballX) < 10; //Sets the argument for the X collision of the player. yPlayerCollision = abs(playerY - ballY) < height/16; //Sets the argument for the Y collision of the player. xComputerCollision = abs(computerX - ballX) < 10; //Sets the argument for the X collision of the computer. yComputerCollision = abs(computerY - ballY) < height/16; //Sets the argument for the Y collision of the computer. if (xPlayerCollision && yPlayerCollision == true) { //If the player has met both conditions ... playerBounce = true; //Then the ball will bounced. } else { //Otherwise ... playerBounce = false; //It didn't. } if (xComputerCollision && yComputerCollision == true) { //If the computer has met both conditions ... computerBounce = true; //Then the ball will bounced. } else { //Otherwise ... computerBounce = false; //It didn't. } if (playerBounce || computerBounce == true) { //If the ball bounced ... ballDX = -ballDX; //Then the ball changes X velocity. } } void draw() { frameRate(60); //Smoother framerate drawScene(); //Make the scene. drawPlayer(); //Make the player. drawBall(); //Make the ball. drawComputer(); //Make the computer. drawScoreboards(); //Make the scoreboards. ballConstraints(); //Make the ball bounce off the sides. paddleBounce(); //Make the ball bounce off the paddles. }