Learn Computing from the Experts | The Rheinwerk Computing Blog

How to Create a Snake Game with Java

Written by Rheinwerk Computing | Mar 3, 2025 2:00:00 PM

In this blog post, you'll learn how to create a Snake game with the Java language.

 

A classic computer game is Snake. On the screen is a player, a snake, some gold, and a door. The door and the gold are fixed, the player can be moved, and the snake moves independently towards the player. You must try to move the player to the gold and then to the door. If the snake catches you before you achieve these goals, you’re unlucky, and it’s game over.

 

This game may sound complex at first glance, but it can be simple to program this game with Java:

  • Player, Snake, Gold, and Door are Point objects preconfigured with coordinates.
  • A loop runs through all coordinates. If a player, the door, the snake, or gold has been “hit,” a symbolic representation of the figure is displayed.
  • You’ll test three conditions for the game status: 1) Has the player collected the gold and is standing on the door? (You’ve won the game.) 2) Does the snake bite the player? (You’ve lost the game.) 3) Does the player collect gold?
  • The Scanner enables you to respond to keystrokes and move the player around the board.
  • The snake must move in the direction of the player. While the player can only move horizontally or vertically, the snake can move diagonally.

The corresponding source code for this game follows:

 

public class ZZZZZnake {

 

   public static void main( String[] args ) {

      java.awt.Point playerPosition  = new java.awt.Point( 10, 9 );

      java.awt.Point snakePosition   = new java.awt.Point( 30, 2 );

      java.awt.Point goldPosition    = new java.awt.Point( 6, 6 );

      java.awt.Point doorPosition    = new java.awt.Point( 0, 5 );

      boolean rich = false;

 

   while ( true ) {

      // Draw grid and symbols

 

      for ( int y = 0; y < 10; y++ ) {

         for ( int x = 0; x < 40; x++ ) {

            java.awt.Point p = new java.awt.Point( x, y );

            if ( playerPosition.equals( p ) )

               System.out.print( '&' );

            else if ( snakePosition.equals( p ) )

               System.out.print( 'S' );

            else if ( goldPosition.equals( p ) )

               System.out.print( '$' );

            else if ( doorPosition.equals( p ) )

               System.out.print( '#' );

            else System.out.print( '.' );

         }

            System.out.println();

         }

 

            // Determine status

 

            if ( rich && playerPosition.equals( doorPosition ) ) {

               System.out.println( "You won!" );

               return;

            }

            if ( playerPosition.equals( snakePosition ) ) {

               System.out.println( "SSSSSS. You were bitten by the snake!" );

               return;

            }

            if ( playerPosition.equals( goldPosition ) ) {

               rich = true;

               goldPosition.setLocation( -1, -1 );

            }

 

            // Console input and change player position

            // Keep playing field between 0/0.. 39/9

            switch ( new java.util.Scanner( System.in ).next() ) {

            case "u" /* p */ -> playerPosition.y = Math.max( 0, playerPosition.y - 1 );

            case "d" /* own */ -> playerPosition.y = Math.min( 9, playerPosition.y + 1 );

            case "l" /* eft */ -> playerPosition.x = Math.max( 0, playerPosition.x - 1 );

            case "r" /* ight */ -> playerPosition.x = Math.min( 39, playerPosition.x + 1 );

         }

 

         // Snake moves towards the player

 

         if ( playerPosition.x < snakePosition.x )

            snakePosition.x--;

         else if ( playerPosition.x > snakePosition.x )

            snakePosition.x++;

         if ( playerPosition.y < snakePosition.y )

            snakePosition.y--;

         else if ( playerPosition.y > snakePosition.y )

            snakePosition.y++;

      } // end while

   }

}

 

The Point members in use include the following:

  • The object states x, y: The player and the snake can be moved, and the coordinates must be reset.
  • The setLocation(...) method: Once the gold has been collected, you set the coordinates so that the coordinate from the gold is no longer on our grid.
  • The equals(...) method: Tests if a point is on top of another point. 

If you’re interested in a little more programming on this task, consider the following

enhancements:

  • Player, snake, gold, and door should be set to random coordinates.
  • Instead of just one piece of gold, there should be two pieces.
  • Instead of one snake, there should be two snakes.
  • With two snakes and two pieces of gold, things can get a little tight for the player.
  • Let’s give the player a head start of 5 moves at the beginning without the snakes moving.
  • For advanced developers: The program, which is so far only contained in the main method, should be split into different methods.

Editor’s note: This post has been adapted from a section of the book Java: The Comprehensive Guide by Christian Ullenboom. Christian is an Oracle-certified Java programmer and has been a trainer and consultant for Java technologies and object-oriented analysis and design since 1997.

 

This post was originally published in 3/2025.