首页 > 解决方案 > 每次吃东西我都需要增加蛇的速度

问题描述

蛇的速度是由两个值来衡量的,我试图在每次它吃东西的时候提高它的速度

我玩过布尔方法和值,但我找不到正确的逻辑,我应该将值增加到哪个变量,我应该减少哪个变量或保持原样


    public class Player {
        public boolean justAte;

        public int moveCounter;
        public int speedManager;

        public Player(Handler handler){
            this.handler = handler;
            moveCounter = 0;
            speedManager = 5; // Whenever I increase this value, the  
                              // snake's initial speed gets slower
            justAte = false;
        }
        public void tick() {
            moveCounter += 1;
            if(moveCounter >= speedManager) {
                checkCollisionAndMove();
                moveCounter = 0;
            }
            /*
             * In the next if statement I attempted to increase the
             * snake's speed each time the Eat() method was being used
             * ;nevertheless, whenever I ate an apple the snake simply
             * went super fast and no change was seen if I ate again.
             */
            if (isJustAte()) { 
            checkCollisionAndMove();
            moveCounter += 5;
            }
        }
        public void Eat() {
            setJustAte(true); 
        }
        public boolean isJustAte() {
            return justAte; 
        }
        public void setJustAte(boolean justAte) {
            this.justAte = justAte; 
        }
    }

在第二个 if 语句中,我尝试添加 (justAte = true),但它对蛇的影响从一开始就是荒谬的速度。

标签: javaclassmethodsgame-engineboolean-logic

解决方案


您使用每刻增加一个移动计数器。一旦它大于/等于 ,您就前进speedManager。这意味着speedManager实际上等于移动之间的刻度数。因此增加它会增加运动之间的等待。如果你想走得更快,你将不得不减少等待。

此外,在您当前的逻辑中,justAte 设置后将始终等于 true,在 gameTick 中您需要在增加移动速度后将其设置为 false 以不再增加它。

使用这种逻辑,您的移动速度不能超过每刻 1 次移动,并且您不能使用非整数的速度。如果您想要更精确的移动,您必须将位置存储为浮点数(尽管显示一个四舍五入的整数)并根据时间在每个刻度上增加一个。大多数游戏引擎使用指示经过时间量的增量工作。

下面是一个例子,如果你只是沿着一个轴(1D)以一定的速度移动某事会是什么样子。

private static final int TICKS_PER_SECOND = 60;
private float position = 0.0f;
private float speed = 2.0f; // specified in per second in this case

/**
 * @param delta  the time passed since gameTick was called the last time in seconds (fraction of a second)
 */
public void gameTick(float delta) {
    //update game elements
    position += speed * delta;  // as speed is specified in distance per second we can just multiply it with the time passed (delta in seconds) and add that to the position
}

public void gameLoop() {
    long lastTick = System.currentTimeMillis(); // the time the last tick occured (in milliseconds since 1970)
    while(true) {
        try { Thread.sleep(1000 / TICKS_PER_SECOND); } catch(InterruptedException e) { e.printStackTrace(); } // tick rate will slightly undercut TICKS_PER_SECOND as this wait doesn't correct for processing time
        long deltaMillis = System.currentTimeMillis() - lastTick; // milliseconds passed since the last tick

        //TODO: perform input

        //update
        gameTick((float)deltaMillis / 1000.0f);

        //TODO: render

        lastTick += deltaMillis; // by not using currentTime but the delta that was processed by gameTick, we keep an accurate time (not slowing down the game due to processing time in the gameTick method) 
    }
}

推荐阅读