Posted By

R33D on 08/30/13


Tagged

java game loop


Versions (?)

Basic "Game Loop"


 / Published in: Java
 

Basic game loop in java. Basic loop necessary in any game; hence the term "game loop." Game loops are a fundamental part of learning to write games.

  1. // Run is what I chose to name it, but this is the game loop..
  2. public void run() {
  3.  
  4. //Basic "if statement."
  5.  
  6. if (state == GameState.Running) {
  7.  
  8. //My Character is named "dude" so you'll see him alot, this could called be
  9. anything
  10.  
  11. while (true) {
  12. dude.update();
  13. if (dude.isJumped()) {
  14.  
  15. //character jumped is one of the many images of the dude
  16.  
  17. currentSprite = characterJumped;
  18. } else if (dude.isJumped() == false && dude.isDucked() == false) {
  19. currentSprite = anim.getImage();
  20. }
  21.  
  22.  
  23. // this has to do with the fact that this game was tile based, which is a bit more complex
  24.  
  25. updateTiles();
  26. ab.update();
  27. ab2.update();
  28. bg1.update();
  29. bg2.update();
  30. animate();
  31. repaint();
  32. try {
  33. Thread.sleep(17);
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. if (dude.getCenterY() > 500) {
  38. state = GameState.Dead;
  39. }
  40. }}
  41. }

Report this snippet  

You need to login to post a comment.