samedi 3 septembre 2016

LibGDX: Making random and different Pipes appear (Flappy Bird Style Game)

I'm new to the Android game making scene and for my first game, I made a successful Flappy Bird clone thanks to LibGdx, coffee, and Youtube. To make it NOT be a total clone however, I'm trying to add different types of pipes and this is where I need help with.

I'm trying to make it so that the 4 different types of pipes (or trees, as they are in my game) would appear randomly to make it unpredictable but so far I've only ever managed to make 1 type appear. Here are the codes I used for 2 of the types and for my Play State.

public class HTree {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree,bottomtree;
private Vector2 postoptree,posbottree;
private Rectangle boundsTop,boundsBot;
private Random rand;
public HTree(float x){
    toptree = new Texture("uppertree.png");

    bottomtree = new Texture("bottomtree.png");

    rand = new Random();

    postoptree = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree = new Vector2(x, postoptree.y - TREE_GAP - bottomtree.getHeight());

    boundsTop = new Rectangle(getPostoptree().x,getPostoptree().y, toptree.getWidth(), toptree.getHeight());

    boundsBot = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree.getWidth(), bottomtree.getHeight());


}

public Texture getToptree() {
    return toptree;

}

public Texture getBottomtree() {
    return bottomtree;

}

public Vector2 getPostoptree() {
    return postoptree;

}

public Vector2 getPosbottree() {
    return posbottree;
}

public void reposition(float x){
    postoptree.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree.set(x, postoptree.y - TREE_GAP - bottomtree.getHeight());
    boundsTop.setPosition(postoptree.x,postoptree.y);
    boundsBot.setPosition(posbottree.x,posbottree.y);
}

public boolean collides(Rectangle player){
    return player.overlaps(boundsTop) || player.overlaps(boundsBot);
}

public void dispose(){
    toptree.dispose();
    bottomtree.dispose();
}

}

public class HTree2 {
public static final int TREE_WIDTH = 52;
private static final int FLUCTUACTION = 130;
private static final int TREE_GAP = 100;
private static final int LOWEST_OPENING = 120;
private Texture toptree2,bottomtree2;
private Vector2 postoptree2,posbottree2;
private Rectangle boundsTop2,boundsBot2;
private Random rand;
public HTree2(float x){
    toptree2 = new Texture("uppertree2.png");

    bottomtree2 = new Texture("bottomtree2.png");

    rand = new Random();

    postoptree2 = new Vector2(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);

    posbottree2 = new Vector2(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());

    boundsTop2 = new Rectangle(getPostoptree().x,getPostoptree().y, toptree2.getWidth(), toptree2.getHeight());

    boundsBot2 = new Rectangle(getPosbottree().x,getPosbottree().y, bottomtree2.getWidth(), bottomtree2.getHeight());


}

public Texture getToptree() {
    return toptree2;

}

public Texture getBottomtree() {
    return bottomtree2;

}

public Vector2 getPostoptree() {
    return postoptree2;

}

public Vector2 getPosbottree() {
    return posbottree2;
}

public void reposition2(float x){
    postoptree2.set(x, rand.nextInt(FLUCTUACTION) + TREE_GAP + LOWEST_OPENING);
    posbottree2.set(x, postoptree2.y - TREE_GAP - bottomtree2.getHeight());
    boundsTop2.setPosition(postoptree2.x,postoptree2.y);
    boundsBot2.setPosition(posbottree2.x,posbottree2.y);
}

public boolean collides2(Rectangle player){
    return player.overlaps(boundsTop2) || player.overlaps(boundsBot2);
}

public void dispose2(){
    toptree2.dispose();
    bottomtree2.dispose();
}

}

public class HPlayState extends HState {
private static final int TREE_SPACING = 125;
private static final int TREE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50;


private HBird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1,groundPos2;
private Array<HTree> trees;
private Array<HTree2> trees2;



public HPlayState(HGameStateManager gsm) {
    super(gsm);
    bird = new HBird(50,300);
    cam.setToOrtho(false, HBonGame.WIDTH/2,HBonGame.HEIGHT/2);
    bg = new Texture("background3.jpg");
    ground = new Texture("ground.png");
    groundPos1 = new Vector2(cam.position.x-cam.viewportWidth/2,GROUND_Y_OFFSET);
    groundPos2 = new Vector2((cam.position.x-cam.viewportWidth/2) + ground.getWidth(),GROUND_Y_OFFSET);
    trees = new Array<HTree>();
    trees2 = new Array<HTree2>();

    for(int i=1; i <= TREE_COUNT; i++){
        trees.add(new HTree(i * (TREE_SPACING + HTree.TREE_WIDTH)));
    }
    for (int i=1; i >= TREE_COUNT; i++){
        trees2.add(new HTree2(i * (TREE_SPACING + HTree2.TREE_WIDTH)));
    }

}

@Override
protected void handleInput() {
    if (Gdx.input.justTouched())
        bird.jump();

}

@Override
public void update(float dt) {
    handleInput();
    updateGround();
    bird.update(dt);
    cam.position.x = bird.getPosition().x + 80;
    for (int i=0; i < trees.size; i++ ){
        HTree tree = trees.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree.getPostoptree().x + tree.getToptree().getWidth()){
            tree.reposition(tree.getPostoptree().x + ((HTree.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree.collides(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    for (int i=0; i < trees2.size; i++ ){
        HTree2 tree2 = trees2.get(i);

        if (cam.position.x - (cam.viewportWidth / 2) > tree2.getPostoptree().x + tree2.getToptree().getWidth()){
            tree2.reposition2(tree2.getPostoptree().x + ((HTree2.TREE_WIDTH + TREE_SPACING) * TREE_COUNT));
        }

        if(tree2.collides2(bird.getBounds())){
            gsm.set(new HGameOverState(gsm));
        }
    }
    if (bird.getPosition().y <= ground.getHeight()+GROUND_Y_OFFSET){
        gsm.set(new HGameOverState(gsm));
    }
    cam.update();

}

@Override
public void render(SpriteBatch sb) {
    sb.setProjectionMatrix(cam.combined);
    sb.begin();
    sb.draw(bg,cam.position.x - (cam.viewportWidth/2), 0);
    sb.draw(bird.getTexture(),bird.getPosition().x,bird.getPosition().y);
    for (HTree tree : trees) {
        sb.draw(tree.getToptree(), tree.getPostoptree().x, tree.getPostoptree().y);
        sb.draw(tree.getBottomtree(), tree.getPosbottree().x, tree.getPosbottree().y);
    }
    for (HTree2 tree2 : trees2) {
        sb.draw(tree2.getToptree(), tree2.getPostoptree().x, tree2.getPostoptree().y);
        sb.draw(tree2.getBottomtree(), tree2.getPosbottree().x, tree2.getPosbottree().y);
    }
    sb.draw(ground, groundPos1.x,groundPos1.y);
    sb.draw(ground, groundPos2.x,groundPos2.y);
    sb.end();
}

@Override
public void dispose() {
    bg.dispose();
    bird.dispose();
    ground.dispose();
    for (HTree tree : trees){
        tree.dispose();

    }
    for (HTree2 tree2 : trees2){
        tree2.dispose2();

    }


}

private void updateGround() {
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
        groundPos1.add(ground.getWidth() * 2, 0);
    if (cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
        groundPos2.add(ground.getWidth() * 2, 0);



}
@Override
public void resize(int width, int height) {
    bird = new HBird(50,300);
}

}




Aucun commentaire:

Enregistrer un commentaire