Ping Pong Single Player Game

screen-shot-2017-02-16-at-8-58-30-am

For this week’s assignment, I decided to code and make a simple ping-pong single player game. The following is the code:

 

import javax.swing.JOptionPane;
 //ellipse 1
 float xDir;
 int xPosition=250; int xDirection=0; int yPosition=250; int yDirection=5;
 //paddle
 int paddleX=20; int paddleY=mouseY; int paddleW=10; int paddleH=80; //int paddleSpeed;
 //AI Paddle
 int apaddleX=480; int apaddleY=250; int apaddleW=10; int apaddleH=80; int apaddleSpeed=2;
 //score
 int pscore=0; int ascore=0;

PImage bg_img;

void setup()
 {
 size(500, 500);
 //noStroke();
 smooth();
 frameRate(80);
 rectMode(CENTER);
 bg_img=loadImage("bg02.jpg");
 float xDir=random(-5, 5);
 int a=int(xDir);
 if(a==0)
 {
 xDir=random(-5,5);
 }
 xDirection=a;
 }
 //ellipse 1 animation
 void draw()
 {
 //background
 background(bg_img);
 paddleY=mouseY;
 drawBall();
 gameEngine();
 drawPaddle();
 drawAIPaddle();
 aiEngine();
 paddleBounce();
 win();
 textSize(32);
 fill(#FFF179);
 text(pscore,125,50);
 text(ascore,375,50);
 // text(yPosition,50,50);
 // text(paddleY,50,100);
 // text(xDirection,50,150);
 }

void drawBall()
 {
 fill(#7683FF);
 noStroke();
 ellipse(xPosition, yPosition, 20, 20);
 }

void gameEngine()
 {
 //animation start

xPosition=xPosition+xDirection;
 //if(xPosition>width+10 || xPosition<10)
 //{
 // xDirection=xDirection*-1;
 //}
 yPosition=yPosition+yDirection;
 if(yPosition>height-10 || yPosition<10)
 {
 yDirection=yDirection*-1;
 }
 }

void drawPaddle()
 {
 fill(#6CF27B);
 rect(paddleX, mouseY, paddleW, paddleH);
 }

void drawAIPaddle()
 {
 fill(#DB464E);
 rect(apaddleX, apaddleY, apaddleW, apaddleH);
 }

void aiEngine()
 {
 if(apaddleY<yPosition)
 {
 apaddleY+=apaddleSpeed;
 }
 else
 {
 apaddleY-=apaddleSpeed;
 }
 }

void paddleBounce()
 {
 if(xPosition<30 && yPosition<paddleY+paddleH/2 && yPosition+10>paddleY-paddleH/2)
 {
 xDirection=-xDirection;
 }
 else if(xPosition<-20)
 {
 ascore++;
 reset();
 xDir=-xDir;
 }
 if(xPosition>470 && yPosition<apaddleY+apaddleH/2 && yPosition+10>apaddleY-apaddleH/2)
 {
 xDirection=-xDirection;
 }
 else if(xPosition>520)
 {
 pscore++;
 reset();
 xDir=-xDir;
 }
 }

void reset()
 {
 xPosition=yPosition=250;
 }

void win()
 {
 if(ascore>10)
 {
 int rep = JOptionPane.showConfirmDialog(null, "AI Wins! Let's make it easier?", "AI Wins!", JOptionPane.YES_NO_OPTION);
 if (rep == JOptionPane.YES_OPTION) {
 JOptionPane.showMessageDialog(null, "Difficulty decreased!");
 apaddleSpeed--;
 ascore=pscore=0;
 }
 else {
 int rep2 = JOptionPane.showConfirmDialog(null, "Continue on current difficulty?", "Continue?", JOptionPane.YES_NO_OPTION);
 if(rep2 == JOptionPane.YES_OPTION)
 {
 pscore=ascore=0;
 }
 else
 {
 exit();
 }
 }
 }
 else if(pscore>10)
 {
 int reply = JOptionPane.showConfirmDialog(null, "Player Wins! Let's make it harder?", "Player Wins!", JOptionPane.YES_NO_OPTION);
 if (reply == JOptionPane.YES_OPTION) {
 JOptionPane.showMessageDialog(null, "Difficulty increased!");
 apaddleSpeed++;
 ascore=pscore=0;
 }
 else {
 int reply2 = JOptionPane.showConfirmDialog(null, "Continue on current difficulty?", "Continue?", JOptionPane.YES_NO_OPTION);
 if(reply2 == JOptionPane.YES_OPTION)
 {
 pscore=ascore=0;
 }
 else
 {
 exit();
 }
 }

}
 }

If you want to download and play the game for yourself, click here to download the project files.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s