How to program a game loop with pausing? <p>To begin with I'm programming a simple Snake recreation in Java and I need your advice on how to go about gameloop/pausing in the game. Right now the game is fully working but I've used a Timer class in my code and I'm not sure if it's the right way. My questions are what is the best way to code game loop and pausing the game, and how to go about speeding up the snake (I want to have 4 different levels) ? I attach my current "gameloop".</p> <p>Here's an initialization of timer:</p> <pre><code> level = model.getLevelTime(); gameTimer = new Timer(level,this); gameTimer.start(); </code></pre> <p>Here's a loop.</p> <pre><code>public void actionPerformed(ActionEvent event) { gameLoop(); } public void gameLoop() { view.passArrays(model.snake.getPosX(), model.snake.getPosY()); view.snakePanel.updateMoveDirection(model.snake.getUp(), model.snake.getDown(), model.snake.getRight(), model.snake.getLeft()); if(model.snake.getGrown()) model.snake.addBodyPart(); model.snake.moveSnake(); model.fruit.checkIfEaten(model.snake.getXPos(), model.snake.getYPos()); view.passApplePos(model.fruit.getAppleXPos(), model.fruit.getAppleYPos()); view.snakePanel.setScore(model.getScore()); refresh(); checkGameOver(); defineActions(); } </code></pre>