LibGDX:出现随机和不同的管道(Flappy Bird Style Game)

时间:2016-09-04 05:54:44

标签: java android random libgdx

我是Android游戏制作场景的新手,在我的第一款游戏中,由于LibGdx,咖啡和Youtube,我成功完成了Flappy Bird克隆。然而,为了使它不是一个完整的克隆,我试图添加不同类型的管道,这是我需要帮助的地方。

我试图做到这一点,以便4种不同类型的管道(或树木,因为它们在我的游戏中)会随机出现以使其变得不可预测但到目前为止我只能设法出现1种类型。以下是我用于2种类型和播放状态的代码。

HTree课程实施:

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();
}

}

HTree2课程实施:

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();
}

}

HPlayState课程实施:

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);
}
}

1 个答案:

答案 0 :(得分:0)

我建议使用类型HTree的接口或超类,然后可以扩展所有其他类型的接口。 E.g。

public abstract class HTree {

    /*
    All your class variables as declared
    in your question
    */

    public HTree(float x, String topTreeFile, String bottomTreeFile) {

        /* Do all the setup stuff here */

        // Set the textures
        toptree = new Texture(topTreeFile);
        bottomtree = new Texture(bottomTreeFile);
    }

    /*
    All your normal getters and setters
    and other functions here
    */
}

然后你将创建4个不同的类,它们都从HTree扩展如下:

/* Different types of tree each extend the main HTree class */

public class HTree1 extends HTree {

    public HTree1(float x) {
        super(x, "uppertree.png", "lowertree.png");

        /* Any other custom code for this tree type 
           can go here */
    }
}

public class HTree2 extends HTree {

    public HTree1(float x) {
        super(x, "uppertree2.png", "lowertree2.png");

        /* Any other custom code for this tree type 
           can go here */
    }
}

/* And so on for HTree3 and HTree4 ... */

最后,您可以将这四种不同类型中的任何一种用作HTree,因为它们都从该类扩展,因此它们可以分组为单个Array<HTree>,并且可以在一个public class HPlayState extends HState { /* Other class variables here ... */ /* Only one array for all different tree types */ private Array<HTree> trees; public HPlayState(GameStateManager gsm) { /* Same setup code ... */ trees = new Array<HTree>(); Random random = new Random(); for (int i = 1; i <= TREECOUNT; i++) { /* Randomly pick which tree type to choose */ int treeType = rand.next(4); // 4 or however many different types of tree you have float x = i * (TREE_SPACING + HTree.TREE_WIDTH); if (treeType == 0) trees.add(new HTree1(x)); else if (treeType == 1) trees.add(new HTree2(x)); else if (treeType == 2) trees.add(new HTree3(x)); else trees.add(new HTree4(x)); } } /* Rendering and updating code here */ } 中一起呈现/更新单循环。

IllegalArgumentException

注意:我还没有对任何代码进行过测试,因此可能会出现一些拼写错误或需要更改,但它应该可以正常工作。