Tutorial [Java]: Snake Game Remake

The first thing I wrote when I made this blog was a 7-part tutorial series on writing a snake game in Java. Unfortunately, due to inexperience and the tutorial format, the code wasn't very well written and the result was rather sloppy. I decided to spend much of this morning rewriting a greatly improved version of the game from scratch.

The format for this post will be pretty much identical to my Tetris Tutorial. That is, the entire source will be will documented and available at the bottom of this post. If you have any questions about the code, or bugs to report, feel free to send me a message on YouTube, or leave a comment here.

Here are a list of improvements from the original version of the game:

  • Larger grid (Was 20x20, now 25x25).
  • Improved Input (Fixed the input polling system from the first series).
  • Framerate now independent of update rate.
  • Snake head looks different than body.
  • Snake has a minimum length of 5.
  • New sidebar displays statistics and controls.
  • Improved scoring system (Was 100 points each, now decreases depending on how long it takes to reach it).
  • Now uses pure Swing (Old version mixed AWT Canvas with Swing JFrame).

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

33 Responses to Tutorial [Java]: Snake Game Remake

  1. Hi which one is your newer version &where is the tutorial for that?
    [note: I can see "Download jar and" and " Download source". I can't open any of those in my computer. It say's "couldn't find the main class: org.psnbtech.SnakeGame. The program will exit".

    1. This is the newer version. I didn't exactly write a tutorial for it, but heavily commented the code instead.

      The "Download Source" link just leads to the svn repository for this particular Eclipse project. The raw source code can be found in the src directory.

      Just look at the source code files and read through the comments that I've written to get a better understanding of their function. Of course, if anything is unclear, feel free to ask.

      Also, the Jar executes just fine for me, so I don't know why you're unable to run it.

    2. I keep getting an error with the board.repaint(); of ryour code I have no idea why. Is anyone else getting that error?

    3. I realized the error was I missed part of the code :) Human error, now I can not figure out why the keys don't work other than that everything esle is working good :)

    4. Hi, Get Android, IOS source code only for $5 and reskinning on unity here

  2. How should I put all the codes of SNAKE REMAKE at once in the NETBEANS IDE & make it work as I'm a novice I've no IDEA :(

  3. You will want to download all of the .java files, and load them into a project in Eclipse.

    IF you don't get adn use the .project file, you will need to delete the project entry at the beginning of each file. Once all files are loaded in, save all, and the errors should disappear.

  4. How to download source code ??

  5. Hard to image more inconvenient way of downloading sorces.

    1. I'd ignorantly assumed more people would understand how to use SVN, but was apparently very wrong.

      A lot of people seemed to be having trouble downloading the source, so I've moved the source over from GoogleCode to GitHub just now. If you're not using some sort of version control software, you can just click the "Download Zip" button on the bottom right to download the project directly.

      Hopefully this solves everyone's problems!

  6. Replies
    1. I don't distribute the jar files for these projects anymore. You're going to need to run it from the command line, or load the project into an IDE like Eclipse or NetBeans and run it from there.

  7. Works! But I have to update two lines in SnakeGame.java file (#248 and 249 with empty <>). My compiler doesn't like them. When I put explicit and no more error messages. Thanks.

  8. (in addition to 2:34). I mean explicitly name parameters Point and Direction. For some reason blog doesn't publish those names in angle brackets (I guess it tried to interpret them as html tags).

    1. One of the features that Java 7 introduced was the ability to omit redundant generic type parameters. For example;

      List< String > list = new ArrayList<>();

      Since the type of list is known to be List, the compiler knows that the generic type for the ArrayList is also String, so there's no need to write it twice.

      Anything before Java 7 still requires the generic types to be explicitly stated, however.

  9. Replies
    1. You can download the source code here:

  10. Parabéns pelo código, gostei muito!

  11. This comment has been removed by the author.

  12. I hope you still look at this page becaues I would love that error to be fixed :) God bless good night :)

  13. Ok its a cool game but I suggest to put the game in a zip file, so people can download it fast.
    just a thought...

  14. I tried to compile (javac *.java) and exec it (java SnakeGame) from prompt, but I had the following problem: "Exception in thread "main" java.lang.NoClassDefFoundError: SnakeGame (wrong name: org/psnbtech/SnakeGame)" and then a list of the classes where I got the problem...
    Do you know how can I run correctly the program from prompt?

  15. does anyone in this group know how program snake using Ada program?

  16. hi...I have problem in 247,248 line(SnakerGame.java) in this code..how can i solve it ?
    this.snake = new LinkedList<>();
    this.directions = new LinkedList<>();

  17. cant you change the background image?

  18. where is the code

  19. Hello. I'm a student who is learning java right now in computer science.
    If I would like to make the SNAKE game but have to use methods such as update, paint,keypressed, keyreleased, and keytyped, How should I do so?

  20. This comment has been removed by the author.

  21. Thank You So Much...
    That's so clean and helpful codes and wroted with OOP way.
    I wont to learn JavaSE perfectly but I started 7 month ago to programming but developing has been my hobby,everyday im working 6 hours with examples and I created 4 simple program(small music player(.mp3; .wav),live weather(with animated theme), calendar(add note to day),student control app(working on database)) after that what can I learn more?From where?Anybody have a idea about that?
    Thanks for everyone

  22. Thanks for the tutorial in advance. I've just started writing this game. I don't understand why do all these variable have exactly the following values?
    private static final long FRAME_TIME = 1000L / 50L;

    long delta = (System.nanoTime() - start) / 1000000L;
    if(delta < FRAME_TIME) {
    try {
    Thread.sleep(FRAME_TIME - delta);
    } catch(Exception e) {

  23. can u give me source code..pls i need this code..



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