Tutorial [Java]: Creating a Snake Game [Part 07] - Putting it All Together

Note: I have completely rewritten and improved this game. Most of my traffic comes from these posts, so I've not deleted them. Please use the new version which can be found here.

Finally, we're on the home stretch for the game. All that's left to implement is death and respawning. Let's not waste any time and jump straight in and get this beast finished, shall we?

First let's create a new boolean variable that we can use to determine whether or not the game is over.

private boolean gameOver;

Now let's go back to the update method and implement the gameOver flag. We'll only update the game if the game is focused, and if the gameOver flag is false. If we run into a null tile (The edge of the map) or a snake tile, we'll set the gameOver flag to true.

private void update() {
 if(gameOver || !canvas.hasFocus()) {
  return;
 }
 TileType snakeTile = snake.updateSnake();
 if(snakeTile == null || snakeTile.equals(TileType.SNAKE)) {
  gameOver = true;
 } else if(snakeTile.equals(TileType.FRUIT)) {
  score += 10;
  spawnFruit();
 }
}

Now when we run into ourselves or a wall, the game will stop updating. Now all we have to do is inform the user that they died, and let them reset the game when the want to. Let's start with rendering the game over screen.

private static final Font FONT_LARGE = new Font("Times New Roman", Font.BOLD, 40);

private void render(Graphics2D g) {
 board.draw(g);

 g.setColor(Color.WHITE);

 if(gameOver) {
  g.setFont(FONT_LARGE);
  String message = new String("Final Score: " + score);
  g.drawString(message, canvas.getWidth() / 2
  - g.getFontMetrics().stringWidth(message) / 2), 250);
  g.setFont(FONT_SMALL);
  message = new String("Press Enter to Restart");
  g.drawString(message, canvas.getWidth() / 2
  - g.getFontMetrics().stringWidth(message) / 2), 350);
 } else {
  g.setFont(FONT_SMALL);
  g.drawString("Score: " + score, 10, 20);
 }

Now, as you may have guessed by now, we're going to be using the Enter key to reset the game, so let's go back down to the keyPressed method and add the logic there.

@Override
public void keyPressed(KeyEvent e) {
 //Snake moving code...
 if(e.getKeyCode() == KeyEvent.VK_ENTER && gameOver) {
  resetGame();
 }
}

All that's left is to make resetGame set the gameOver flag to false, so let's add that quickly.

private void resetGame() {
 //Reset code...
 gameOver = false;
}

And that's it! If you did everything right, you should have a fully functioning snake game. If not, you may want to take a look at the source code to see what went wrong. Hopefully you learned a little something from this, if you have any questions feel free to leave a comment below.

Source Code

Posted in , , , , , . Bookmark the permalink. RSS feed for this post.

6 Responses to Tutorial [Java]: Creating a Snake Game [Part 07] - Putting it All Together

  1. Hi thank you for the great tutorial. it is very much appreciated. I am having trouble running the game because the TileType is not recognized. is there a separate .java file for TileType. Thank you.

    ReplyDelete
  2. TileType is an enum that can be found in the GameBoard class. If you need to use it, you'll need to import it using (or the appropriate import, depending on your project packaging:

    import org.psnb.GameBoard.TileType;

    ReplyDelete
  3. so i went through the tutorial and then i went over the code again with the three *.java files(Engine.java, GameBoard.java and Snake.java)provided in the source code. i am using netbeans and named my package Wigglesworth. i a does not exist for every instancee of TileType and also instances of direction under the keypressed method. i did see in the source code a GameBoard$TileType.class and a Snake$Direction.class. so i thought maybe i was missing out on a couple .java files. sorry for the trouble and thanks for the help.

    ReplyDelete
  4. ok thank you for tutorial
    this is good for newbie

    ReplyDelete
    Replies
    1. You're quite welcome. I'd recommend checking out the updated version linked at the top of the page though, it's not as messy and is much easier to understand.

      Delete
  5. source code download nai ho raha plz help

    ReplyDelete

Search

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.