Wednesday, April 15, 2015

Pong Assignment

Here's a video explaining what I did

https://drive.google.com/file/d/0B8ZGdT5f3eJkZG5CaGktZnVlRjg/view?usp=sharing

ARDUINO CODE
/////////////////////////////////////////
const int potPin1 = A0;
const int potPin2 = A5;

const int button1=3;
const int button2=5;

void setup() {
 Serial.begin(9600);
}
void loop() {
 int potVal1 = analogRead(potPin1);
 potVal1 = map(potVal1,0,1023,0,127);
 int potVal2 = analogRead(potPin2);
 potVal2 = map(potVal2,0,1023,0,127);

 int buttonVal1 = digitalRead(button1);
 int buttonVal2 = digitalRead(button2);


 Serial.write(buttonVal1);
 delay(10);
 Serial.write(buttonVal2);
 delay(10);
 Serial.write(potVal1);
 delay(10);
 Serial.write(potVal2);
 delay(50);
}
////////////////////////////////////////////

PONG GAME CODE(I didn't take any code from any online sources, everything was written by me)
/////////////////////////////////////////////
import processing.serial.*;
Serial arduinoPort;

//Input Variables
int buttonVal1; // 0 or 1
int buttonVal2;
int potVal1; // 0 to 127(max byte)
int potVal2;

//Screen vars
int scrnW = 800;
int scrnH = 550;

//ball variables
static float bRad = 30;
float bPosX=0;
float bPosY=0;
float bVelX=0;
float bVelY=0;

//paddle variables
float pWidth = bRad;
float pHeight = 6*bRad;
float pPosX=0;
float pPosY=0;
float pVel=8;

void setup(){
  size(scrnW,scrnH);
  bPosX=width/2;
  bPosY=height/2;
 
  println(Serial.list());
  String portName = Serial.list()[0];
  arduinoPort = new Serial(this,portName,9600);
}

void draw(){
  byte [] inputBuffer = new byte[7];
  while(arduinoPort.available()>3){
    inputBuffer = arduinoPort.readBytes();
    arduinoPort.readBytes(inputBuffer);
    if(inputBuffer!=null){
      String myString = new String(inputBuffer);
      buttonVal1 = inputBuffer[0];
      buttonVal2 = inputBuffer[1];
      potVal1 = inputBuffer[2];
      potVal2 = inputBuffer[3];
     
      //println(buttonVal1 + " "+buttonVal2 + " " + potVal1+" "+potVal2);
    }
  }
 
 
 
  //UPDATE PHYSICS
  Update();
 
  //DRAW
  background(255,255,0);
 
  //draw paddle
  fill(0,0,255);
  rect(pPosX,pPosY,pWidth,pHeight,7);
 
  //draw ball
  fill(255,0,0);
  ellipse(bPosX,bPosY, bRad*2, bRad*2);
 
}


//bound of paddle
//0 to height-pHeight/2;
void Update(){
  //move paddle
  if(buttonVal1==1){
    pPosY+=pVel;
    if(pPosY>height-pHeight){pPosY=height-pHeight;}
  }
  if(buttonVal2==1){
    pPosY-=pVel;
    if(pPosY<0){pPosY=0;}
  }
 
  //update Ball Velocity
  float acellX = map(potVal1,6,127,1,-1);
  float acellY = map(potVal2,6,127,-1,1);
  bVelX+=acellX;
  bVelY+=acellY;
  if(bVelX>pVel){bVelX=pVel;}
  if(bVelX<-pVel){bVelX=-pVel;}
  if(bVelY>pVel){bVelY=pVel;}
  if(bVelY<-pVel){bVelY=-pVel;}
 
  //update Ball position
  bPosX+=bVelX;
  bPosY+=bVelY;
 
  //Process ball borders
  ballBorders();
 
  //Process ball-Paddle Collision
  paddleCollision();
}

void ballBorders(){
  float top = bPosY+bRad;
  float bot = bPosY-bRad;
  float left = bPosX-bRad;
  float right = bPosX+bRad;
 
  if(top>height){
    bPosY=height-bRad;
    bVelY*=-1;
  }
  if(bot<0){
    bPosY=0+bRad;
    bVelY*=-1;
  }
  if(right>width){
    bPosX=width-bRad;
    bVelX*=-1;
  }
  if(left<0){
    bPosX=bRad;
    bVelX*=-1;
    println("LOSE");
  }
 
}

void paddleCollision(){
  //find out if there is collision.
  boolean tf = isPaddleCollision();
  //if there is,
  if(tf){
    bVelX*=-1;
    bPosX=bRad+pWidth;
  }
}

boolean isPaddleCollision(){
  float topB = bPosY+bRad;
  float botB = bPosY-bRad;
  float leftB = bPosX-bRad;
  float rightB = bPosX+bRad;
 
  float topP = pPosY+pHeight;
  float botP = pPosY;
  float leftP = pPosX;
  float rightP = pPosX+pWidth;
 
  if(topB<botP) {
    println(1);
    return false;
  }
 
  if(botB>topP){
    println(2);
   return false;
  }
  if(rightB<leftP){
    println(3);
    return false;
  }
  if(leftB>rightP){
    println(4);
    return false;
  }
  println("COLLISION");
  return true;
 
 
}

/////////////////////////////////////////////

No comments:

Post a Comment