我想创建一个+分数系统

时间:2016-11-15 15:33:51

标签: android crash

所以我创造了一个游戏,你可以移动你的角色,你需要传递那个角色,但我有一个问题。当第一行到达我的屏幕底部时,分数开始计数。我想这样做,当我的角色通过线条间隙时,得分将+1。我该怎么办? 编辑:当最后一行达到最低点时,得分也会计算在内!

 if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT ){
        int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap));
         obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
        obstacles.remove(obstacles.size()-1);

        score++;

    }

}

public void draw(Canvas canvas){
    for(Obstacle ob : obstacles)
        ob.draw(canvas);
    Paint paint = new Paint();
    paint.setTextSize(100);
    paint.setColor(Color.GREEN);
    canvas.drawText("" + score, 50, 100 + paint.descent()- paint.descent(), paint);

编辑:

int currY = -5*Constants.SCREEN_HEIGHT/4, rect;
    while(currY < 0){
        int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH-     playerGap));
        obstacles.add(new Obstacle(obstacleHeight, color, xStart, currY, playerGap));
        currY += obstacleHeight + obstacleGap;
    }
    if (obstacles.get(obstacles.size() - 1).getRectangle().top >= currY) {
        score++;
    }

EDIT2:

 private void populateObatacles(){
    int currY = -5*Constants.SCREEN_HEIGHT/4;
    while(currY < 0){
        int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap));
        obstacles.add(new Obstacle(obstacleHeight, color, xStart, currY, playerGap));
        currY += obstacleHeight + obstacleGap;
    }
}

EDIT3:

  if (obstacles.get(lastLineScored-1).getRectangle().top >= currY) {
            score++;
            lastLineScored--;
        }

        if (obstacles.get(obstacles.size()-1).getRectangle().top >=  Constants.SCREEN_HEIGHT ){
            int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap));
            obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
            obstacles.remove(obstacles.size()-1);
            lastLineScored++;
        }
        }
    }

 public void draw(Canvas canvas){
    for(Obstacle ob : obstacles)
        ob.draw(canvas);
    Paint paint = new Paint();
    paint.setTextSize(100);
    paint.setColor(Color.GREEN);
    canvas.drawText("" + score ,  50, 100 + paint.descent()- paint.descent (), paint);

EDIT4:     障碍经理http://pastebin.com/6E77QtHj     障碍http://pastebin.com/p33mPrat

1 个答案:

答案 0 :(得分:0)

更改您的测试。而不是检查行的顶部是否大于SCREEN_HEIGHT,检查它是否大于字符的Y坐标。此外,您需要检查每个

<击>

<击>
if (obstacles.get(obstacles.size()-1).getRectangle().top >= /** character Y coordinate */) {
    score++;
}

if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT ){
    int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap));
    obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
    obstacles.remove(obstacles.size()-1);
}

<击>

编辑:您需要跟踪哪些线路已经超过角色,并且只有在新线路经过时才会得分。您需要保留已经评分了哪些行的索引。因此,创建一个字段来保持该索引;我们称之为lastScoredLine。它应该初始化为obstacles.size()。当线条从屏幕上掉下来时,您必须调整该字段。

if (obstacles.get(lastLineScored-1).getRectangle().top >= /** character Y coordinate */) {
    score++;
    lastLineScored--;
}

if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT ){
    int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap));
    obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
    obstacles.remove(obstacles.size()-1);
    lastLineScored++;
}