Home > Source Code > Snake game in Java with source code

Snake game in Java with source code

I have released new version of this game. I hope soon i can post it with tutorial. To download new version please click the bellow link:

Download Snake 1.0.1.rar (mirror 1)

Download Snake 1.0.1.rar (mirror 2)

Features of new version:

  • Added levels and Type. There is only two type “No maze” and “Border maze”.
  • Anyone can add new mazes in this game easily.
  • Snake die bug is fixed.
  • Code Readability is improved.
  • Code is well commented and structured.
  • Developed using Net-Beans IDE.

Screen shots:

Starting of old post

We all play snake game in our mobile. here i am presenting my developed Snake game for desktop which is developed in java. I used Net-Beans IDE. here is the screen shot:

This Project contain two file one is Snake.java and other is Main.java which contain main method. All gaming logic and graphical user interface has coded in Snake.Java and  source code is :

How to Run :

The project source code is here

this application required Net-Beans IDE to run. Open this project in Net-Beans IDE then run this project.

Categories: Source Code
  1. February 21, 2012 at 6:56 am

    where iz da main.java

  2. February 22, 2012 at 4:13 am

    I have included Main.java

  3. ravi
    May 20, 2012 at 12:15 pm

    thanks for the coad

  4. yasmina
    May 24, 2012 at 10:57 pm

    if I want to copy it in eclipse what is the different!!?

    • May 25, 2012 at 4:30 am

      There will be no difference.

      • Lizel Atis
        September 20, 2012 at 12:22 pm

        di ko sya mapa run.

  5. hiba
    June 3, 2012 at 11:16 am

    plz can you put some command on the script so it will be more easy to undestand it because im a new java learner thanks

  6. sky
    June 16, 2012 at 8:11 pm

    in this game snake never dies only 😛

    • June 17, 2012 at 3:51 am

      yah u r ri8 😛 this is a bug.

  7. Asad Bukhari
    August 6, 2012 at 7:11 pm

    thanks dear for source code and nice code hardworking and good game 🙂

  8. gwennie
    September 15, 2012 at 6:33 pm

    Hi! YOU ARE AN EPIC! u know that? u inspired me just now. tnk u.
    but it seems unfinished. how do i make the level?

  9. gwennie
    September 15, 2012 at 7:09 pm

    And for the record, U r not a loser. U r far way better than many (i am one of them). 😀

  10. gwennie
    September 16, 2012 at 5:42 am

    ohh i already have figured out the level. tnx

    • September 24, 2012 at 3:55 pm

      plz send me ur code….

    • September 24, 2012 at 3:57 pm

      plz send me ur code….gwennie..plz……to my mail…:bujjivishali@gmail.com

    • mike
      October 10, 2012 at 10:14 am

      can you post here your code because this snake games doesn’t dies..and the level isn’t work..thanks..

      • Hasib
        October 11, 2012 at 3:52 am

        I have already posted full source code. I m sorry to say,”Snake doesn’t die” is a bug.

  11. gwennie
    September 16, 2012 at 6:08 am

    how will the game be over?

  12. September 16, 2012 at 12:44 pm

    but in this code when snake touches its body…it is nt coming as game over…plz explain me this code….

  13. September 16, 2012 at 12:45 pm

    when will this game terminates…

  14. September 18, 2012 at 12:45 pm

    plz tel me…..wat variables u took…n for every sentence plz write comments………

  15. vaan
    September 25, 2012 at 10:48 am

    Hi . can you send the FULL codes in me ?

  16. vaan
    September 25, 2012 at 10:49 am

    I need it immediately please send it to me 😦 @ vaanallen@yahoo.com.ph . Please

  17. Sree Bhuvan
    October 1, 2012 at 2:14 pm

    nahi chal raha bhai

    • Hasib
      October 11, 2012 at 3:55 am

      It should be run. Plzzz use Netbeans IDE.

  18. October 10, 2012 at 6:46 am

    how about the Game over , what will be the process to end the game . .

    • Hasib
      October 11, 2012 at 3:53 am

      I m sorry to say This is a bug.

  19. Harsh
    October 30, 2012 at 3:14 pm

    Can you explain the code for me please i want to do it by my self
    actually the problem is with the turning of the snake and also in increasing its size. Please explain these two things.

  20. revi
    November 7, 2012 at 4:37 pm

    How to make level games?

  21. dada
    December 4, 2012 at 8:42 pm

    where’s the main method?

    • December 5, 2012 at 4:13 am

      You can find main method in Main.java file.

  22. Rishikesh
    January 15, 2013 at 11:14 am


  23. yuvaraj
    January 28, 2013 at 3:19 pm

    thanks for this coad…..i ill implement level 1-3 in this game…if any one want just send ur mail ID to tis mail(yuvaraja8668@gmail.com)….

    • James
      May 14, 2013 at 11:06 am

      Hahah I know this is late -.-
      Can u send the levels to me in the whole code please? Did u work out a way to fix the no die bug?

  24. mae
    February 5, 2013 at 3:51 am

    nice one…it really works !! great!! but, the level cant be changed..

    • February 5, 2013 at 4:59 am

      Thanks. This app was developed about five years ago. After that no upgrade version has released yet. Coding structure and finishing is also poor.

  25. nunim
    February 8, 2013 at 6:21 am


  26. March 7, 2013 at 2:05 pm

    very nice game……..its very helpful of my project….

  27. Raphael Figueiredo
    March 9, 2013 at 12:38 am

    Can you explain what means the variable “gu”, please?

    • March 11, 2013 at 3:47 pm

      gu is the minimum length of snake which is 3.

  28. jeff
    March 18, 2013 at 11:45 am

    net beans ide ?

  29. March 21, 2013 at 9:34 am

    the code is not complete i tried it..

  30. salman
    March 22, 2013 at 12:30 pm

    what are the components that i require

  31. March 23, 2013 at 1:34 am

    i cant run my project?

  32. Mcolisi
    March 25, 2013 at 1:47 pm

    good code but what about the level? i need it

  33. Naveed
    April 3, 2013 at 9:21 am

    this game is not flexible to create levels for it, if you want to create levels you will make many changes in this game, also there are many other bugs but it is a great job for a student, i have also created a game of snake which is much flexible and bug free

  34. sohail
    May 10, 2013 at 11:38 am

    plzzz tell me the difference b/w snake game for mobile and snake game for PC..????? m waiting…..

  35. May 10, 2013 at 12:37 pm

    why the code if (food == false) {
    growup ();
    food = true;
    Else {}
    lb [gu – 1]. setBounds (LBX [gu – 1], LBY [gu – 1], 10, 10);

    for (int i = 1; i <gu, i + +) {
    if (LBP [0] == LBP [i]) {
    t.setText ("GAME OVER" + score);
    try {
    myt.join ();
    } Catch (InterruptedException ie) {
    Do not run? Touching the wall or touching the snake tail did not stop the game?

  36. eshaan
    May 11, 2013 at 1:50 pm

    please add some levels and border to the game

  37. May 13, 2013 at 12:55 pm

    please add level choice source code?

  38. GJ
    May 25, 2013 at 5:54 pm

    Do you know what changes have to be made to make it able to die? I cannot figure this out.

    • May 29, 2013 at 10:39 am

      Sorry for that. I will try to fix it as early as possible.

      • June 1, 2013 at 5:20 pm

        Updated version of snake game is here.
        Code is well commented hope you will understand.

  39. merry
    May 26, 2013 at 11:15 am

    what are these…?
    runl = false;
    runr = true;
    runu = true;
    rund = true;
    bonusflag = true;

    • May 29, 2013 at 10:38 am

      runl = true means snake is running from right to left side.
      runr = true means snake is running from left to right side.
      runu = true means snake is running from down to upper side.

      bonusflag = true means food of a snake is given.

    • cowboy
      May 29, 2013 at 2:26 pm

      I want to try to fix no die bug myself, what variable keeps track of the snakes body? thats what im confused on

      • June 1, 2013 at 5:20 pm

        Updated version of snake game is here.
        Code is well commented hope you will understand.

  40. s
    May 29, 2013 at 9:00 am

    how u gama

  41. lubz
    May 30, 2013 at 6:46 pm

    can u plz snd me the tutorial of diz game n plz explain me hw to run diz game in netbeans. Really need ua hlp..

    • June 1, 2013 at 5:18 pm

      Updated version of snake game is here.
      Code is well commented hope you will understand.

  42. kraken
    June 2, 2013 at 3:49 am

    ” int[] lbx = new int[300];
    int[] lby = new int[300];
    Point[] lbp = new Point[300];
    Point bfp = new Point();
    Thread myt;”

    what does all of this mean? thanks in advamce

    • June 2, 2013 at 4:26 am

      New version of snake game is here
      This code is well commented hope you will understand. I will post that version soon.

      • kraken
        June 2, 2013 at 1:25 pm

        Yeah, I saw that too. I think both of these projects are really good!:) I was just wondering what all these mean:

        ” int[] lbx = new int[300];
        int[] lby = new int[300];
        Point[] lbp = new Point[300];
        Point bfp = new Point();
        Thread myt;”

        Can you please tell me?

      • kraken
        June 2, 2013 at 1:47 pm

        also on a side note, when I tried the new version everything compiled perfectly. except when I ran it, it wasn’t turning when I pressed the keys.

      • June 2, 2013 at 4:29 pm

        It shouldn’t be happend. 😦 I will check it out as soon as possible.

  43. getamesay
    June 10, 2013 at 1:12 pm

    thanks for your code but can you tell me these code:
    ” int[] lbx = new int[300];
    int[] lby = new int[300];
    Point[] lbp = new Point[300];
    Point bfp = new Point();
    Thread myt;”

  44. tin
    July 6, 2013 at 9:17 am

    can i get the code?? i need it to my java project.

  45. July 12, 2013 at 6:08 am

    You guys do realize that he’s using buttons for the snake and food?
    Other than a POC, this method is horribly wrong.
    What you should do is use some kind of canvas mechanism, instead of rendering the snake as a set of buttons.
    This wont scale much.
    Other than that, awesome job.

    • July 12, 2013 at 3:42 pm

      I have already mentioned that, this code is for beginner. I think canvas mechanism isn’t easy for beginner.

  1. January 20, 2015 at 11:01 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: