I am creating an implementation of Snakes and ladders
and I run into some issues.The game is updating too fast and I want to limit the framerate kinda,or make a pause so that the move is actually visible.I tried using Thread.sleep() but that created issues of its own.Have you solved any similar problems that could point me to the right direction?
I have to point out that run method is in an abstract class and my actual game extends this abstract class
This is the game loop :
public void run() {
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while (running) {
long now = System.nanoTime();
delta = delta + (now - lastTime) / ns;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer = timer + 1000;
if (this.FPSLOG)
System.out.println("FPS: " + frames + " " + "Ticks: " + updates);
frames = 0;
updates = 0;
}
}
stop();
}
The render method :
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(background);
g.fillRect(0, 0, getWidth(), getHeight());
draw(g);
g.dispose();
bs.show();
}
The game implementation of those methods:
@Override
public void init() {
setBackground(Color.black);
board = new Board();
player = new Player(Color.cyan,board.getTiles()[0]);
dice = new Random();
}
@Override
public void draw(Graphics g) {
board.draw(g);
player.draw(g);
}
@Override
public void update() {
for (int i = 0; i < dice.nextInt(7)+1; i++) {
player.move();
}
}
public void move(){
this.currentTile = this.currentTile.getNext() == null ? this.currentTile : this.currentTile.getNext();
this.xPos = this.currentTile.getxPos();
this.yPos = this.currentTile.getyPos();
}
update()once per second. Is that what is happening? \$\endgroup\$