我需要在每次吃蛇时提高它的速度

时间:2019-09-12 08:51:42

标签: java class methods game-engine boolean-logic

这条蛇的速度是由两个值来衡量的,我正试图提高它每次进食的速度

我一直在使用布尔方法和值,但是我找不到应该将值增加到哪个变量,应该减少或保留该值的正确逻辑


    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),但是它对蛇的影响自开始以来就令人难以置信。

2 个答案:

答案 0 :(得分:0)

简短的回答:将speedManager降低1。

但是您只能这样做5次,所以在吃了5种食物之后,它将具有恒定的速度,等于每勾1步。 游戏中的动作要复杂得多。如果您可以访问两次刻度之间的增量时间(实时测量的两次刻度之间的差异),则可以使用浮点数来更平滑地更改蛇的速度。这样,它就不会被整数值那么严重地量化,并且将取决于实时而不是帧速率

答案 1 :(得分:0)

您使用增加移动计数器每勾一个。一旦大于/等于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) 
    }
}