Thursday, May 21, 2015

Ping Pong Final

On May 21, 2015 I had assignment due where I had to add more functionality to a Ping Pong game code that my teacher had taught the class. Below is my new and improved code. I will be including this game in my Capstone Presentation. My code is down below.

PFont f;


int x, y, w, h, maxSpeedX, maxSpeedY;
int paddleX, paddleY, paddleW, paddleH, paddleS;
int paddleX2, paddleY2;
int scorePlayer1 = 0;
int scorePlayer2 = 0;
int winner = 7;
int s1 = 4; // controls speed for ball
int s2 = 4; // controls speed for ball


boolean up, down;
boolean up2, down2;


color color1 = (#FF2600);
color color2 = (#FEFF00);


void setup() {
 size(600, 600);
 background(0);
 frameRate(60);


 x = width/2;
 y = height/2;
 w = 50;
 h = 50;


 maxSpeedX = s1;
 maxSpeedY = s2;


 f = loadFont("ArialRoundedMTBold-48.vlw");
 textFont(f, 25);
 textAlign(CENTER, CENTER);


 rectMode(CENTER);
 paddleX = 25;
 paddleY = height/2;


 paddleX2 = width - 25;
 paddleY2 = height/2;


 paddleW = 25;
 paddleH = 125;
 paddleS = 6;
}


void draw() {
  background(0);


 drawBall();
 moveBall();
 bounceBall();


 drawPaddle();
 movePaddle();
 stopPaddle();


 touchBallPaddle();


 keepscore();
 gameOver();
 
 
 text("Ping Pong Time", 300,25);
}

// begining of drawBall
void drawBall() {
 stroke(#00FFDB);
 strokeWeight(4);
 fill(random(255), random(234), random(213));
 ellipse(x, y, w, h); // ellipse(w,h,x,y) check it out!//
}
// end of drawBall


//Begining of moveBall
void moveBall() {
 x = x + maxSpeedX;
 y = y + maxSpeedY;
}
// end of moveBall


//begining of bounceBall
void bounceBall() {
 if (x > width - w/2) {
   setup();
   maxSpeedX = -maxSpeedX;
   scorePlayer1 = scorePlayer1 + 1;
   
 } else if (x < 0 + w/2) {
   setup();
   maxSpeedX = +maxSpeedX;
   scorePlayer2 = scorePlayer2 + 1;
 }


 if (y > height - h/2) {
   maxSpeedY = -maxSpeedY;
 } else if (y < 0 + h/2) {
   maxSpeedY = -maxSpeedY;
 }
}
// end of bounceBall


//beinging of paddle
void drawPaddle() {
 stroke(#0A0A0A);
 fill(color1);
 rect(paddleX, paddleY, paddleW, paddleH);
 stroke(#0A0A0A);
 fill(color2);
 rect(paddleX2, paddleY2, paddleW, paddleH);
}
// end of paddle

//begining of movePaddle
void movePaddle() {
 if (up) {
   paddleY  = paddleY - paddleS;
 }


 if (down) {
   paddleY = paddleY + paddleS;
 }
 if (up2) {
   paddleY2  = paddleY2 - paddleS;
 }


 if (down2) {
   paddleY2 = paddleY2 + paddleS;
 }
}
// end of movePaddle


//begining os stopPaddle
void stopPaddle() {
 if (paddleY - paddleH/2 < 0) {
   paddleY = paddleY + paddleS;
 }
 if (paddleY +  paddleH/2 > height) {
   paddleY = paddleY - paddleS;
 }


 if (paddleY2 - paddleH/2 < 0) {
   paddleY2 = paddleY2 + paddleS;
 }
 if (paddleY2 +  paddleH/2 > height) {
   paddleY2 = paddleY2 - paddleS;
 }
}
// end of stopPaddle


//begining of touchBallPaddle
void touchBallPaddle() {
 if (x - w/2 < paddleX + paddleW/2 && y - h/2 < paddleY + paddleH/2 && y + h/2 > paddleY - paddleH/2 ) {
   if (maxSpeedX < 0) {
     maxSpeedX = -maxSpeedX;
   }
 }


 //right collision
 else if (x + w/2 > paddleX2 - paddleW/2 && y - h/2 < paddleY2 + paddleH/2 && y + h/2 > paddleY2 - paddleH/2 ) {
   if (maxSpeedX > 0) {
     maxSpeedX = -maxSpeedX;
   }
 }
}
// end of of touchBallPaddle


// begining of keepScore
void keepscore() {
 fill(#DE8BFF);
 text(scorePlayer1, 100, 50);
 text(scorePlayer2, width-100, 50);
}
//end of keepScore


// begining of gameOver
void gameOver() {


 if (scorePlayer1 == winner) {
   gameDone(" Player 1 wins!", color1);
 }


 if (scorePlayer2 == winner) {
   gameDone(" Player 2 wins!", color2 );
 }
}
// end of gameOver


//begining of gameDone
void gameDone(String text, color c) {


 maxSpeedX = 0;
 maxSpeedY = 0;


 fill(random(100), random(150), random(200));
 text(" Game OVER", width/2, height/3 - 40);
 text("Click here to play again!", width/2, height/3+40);
 fill(c);
 text(text, width/2, height/3);


 if (mousePressed) {
   scorePlayer1 = 0;
   scorePlayer2 = 0;
   maxSpeedX = s1;
   maxSpeedY = s2;
 }
}
// end of gameDone


//begining of keyPressed
void keyPressed() {
 if (key == 'w' || key == 'W') {
   up = true;
 }
 if (key == 's' || key == 'S') {
   down = true;
 }


 if (keyCode == UP) {
   up2 = true;
 }
 if (keyCode == DOWN) {
   down2 = true;
 }
}
// end of keyPressed

// begining of keyRealsed
void keyReleased() {
 if (key == 'w' || key == 'W') {
   up = false;
 }
 if (key == 's' || key == 'S') {
   down = false;
 }
 if (keyCode == UP) {
   up2 = false;
 }
 if (keyCode == DOWN) {
   down2 = false;
 }
}
// end of keyrealsed


No comments:

Post a Comment