quarta-feira, 10 de fevereiro de 2016

Jogo PONG - Tarefa 10

Tarefa: considerando também a movimentação vertical, criar uma versão simplificada do jogo Pong, com um único ponto disputado.

int r = 10;
int xC = 10;
int yC = 10;
int dX = 2;
int dY = 2;
int pontoD = 0;
int pontoE = 0;


int yBD = height/2 + 40;
int yBE = height/2 + 40;

boolean z;
boolean e;
boolean w;
boolean p;

void setup() {
  size(320, 240);
  background(0);
}

boolean colisao(int xP, int x1, int x2) {
  if ((xP <= x1) || (xP >= x2)) {
    return false;
  } else {
    return true;
  }
}


void draw() {
  background(197, 202, 233);
  
  if(keyPressed == true) if(keyCode == UP) yBD--;
  if(keyPressed == true) if(keyCode == DOWN) yBD++;
  if(keyPressed == true) if(keyCode == SHIFT) yBE--;
  if(keyPressed == true) if(keyCode == CONTROL) yBE++;
  
  
  fill(63, 81, 181);
  noStroke();
  rect (width - 40, yBD, 20, 80);
  rect (20, yBE, 20 ,80);

fill(255, 64, 129);
ellipse(xC, yC, r*2, r*2);
  
   xC = xC + dX;
   yC = yC + dY;
  
  
  if (xC >=width-r){
   
    pontoE = 1;
    dX = -dX;
  }else if(xC<=r){
    
    pontoD = 1;
    dX = -dX;
  }
  if (yC >=height-r){
    
    dY = -dY;
  }else if(yC<=r){
    dY = -dY;
  }
  
 z = colisao(xC, width - 40, width - 20);
 e = colisao(yC, yBD, yBD + 80);
 if ((z == true) && (e == true)){
   dX = -dX;
 }
 w = colisao(xC, 20, 40);
 p = colisao(yC, yBE, yBE + 80);
 if ((w == true) && (p == true)){
   dX = -dX;
 }
 
 if (pontoD == 1) {
    background(0,0,0,25);
    fill(255);
    text("O lado direito ganhou!", width/3.5, height/2);
  } else if (pontoE == 1) {
    background(0,0,0,25);
    fill(255);
    text("O lado esquerdo ganhou!", width/3.5, height/2);
  }

 if ((colisao(xC, width - 40 - r, width - 20) == true) && (colisao(yC, yBD, yBD + 80) == true)){
  println("direita");
}
  if ((colisao(xC, 20, 40)== true) && (colisao(yC, yBE, yBE + 80) == true)){
  println("esquerda");
}
 
}

Nenhum comentário:

Postar um comentário