for personal practice, I am remaking Flappy bird for desktop with Java, I have managed to get all the pillars generating, screen, bg moving done, but now I have one problem, which is performance.
I sometimes feel that the game isn't moving as fast, and sometimes gets stuck for 0.5 secs or something, but that's not the case, when I move my bird, it's moving a bit weird, looks like its moving too much forward & then to the back, watch the gif in MP4 format:
http://gyazo.com/d7e94c0b772192e5a5dd1d2b61b8c529
What could be causing that? could this be my game loop or the way I draw the graphics? I don't use double buffering, I render simply by calling .repaint in jframe which repaints the JPanel I added.
Game loop:
private void gameLoop() {
new Thread() {
private long last;
private long start;
private int wait;
public void run() {
while(game) {
long now = System.nanoTime();
long length = now - lastLoop;
lastLoop = now;
double delta = length / ((double) Constants.OPTIMAL);
lastFps += length;
fps++;
if (lastFps >= 1000000000) {
System.out.println("FPS: " + fps);
lastFps = 0;
fps = 0;
}
update(delta);
render();
this.sleep
}
}
private void sleep() {
try {
Thread.sleep((this.last - System.nanoTime() + Constants.OPTIMAL) / 1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
My drawing area:
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int translateAmount = this.game.getLevel().getTranslate();
g.translate(-translateAmount, 0);
this.background.render(g2d);
this.game.getLevel().renderLevel(g2d);
g.translate(0, 0);
this.game.getBird().render(g2d);
}
The way I render the backgrounds is this: I add backgrounds one after one, and if a background is outside of the frame, i won't render it.
public void render(Graphics2D g) {
Iterator<RepeatedBackground> itr = this.repeats.iterator();
while (itr.hasNext()) {
RepeatedBackground b = itr.next();
if (this.game.getLevel().getTranslate() - b.getX() >= 300) {
itr.remove();
continue;
}
if (b.getX() - this.game.getLevel().getTranslate() < Constants.WIDTH) {
b.render(g);
}
}
}
This is how I move my bird (i dont use delta, used some tutorial on this):
private void update(double delta) {
if (System.currentTimeMillis() - this.level.getTime() >= Constants.TRANSLATE_SPEED) {
// move background
this.level.updateTranslate();
this.level.setTime();
// move bird
this.getBird().move();
}
}
public void move() {
this.x += 2 / 1.10;
}
What could be causing lag to the bird or the background? Is there something wrong with my rendering ways or game loop?
Fps always printing this:
FPS: 1724172
FPS: 1551857
FPS: 1494378
FPS: 1471987
FPS: 1434095
FPS: 1629905
Constants.OPTIMAL
set to? You never call sleep? – MadProgrammer