蛇游戏:画身体

时间:2019-01-06 18:18:55

标签: java android

几天来,我一直在努力解决这个看似简单的问题。我希望蛇在进食时通过在蛇头所在的位置绘制一个新的矩形来生长。

蛇的身体被存储在一个ArrayList中,我一直在吃东西的时候向它添加一个rect。我可以绘制蛇的身体,但是问题是,如果我尝试在for循环中删除身体的最后一部分,则它的绘制速度与绘制的速度一样快,因此蛇不会长出。如果我删除了for循环之外的身体的最后一部分,那么它的生长速度将比被删除的快,并且它的生长只是不吃食物。

这是跟踪运动的地方

public void reset() {
    // reset player
    setObject(player);

    //reset apple
    setObject(apple);
}

public void setObject(GameObject obj) {
    int x = (int) (Math.random() * cols);
    int y = (int) (Math.random() * rows);
    x = x * Constants.SCALE;
    y = y * Constants.SCALE;
    if (obj instanceof Snek) {
        playerPoint = new Point(x, y);
        obj.update(playerPoint);
    }
    if (obj instanceof Snack) {
        snackPoint = new Point(x, y);
        obj.update(snackPoint);
    }
}

@Override
public void recieveTouch(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Move Left
            if (!gameOver && !movingRight && event.getX() < (Constants.SCREEN_WIDTH / 4) && event.getY() > (3 * Constants.SCREEN_HEIGHT / 4)) {
                movingPlayer = true;
                movingLeft = true;
                movingRight = false;
                movingDown = false;
                movingUp = false;
            }

            // Move Right
            else if (!gameOver && !movingLeft && event.getX() > (3 * Constants.SCREEN_WIDTH / 4) && event.getY() > (3 * Constants.SCREEN_HEIGHT / 4)) {
                movingPlayer = true;
                movingLeft = false;
                movingRight = true;
                movingDown = false;
                movingUp = false;
            }

            // Move Down
            else if (!gameOver && !movingUp && event.getX() > (Constants.SCREEN_WIDTH / 4) && event.getX() < (3 * Constants.SCREEN_HEIGHT / 4) && event.getY() > (7 * Constants.SCREEN_HEIGHT / 8)) {
                movingPlayer = true;
                movingLeft = false;
                movingRight = false;
                movingDown = true;
                movingUp = false;
            }

            //Move Up
            else if (!gameOver && !movingDown && event.getX() > (Constants.SCREEN_WIDTH / 4) && event.getX() < (3 * Constants.SCREEN_HEIGHT / 4) && event.getY() > (3 * Constants.SCREEN_HEIGHT / 4) && event.getY() < (7 * Constants.SCREEN_HEIGHT / 8)) {
                movingPlayer = true;
                movingLeft = false;
                movingRight = false;
                movingDown = false;
                movingUp = true;
            }

            if (gameOver && System.currentTimeMillis() - gameOverTime > 2000) {
                reset();
                gameOver = false;
            }
            break;
    }
}

// Draw text in the center of phone
private void drawCenterText(Canvas canvas, Paint paint, String text) {
    paint.setTextAlign(Paint.Align.LEFT);
    canvas.getClipBounds(r);
    int cHeight = r.height();
    int cWidth = r.width();
    paint.getTextBounds(text, 0, text.length(), r);
    float x = cWidth / 2f - r.width() / 2f - r.left;
    float y = cHeight / 2f + r.height() / 2f - r.bottom;
    canvas.drawText(text, x, y, paint);
}


@Override
public void update() {
    // Always moving in one direction
    if (movingLeft && movingPlayer)
        playerPoint.set(playerPoint.x - Constants.SCALE, playerPoint.y);
    if (movingRight && movingPlayer)
        playerPoint.set(playerPoint.x + Constants.SCALE, playerPoint.y);
    if (movingDown && movingPlayer)
        playerPoint.set(playerPoint.x, playerPoint.y + Constants.SCALE);
    if (movingUp && movingPlayer)
        playerPoint.set(playerPoint.x, playerPoint.y - Constants.SCALE);

    // Game Over
    if (playerPoint.x < 0 || playerPoint.x > Constants.SCREEN_WIDTH || playerPoint.y < 0 || playerPoint.y > Constants.SCREEN_HEIGHT) {
        gameOver = true;
        movingPlayer = false;
        movingLeft = false;
        movingRight = false;
        movingDown = false;
        movingUp = false;
    }

    // Eats apple
    if (player.eat(apple)) {
        setObject(apple);
    }

    player.update(playerPoint);
    player.update();
}


@Override
public void draw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    player.draw(canvas);
    apple.draw(canvas);
    if (gameOver) {
        Paint paint = new Paint();
        paint.setTextSize(100);
        paint.setColor(Color.MAGENTA);
        drawCenterText(canvas, paint, "Game Over");
    }
}

这是蛇类

public class Snack implements GameObject {

private Rect snack;
private int colour;

public Rect getSnack() {
    return snack;
}

public Snack(Rect snack, int colour) {
    this.snack = snack;
    this.colour = colour;
}

@Override
public void draw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(colour);
    canvas.drawRect(snack, paint);
}

@Override
public void update() {

}

public void update(Point point) {
    snack.set(point.x - snack.width() / 2, point.y - snack.height() / 2
            , point.x + snack.width() / 2, point.y + snack.height() / 2);
}
}

蛇之间的间隙也太大,我想是因为我将帧速率设置为8 FPS。如果我将它设置得更高,蛇会移动得太快而无法进行游戏。

0 个答案:

没有答案