class Pong { //Ball parameters float ballX = 200; float ballY = 200; //Location of players float compX = 20, compY = 200; float humX = 380, humY = 200; //Size of stick float stickW = 10, stickH = 60; //Direction int ballXdirection = 1; int ballYdirection = 1; Pong(int _level) { level = _level; ballSize = ballSize - level * 1.7; ballSpeed = ballSpeed + level * 1.1; playgame = -1; } //----------------------------------------------------------------------------- void drawGameTable() { //External Box and middle line rectMode(CORNER); strokeWeight(2); stroke(255); fill(0); rect(5, 5, 390, 390); line(200,0,200,400); //Game values rect(135,10,125,40); textFont(font, 30); fill(255); text(scoreC, 160, 32); text(scoreH, 225, 32); text("x",192,32); textFont(font, 15); text("Level ", 170, 47); text(level, 220, 47); // If Mouse is pressed play the game - pressed again - reset game if (mousePressed == true){ playgame = playgame * -1; } else if (playgame < 0) { rectMode(CORNERS); fill(200,200,200,50); rect(0,0,400,400); if ((scoreH == 0) && (scoreC == 0)){ fill(255); text("PRESS MOUSE TO START",100,200); } else{ fill(255); text("PRESS MOUSE TO CONTINUE",100,200); } } if (playgame > 0){ pong.moveComputerStick(); pong.moveHumanStick(); pong.drawBall(); } } //----------------------------------------------------------------------------- void drawBall() { // Update the position of the ball ballX = ballX + ( ballSpeed * ballXdirection ); ballY = ballY + ( ballSpeed * ballYdirection ); //Ball on Human side and within human paddle? if((ballX > humX - ballSize/2) && (ballX > 200)){ if ((ballY <= humY + stickH) && (ballY >= humY - stickH)){ ballXdirection *= -1; scoreH = scoreH + 1; } } //Ball on Comp side and within Comp paddle? if ((ballX - ballSize/2 < compX) && (ballX < 200)){ if ((ballY <= compY + stickH) && (ballY >= compY - stickH)){ ballXdirection *= -1; scoreC = scoreC + 1; } } //Dont let ball go of screen vertically if ((ballY > screenH - ballSize/2) || (ballY < ballSize/2)){ ballYdirection *= -1; } //Resset Game if ball falls off the screen if ((ballX > humX) || (ballX < compX)){ playgame = -1; scoreH = 0; scoreC = 0; ballX = 200; ballY = 200; } //Draw Ball fill(random(255),random(255),random(255)); ellipseMode(CENTER); ellipse(ballX, ballY, ballSize, ballSize); } //----------------------------------------------------------------------------- void moveHumanStick() { rectMode(CENTER); //Human Stick Limits if (pmouseY > screenH) { humY = screenH; } else if (pmouseY < 0) { humY = 0; } else { humY = pmouseY; } //Draw Human Paddle fill(0,0,255); rect(humX,humY,stickW,stickH); } //----------------------------------------------------------------------------- void moveComputerStick() { rectMode(CENTER); //Computer Stick Limits if (compY > screenH) { compY = screenH; } else if (compY < 0) { compY = 0; } else{ compY = compY; } //Computer stick fill(255,0,0); compY = ballY; rect(compX,compY,stickW,stickH); } }