Jul 21 2010
Turboconnard AGD : Exemple 2 Connard jump
Je continue peinard les tests du framework. Hier j’ai essayé de faire “Connard Jump”. Un peu plus de 100 lignes de code pour un gameplay bien connu des détenteurs de webphones (comme ils disent) :
J’ai ajouté un schtroumpfeur de textures qui charge tout les ressources de R.drawable en texture opengl. Du coup on a plus à se soucier de charger les textures on peut directement faire :
stage.addChild(b);
Voila donc la source de Connard Jump :
import java.util.Vector;
import com.turboconnard.core.Agd;
import com.turboconnard.display.Bitmap;
import com.turboconnard.display.Sprite;
import com.turboconnard.events.AccelerometerEvent;
import com.turboconnard.events.Event;
import com.turboconnard.geom.Point3D;
import com.turboconnard.hardware.Accelerometer;
public class HelloWorld2D extends Agd {
private static final float MINISPACE = 40;
private static final double SPACE = 50;
private Bitmap hero;
private float vitY;
private float vitX = 0;
private float cibleX = 0;
private Sprite decors;
private Vector<Sprite> pfCheck;
private float _y;
private float _lastY;
public HelloWorld2D() {
super();
Bitmap b = new Bitmap(R.drawable.background);
b.x = stage.width / 2;
b.y = stage.height / 2;
vitY = -10;
hero = new Bitmap(R.drawable.test);
hero.x = stage.width / 2;
hero.y = stage.height – 5;
decors = new Sprite();
decors.z = 0;//1;
hero.z = 0;//-1;
stage.addChild(b);
stage.addChild(decors);
stage.addChild(hero);
Accelerometer.getInstance().addEventListener(AccelerometerEvent.ON_CHANGE, this);
generateLevel();
}
/**
* create FirstPlatforms
*/
private void generateLevel() {
_y = stage.height;
while (_y > 0) {
_y = _y – (float) (Math.random() * SPACE) – MINISPACE;
addPlateform(_y);
}
}
private void addPlateform(float pY) {
_lastY = pY;
Bitmap pf = new Bitmap(R.drawable.plate);
pf.y = pY;
pf.x = (float) Math.random() * stage.width;
decors.addChild(pf);
}
public void update() {
//gravity
vitY += 0.3;
//acceleration x
cibleX += (vitX – cibleX) / 5;
//if we go up
if (vitY < 0) {
checkAddPF();
} else {
//if we go down
checkPF();
testColision();
}
//move x
hero.x += cibleX;
//bounce against walls
if (hero.x > stage.width) {
hero.x = stage.width;
cibleX *= -1;
}else if (hero.x < 0) {
hero.x = 0;
cibleX *= -1;
}
//loose !
if (hero.y > stage.height) {
vitY *= -0.5;
/*
hero.y = stage.height;
*/
}
}
private void checkAddPF() {
//if hero.y <250 move level
if (hero.y < 250){
_y += vitY;
//addPlateform if offset is > MINISPACE
if( (_lastY – _y )> MINISPACE ) addPlateform(_y – (float) (Math.random() * SPACE) );
decors.y -= vitY;
}else{
//if hero.y >250 move hero
hero.y += vitY;
}
}
private void checkPF() {
pfCheck = new Vector<Sprite>();
for (Sprite b : decors.getDisplayList()) {
if ((b.y + decors.y) > hero.y)
pfCheck.add(b);
if (b.y + decors.y > stage.height)
decors.removeChild(b);
}
hero.y += vitY;
}
private void testColision() {
for (Sprite b : pfCheck) {
if ((b.x – 50) < hero.x && (b.x + 50) > hero.x && (hero.y+hero.height/2) > (b.y + decors.y) ) {
hero.y = (b.y + decors.y -hero.height/2);
vitY = -10;
return;
}
}
}
public void event(Event e) {
if (e.name == AccelerometerEvent.ON_CHANGE) {
Point3D p = (Point3D) e.param;
vitX = p.x;
}
}
}
hop la :

