//GLOBAL VARIABLES PFont font; int rightBatYPos = 245; int leftBatYPos = mouseY; float ballXPos = 245; float ballYPos = 245; float ballYVelocity = -1; float ballXVelocity = -3; int player1 = 0; int player2 = 0; void setup() { font = loadFont("ArialMT-48.vlw"); textFont(font, 25); fill(#FFFFFF); size(500,500); ellipseMode(RADIUS); smooth(); } void draw() { text(player1, 20, 30); background(0); leftBat(); rightBat(); moveBall(); offScreen(); rightBatHit(); leftBatHit(); rightBatMissTopFix(); leftBatMissTopFix(); diag(); player1Win(); player2Win(); } //LEFT BAT void leftBat() { rect(30,mouseY-20,5,40); text(player1, 20, 30); } //RIGHT BAT void rightBat() { rect(width-30,rightBatYPos,5,40); text(player2, width-23, 30); } void keyPressed() { if (keyCode == UP) { rightBatYPos -= 15; } if (keyCode == DOWN) { rightBatYPos += 15; } } //BALL void moveBall() { ballXPos += ballXVelocity; ballYPos += ballYVelocity; ellipse(ballXPos, ballYPos, 5, 5); } //OFF SCREEN void offScreen() { if (ballXPos > width) { ballXVelocity = -ballXVelocity; } if (ballXPos < 0) { ballXVelocity = -ballXVelocity; } } //BAT HIT void rightBatHit() { if ((ballXPos > width-35) && (ballYPos <= rightBatYPos+40)) { ballXVelocity = -ballXVelocity; } } //RIGHT BAT FIX - So ball reaches width above bat void rightBatMissTopFix() { if ((ballXPos > width-35) && (ballYPos < rightBatYPos)) { ballXVelocity = -ballXVelocity; } } void leftBatHit() { if ((ballXPos < 35) && (ballYPos <= mouseY+20)) { ballXVelocity = -ballXVelocity; } } //LEFT BAT FIX - So ball reaches width above bat void leftBatMissTopFix() { if ((ballXPos < 35) && (ballYPos < mouseY-20)) { ballXVelocity = -ballXVelocity; } } //DIAGONAL void diag() { if (ballYPos > height) { ballYVelocity = -ballYVelocity; } if (ballYPos < height-height) { ballYVelocity = -ballYVelocity; } } //SCORING void player1Win() { if (ballXPos > width) { player1 += 1; print("PLAYER 1: "); println(player1); //delay(1000); } } void player2Win() { if (ballXPos < 0) { player2 += 1; print("PLAYER 2: "); println(player2); //delay(1000); } }