segunda-feira, 23 de novembro de 2015

MAMI Atividade 10

   O pong se resumiu a união de todos os ensinamentos até agora com a adição de movimentação pelo teclado. A utilização de funções foi fundamental para resumir o código e não torna-lo tão repetitivo.

- Primeiro devia-se criar os elementos que seriam um círculo e dois retângulos.

- Estabelecer os pontos de colisão.

- Criar as regras de colisões e os movimentos dos elementos e o que cada causaria.

Obs: use as teclas UP e DOWN (para o retângulo esquerdo)  5 e 2 ( para o retângulo direito)

Código:

float ex = 160;
float ey = 120;
int xRec1 = 280;
int yRec1 = 80;
int xRec2 = 20;
int yRec2 = 80;
float vx = -0.7;
float vy = -0.5;
void setup()
{
  size(320,240);
  background(0);
  noStroke();


}
boolean Colisaoxa(float xp ,int x1 ,int x2)
{
  if (( xp >= x1) && (xp <= x2))
  {

    println("Dentroxa");
    return true;
  }
  else
  {
    println("Foraxa");
     return false;
  }
}
boolean Colisaoya(float yp ,int y1 ,int y2)
{
  if (( yp >= y1) && (yp <= y2))
  {

    println("Dentroya");
    return true;
  }
  else
  {
    println("Foraya");
     return false;
  }
}
boolean Colisaoxb(float xp ,int x1 ,int x2)
{
  if (( xp >= x1) && (xp <= x2))
  {

    println("Dentroxb");
    return true;
  }
  else
  {
    println("Foraxb");
     return false;
  }
}
boolean Colisaoyb(float yp ,int y1 ,int y2)
{
  if (( yp >= y1) && (yp <= y2))
  {

    println("Dentroyb");
    return true;
  }
  else
  {
    println("Forayb");
    return false;
  }
}

void draw()
{

  background(0);
  ellipse( ex , ey ,20,20);
                                                               
  rect(xRec1 , yRec1 , 20, 80);
  rect(xRec2 , yRec2 , 20 , 80);
 if( (Colisaoxa(ex ,  xRec1 - 10  ,xRec1 + 10)) && (Colisaoya(ey , yRec1  , yRec1 + 80)))
 {
 println("retorna");
 vx = -vx;


 }
  if ((Colisaoxb(ex , xRec2 +10 ,xRec2 + 30)) && (Colisaoyb(ey , yRec2 , yRec2 + 80)))
  {
   println("retorna");
   vx = -vx;
   vx = vx + 0.5 * vx;

 }
 if ((ey <= 10) || (ey >= (height - 10)))
{
 println("retorna");
 vy = -vy;
 vy = vy + 0.5 * vy;
}

ey = ey + vy;
ex = ex + vx;

}

void keyPressed()
{
   if((keyCode == UP) && (yRec2 >= 10))
  {
    yRec2 = yRec2 - 5;
 
  }
  if((keyCode == DOWN) && (yRec2 <= 150))
  {
    yRec2 = yRec2 + 5;
 
  }

  if((key == '5') && (yRec1 >= 10))
  {
    yRec1 = yRec1 - 5;
 
  }
  if((key == '2') && (yRec1 <= 150))
  {
    yRec1 = yRec1 + 5;
 
  }
}




Nenhum comentário:

Postar um comentário