---
title: How to Create a Snake Game with Java
description: Learn how to create a classic Snake game in Java, including movement mechanics, collision detection, and game-winning conditions.
image: https://blog.rheinwerk-computing.com/hubfs/How%20to%20Create%20a%20Snake%20Game%20with%20Java.jpg
---

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://blog.rheinwerk-computing.com/) [Blog](https://blog.rheinwerk-computing.com)

- Books 
    - Get an overview of our wide selection of books on every relevant computing topic. 
          - [General Computing](https://www.sap-press.com/rheinwerk-computing/general-computing/)
          - [DevOps](https://www.sap-press.com/rheinwerk-computing/devops/)
          - [Programming Languages](https://www.sap-press.com/rheinwerk-computing/programming-languages/)
          - [Security](https://www.sap-press.com/rheinwerk-computing/security/)
          - [Software Development](https://www.sap-press.com/rheinwerk-computing/software-development/)
- Book Subscription 
    - Get unlimited access to all Rheinwerk Computing books! 
          - [Programming Subscription](https://sap-press.com/subscriptions/)

[Java](https://blog.rheinwerk-computing.com/tag/java)

# How to Create a Snake Game with Java

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)  by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/rheinwerk-computing)

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](https://learning.rheinwerk-computing.com/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*](https://www.amazon.com/Java-Comprehensive-Guide-Programming-Professionals/dp/1493222953?maas=maas_adg_0134E1BE08501412B5E91115AD873E87_afap_abs&ref_=aa_maas&tag=maas)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.

## Recommendation

[![Java](https://blog.rheinwerk-computing.com/hs-fs/hubfs/social-suggested-images/2295-1-1-May-17-2024-09-54-23-3990-PM-May-17-2024-10-42-30-5975-PM.jpg?width=170&name=2295-1-1-May-17-2024-09-54-23-3990-PM-May-17-2024-10-42-30-5975-PM.jpg)](https://www.amazon.com/Java-Comprehensive-Guide-Programming-Professionals/dp/1493222953?maas=maas_adg_0134E1BE08501412B5E91115AD873E87_afap_abs&ref_=aa_maas&tag=maas)

**The Complete Java Manual, from Start to Finish!**

Whether you're just starting out, switching from another language, or looking to deepen your expertise, this is the only Java reference you'll need. It covers everything from core concepts like classes, objects, and exception handling to advanced topics like concurrency, data structures, lambda expressions, and JUnit testing — all updated for Java SE 17 and backed by downloadable code examples throughout.

[Learn More](https://www.amazon.com/Java-Comprehensive-Guide-Programming-Professionals/dp/1493222953?maas=maas_adg_0134E1BE08501412B5E91115AD873E87_afap_abs&ref_=aa_maas&tag=maas)

![Rheinwerk Computing](https://blog.rheinwerk-computing.com/hubfs/RW-logo-300x300.png)

**by [Rheinwerk Computing](https://blog.rheinwerk-computing.com/author/rheinwerk-computing)**

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

[Java](https://blog.rheinwerk-computing.com/tag/java) [Programming Languages](https://blog.rheinwerk-computing.com/tag/programming-languages) [How To](https://blog.rheinwerk-computing.com/tag/how-to)

[![Share on facebook](https://7528309.fs1.hubspotusercontent-na1.net/hub/7528309/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/facebook-color.png?width=24&name=facebook-color.png)](https://www.facebook.com/share.php?u=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Dfacebook) [![Share on linkedin](https://7528302.fs1.hubspotusercontent-na1.net/hub/7528302/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/linkedin-color.png?width=24&name=linkedin-color.png)](https://www.linkedin.com/shareArticle?mini=true&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Dlinkedin) [![Share on twitter](https://7528304.fs1.hubspotusercontent-na1.net/hub/7528304/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/twitter-color.png?width=24&name=twitter-color.png)](https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&url=https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Dtwitter&source=tweetbutton&text=) [![Share on email](https://7528311.fs1.hubspotusercontent-na1.net/hub/7528311/hubfs/raw_assets/public/mV0_d-web-default-modules_hubspot/img/email-color.png?width=24&name=email-color.png)](mailto:?subject=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Demail&body=Check+out+https%3A%2F%2Fblog.rheinwerk-computing.com%2Fhow-to-create-a-snake-game-with-java%3Futm_medium%3Dsocial%26utm_source%3Demail)

### Comments

### Latest Blog Posts

[![How to Create Your First Python Program](https://blog.rheinwerk-computing.com/hs-fs/hubfs/How%20to%20Create%20Your%20First%20Python%20Program.png?height=600&name=How%20to%20Create%20Your%20First%20Python%20Program.png)](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

[Python](https://blog.rheinwerk-computing.com/tag/python)

## [How to Create Your First Python Program](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

[Read More](https://blog.rheinwerk-computing.com/how-to-create-your-first-python-program)

[![SOLID Modeling in Java Programming](https://blog.rheinwerk-computing.com/hs-fs/hubfs/SOLID%20Modeling%20in%20Java%20Programming.png?height=600&name=SOLID%20Modeling%20in%20Java%20Programming.png)](https://blog.rheinwerk-computing.com/solid-modeling-in-java-programming)

[Java](https://blog.rheinwerk-computing.com/tag/java)

## [SOLID Modeling in Java Programming](https://blog.rheinwerk-computing.com/solid-modeling-in-java-programming)

[Read More](https://blog.rheinwerk-computing.com/solid-modeling-in-java-programming)

**Subscribe to our blog!**Get notified about future blog updates.

- <https://www.linkedin.com/showcase/rheinwerk-computing/>
- <https://www.youtube.com/channel/UChj-U-uWGM7qRlHXSFU93ew>
- <https://blog.rheinwerk-computing.com/rss.xml>

### The official Rheinwerk Computing Blog

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes resources that will help you accelerate your computing journey. The Rheinwerk Computing Blog is designed to provide helpful, actionable information on a variety of topics, including programming, administration, security, and analytics!

### Blog Topics

- [All Topics](https://blog.rheinwerk-computing.com)
- [Programming Languages](https://blog.rheinwerk-computing.com/tag/programming-languages)
- [How To](https://blog.rheinwerk-computing.com/tag/how-to)
- [JavaScript](https://blog.rheinwerk-computing.com/tag/javascript)
- [DevOps](https://blog.rheinwerk-computing.com/tag/devops)
- [Web Development](https://blog.rheinwerk-computing.com/tag/web-development)
- [Python](https://blog.rheinwerk-computing.com/tag/python)
- [What Is?](https://blog.rheinwerk-computing.com/tag/what-is)
- [Cybersecurity](https://blog.rheinwerk-computing.com/tag/cybersecurity)
- [Software Development](https://blog.rheinwerk-computing.com/tag/software-development)
- [Java](https://blog.rheinwerk-computing.com/tag/java)

### Blog curated by

[![Rheinwerk Computing Logo](https://blog.rheinwerk-computing.com/hubfs/computing_logo-header.svg)](https://www.sap-press.com/rheinwerk-computing/) [Visit Rheinwerk Computing Store](https://www.sap-press.com/rheinwerk-computing/)

### About

- [Home](https://sap-press.com/)
- [About Us](https://sap-press.com/the-publisher/)
- [Contact](mailto:info@rheinwerk-publishing.com)
- [Legal Notes](https://sap-press.com/legal-notes/)
- [Privacy Policy](https://sap-press.com/privacy/)
- [Terms of Use](https://sap-press.com/terms/)

© 2026 Rheinwerk Publishing, Inc. | Change Privacy Options

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Rheinwerk Computing",
    "url" : "https://blog.rheinwerk-computing.com/author/rheinwerk-computing"
  },
  "dateModified" : "2026-03-13T16:13:06.411Z",
  "datePublished" : "2025-03-03T14:00:00.000Z",
  "headline" : "How to Create a Snake Game with Java",
  "image" : [ "https://blog.rheinwerk-computing.com/hubfs/How%20to%20Create%20a%20Snake%20Game%20with%20Java.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.rheinwerk-computing.com/how-to-create-a-snake-game-with-java",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.rheinwerk-computing.com/hubfs/Logo-1.jpg"
    },
    "name" : "Rheinwerk Publishing, Inc."
  }
}
```