Android游戏逻辑 - Obsticles

时间:2015-05-30 06:09:18

标签: java android android-studio logic game-physics

我一直在研究Android应用程序很长一段时间但是现在我决定用我的前微积分决赛创建一个游戏。我已经完成了整个代码,除了一个小问题外,它完美无缺。第一场比赛是基于飞猪(我的同学的脸)避免顶部和底部的osticle。我开发了一种算法,使得obsticles是均匀间隔的,并且基于随机选择放置在屏幕的顶部或底部但不能同时放置两者!。需要改进的算法在第3段代码中!

这是问题的屏幕截图:screenshot here (我的帐户是新的,所以stackoverflow不会让我直接分享照片)

这是调用所有动态对象更新的类(ship = pig,bacon = bonus item, BM = BarrierManager类的更新()更新obsticles

//这将更新资源

void Update(float dt) {
    ship.update(dt);
    //bumbing
    if (!ship.death) {
        background.update(dt);
        **BM.update(dt);**


        for (int i = 0; i % 5 == 0; i++) {

            bacon.update(dt, BM.position);
        }
    }

    ArrayList<Point> bacon_point = new ArrayList<Point>(bacon.getArray());
    if (ship.bump(bacon_point.get(0), bacon_point.get(1), bacon_point.get(2), bacon_point.get(3))) {
        bacon.setX(-200);
        bacon.setY(-200);
        Message msg = BM.game_panel.game.handler.obtainMessage();
        msg.what = 0;
        BM.game_panel.game.handler.sendMessage(msg);

    }

    for (int i = 0; i < BM.TopWalls.size(); i++) {
        ArrayList<Point> temp = new ArrayList<Point>(BM.TopWalls.get(i).getArray());
        //after we have accest the TopWalls arraylist we can call bump that check TopWalls object's points position with the pig's points
        ArrayList<Point> temp2 = new ArrayList<Point>(BM.BottomWalls.get(i).getArray());
        //after we have accest the TopWalls arraylist we can call bump that check BottomWalls object's points position with the pig's points

        if ((ship.bump(temp.get(0), temp.get(1), temp.get(2), temp.get(3))) || (ship.bump(temp2.get(0), temp2.get(1), temp2.get(2), temp2.get(3))) || ship.death) {

            ship.death = true;
            game.onStop();
            while(f==0) {
                MediaPlayer mp = MediaPlayer.create(game, R.raw.explosion_fart);
                mp.start();
                f++;
            }
            f++;
            Message msg = BM.game_panel.game.handler.obtainMessage();
            msg.what = 1;
            BM.game_panel.game.handler.sendMessage(msg);
            i = BM.TopWalls.size()-1;
            if(f == 8){
                thread.setRunning(false);
            }

        }
    }
}

在BarrierManager中我创建了这个更新方法,该方法需要浮动dt = MainTheards游戏的一般时间。 TopWalls是ArrayList,这个ArrayList由顶部obsticles组成。底壁是相同的,但BottomWalls。

// zreb决定是否应该创建TopWalls或BottomWalls对象。此参数稍后转移到我使用它的Barier.update方法

public void update(float dt){
        for (int i=0;i<Num+1; i++) {
            int zreb = new Random().nextInt(2);
            //{if} to make the spacing bigger
            if (i % 5 == 0){
                **TopWalls.get(i).update(dt, true, zreb);
                BottomWalls.get(i).update(dt, false, zreb);**
                if(zreb == 0){
                    position.set(TopWalls.get(i).getX(), TopWalls.get(i).getY());
                }
                else{
                    position.set(BottomWalls.get(i).getX(),BottomWalls.get(i).getY());
                }
            }
        }
    }

现在,Barrier.class中的这个算法是魔术出错的地方。这个更新方法采用前面提到的float dt,一个布尔变量用于确定我们在该实例处理的obsticle是顶部还是底部,zreb random int确定是否将显示顶部或底部obsticle。 / p>

//problem! needs to be discussed
public void update(float dt, boolean b, int zreb) {

    //checking if the barrier is still there
    if (x<-bitmap.getWidth()){
        //'b'is passed from the Barriermanager - 'update' method, determining if we have to use monkey-true or farmer-false
        int raz = 200;
        int vyska = BM.dpos;
        //'zreb' helps me to randomly determine if monkey or ballard is going to appear

        //here it determines if we are going to have Top or Bottom obsticle in the game
        if(zreb == 1) {
            vyska = BM.dpos - raz;
        }
        else {
            vyska = BM.dpos + raz;
        }
        //koniec tohto trienedia

        if (b)
        {
            //setting the y value for the top wall
            y = vyska - BM.dl/2 - bitmap.getHeight()/2;
        }
        else{
            //setting the y value for bottom wall
            y = vyska + BM.dl/2 + bitmap.getHeight()/2;
        }
        //setting x-value
        x = (int) (x +bitmap.getWidth()*(BM.TopWalls.size()-1));
    }

    x = (int) (x - BM.game_panel.ShipSpeed*dt);
}

你知道为什么这个“一个或另一个”的情况不能正常工作吗? 这对我很有帮助,因为这个错误让我从商店停用了应用程序。

0 个答案:

没有答案