Pong

Esta es la última versión que himos en clase. Se juega con las letras x, s y k, m.

This browser does not have a Java Plug-in.
Get the latest Java Plug-in here.

Aquí está para bajar comprimida en un .zip Falta crear la narrativa de juego, arrancar, puntaje, perder, etc.

Acá el código inicial:

 int altoPantalla =500;
 int anchoPantalla = 500;
 int x = (anchoPantalla*2)/100;
 int y = altoPantalla/2;
 int Vel = 10;
 int x2 = anchoPantalla-(anchoPantalla*2)/100;
 int y2 = altoPantalla/2;
 int altoRect = 50;
 boolean paRriba, paBajo;

 void setup(){
   size (anchoPantalla,altoPantalla, JAVA2D);
   paRriba = false;
   paBajo = false;
   //frameRate(5);
 }


 void draw(){
    rectMode(CENTER);
    background(200,100,50);
    rect (x,y,10,50);
    rect (x2,y2,10,50);

    if (paRriba && y > (altoRect/2)) {
      y = y - Vel;  
    } else if (paBajo && y < (altoPantalla-(altoRect/2))) {
      y = y + Vel;
    }

    if (mousePressed  && (mouseButton == LEFT)  
    && y2 < (altoPantalla-((altoPantalla*5)/100))) {
      y2 = y2 + Vel;
    }
    if (mousePressed  && (mouseButton == RIGHT) 
    &&  y2 > (altoPantalla-((altoPantalla*95)/100))) {
      y2 = y2 - Vel;
    }
  }

 void keyPressed() {
    if (key == ‘s’) {
      paRriba = true;  
    } else if (key == ‘x’) {
      paBajo = true;
    }
 }

 void keyReleased() {
   if (key == ‘s’) {
      paRriba = false;  
   } else if (key == ‘x’) {
     paBajo = false;
   }
 }

Distancia

Un ejemplo sencillo para entender el algoritmo de distacia a un objeto circular:

 int posX, posY;

 void setup(){
   size(400,400);
   ellipseMode(CENTER);
   posY = 100;
   posX = 100;
 }

 void draw(){
   float distancia = sqrt(pow(mouseY-posY, 2) 
   + pow(mouseX-posX, 2));
   if (distancia > 25){
     //println(distancia);
     fill(255,0,90);
   } else {
     fill(255);
   }
   ellipse(posX,posY, 50,50);
 }

Programacion /Juego