Bueno ia me mude, en la mayoria, y ya me pusieron internet :D.. y esto que tiene que ver con pulpcore, ni mela, pero pa que vean que he estado ocupado a horrores. Bueno prosiguiendo, estuve revisando el codigo del post anterior, y se nota que lo hize a la volada, pero lo he mejorado y ahora ademas he agregado un obstaculo en la pantalla, primero el principal:
import pulpcore.Stage; import pulpcore.scene.Scene2D; import pulpcore.sprite.FilledSprite; import pulpcore.sprite.ImageSprite; import pulpcore.sprite.Label; import static pulpcore.image.Colors.WHITE; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Julio Cesar Cachay Perez */ public class pelotaobstaculo extends Scene2D{ private ImageSprite pelota; private ImageSprite cuadrados; private double velocidad = 5; private nuevaruta NRPelota; Label infoText; Label tema; Label velo; int i =0; @Override public void load() { add(new FilledSprite(WHITE));// primero agregamos el background NRPelota = new nuevaruta(Stage.getWidth(), Stage.getHeight(), velocidad); pelota = new ImageSprite("res/pelota.png", 140, 240);//cargamos la pelota cuadrados = new ImageSprite("res/Block2.png", 300, 300);//cargamos los cuadrados NRPelota.setAnchoSprite(pelota.width.get()); NRPelota.setLargoSprite(pelota.height.get()); NRPelota.almedio(); pelota.x.set(NRPelota.getX());//lo ponemos al centro pelota.y.set(NRPelota.getY()); add(pelota);//ahora la pelota add(cuadrados); tema = new Label(String.valueOf(Stage.getWidth()) + " , " + String.valueOf(Stage.getHeight()), 320, 20); velo = new Label("", 20, 320); infoText = new Label("", Stage.getWidth() / 4, Stage.getHeight() / 2); add(velo); add(tema); add(infoText); } @Override public void update(int elapsedTime) { mover(); if(pelota.intersects(cuadrados)){ choco(cuadrados); } } private void mover() { NRPelota.nuevaruta(); pelota.x.set(NRPelota.getX()); pelota.y.set(NRPelota.getY()); String ex = String.valueOf(pelota.x.get()); String ey = String.valueOf(pelota.y.get()); infoText.setText(ex + " , " + ey); velo.setText(String.valueOf(NRPelota.velocidadX)+" , "+ String.valueOf(NRPelota.velocidadY)); } private void choco(ImageSprite cua){ NRPelota.cambioDireccionXchoque(cua.x.get(),cua.y.get(),cua.width.get(),cua.height.get()); pelota.x.set(NRPelota.getX()); pelota.y.set(NRPelota.getY()); } }
Ahora el codigo que hace que cuando la pelota choque tenga un comportamiento al azar de rebote
import java.util.Random; /** * * @author Julio Cesar Cachay Perez */ public class nuevaruta { private double x = 0; private double y = 0; private int width; private int height; private double velocidad; public double velocidadX; public double velocidadY; private double anchoSprite = 0; private double largoSprite = 0; public nuevaruta(int width, int height, double velocidad) { this.width = width; this.height = height; this.velocidad = velocidad; velocidadX = velocidad; velocidadY = velocidad; } public double Xmedio() { //calculamos la posicion x media, a partir del ancho del objeto a poner en ese lugar return ((width / 2) - (anchoSprite / 2)); } public double Ymedio() { //calculamos la posicion y media, a partir del largo del objeto a poner en ese lugar return ((height / 2) - (largoSprite / 2)); } private void RutaX() { setX(getX() + velocidadX); } private void RutaY() { setY(getY() + velocidadY); } private boolean chocaX() { if (getX() = width) { return true; } else { return false; } } private boolean chocaY() { if (getY() + largoSprite >= height | getY() <= 0) { return true; } else { return false; } } public void almedio() { x = Xmedio(); y = Ymedio(); } public void nuevaruta() { if (chocaX() & chocaY()) { if (x <= 0) { velocidadX = Math.abs(velocidadX); } if (y = width) { velocidadX = Math.abs(velocidadX) * -1; } if (y + largoSprite >= height) { velocidadY = Math.abs(velocidadY) * -1; } x = x + velocidadX * new Random().nextDouble(); y = y + velocidadY * new Random().nextDouble(); } else { if (chocaX()) { nuevarutaX(); } else { if (chocaY()) { nuevarutaY(); } } } RutaX(); RutaY(); } private void nuevarutaX() { velocidadX = velocidadX * -1; RutaX(); alazarY(); RutaY(); if (chocaY()) { velocidadY = velocidadY * -1; RutaY(); } } private void nuevarutaY() { velocidadY = velocidadY * -1; RutaY(); alazarX(); RutaX(); if (chocaX()) { velocidadX = velocidadX * -1; RutaX(); } } private void alazarY() { Random random = new Random(); if (random.nextDouble() > 0.5) { velocidadY = velocidadY * -1; } y = y + velocidadY * random.nextDouble(); } private void alazarX() { Random random = new Random(); if (random.nextDouble() > 0.5) { velocidadX = velocidadX * -1; } x = x + velocidadX * random.nextDouble(); } /** * @param x the x to set */ public void setX(double x) { this.x = x; } /** * @param y the y to set */ public void setY(double y) { this.y = y; } /** * @param anchoSprite the anchoSprite to set */ public void setAnchoSprite(double anchoSprite) { this.anchoSprite = anchoSprite; } /** * @param largoSprite the largoSprite to set */ public void setLargoSprite(double largoSprite) { this.largoSprite = largoSprite; } /** * @return the x */ public double getX() { return x; } /** * @return the y */ public double getY() { return y; } public void cambioDireccionXchoque(double xO, double yO, double anchoO, double largoO) { if (y - velocidadY > yO & y + velocidadY yO & x + velocidadX < xO + anchoO) { alazarX(); velocidadY = velocidadY * -1; } else { velocidadY = velocidadY * -1; y = y + velocidadY * new Random().nextDouble(); velocidadX = velocidadX * -1; x = x + velocidadX * new Random().nextDouble(); } } } }
Bueno he pulido el codigo, aunque me ha flatado explicarlo un poco, pero es que he tenido sueño, cuando termine la version final prometo subirlo explicado o con su javadoc respectivo.
Alaoz