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).
Hi which one is your newer version &where is the tutorial for that?
ReplyDelete[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".
This is the newer version. I didn't exactly write a tutorial for it, but heavily commented the code instead.
DeleteThe "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.
I keep getting an error with the board.repaint(); of ryour code I have no idea why. Is anyone else getting that error?
DeleteI 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 :)
DeleteHi, Get Android, IOS source code only for $5 and reskinning on unity here
Deletehttps://www.fiverr.com/ruhinaazdev
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 :(
ReplyDeleteYou will want to download all of the .java files, and load them into a project in Eclipse.
ReplyDeleteIF 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.
How to download source code ??
ReplyDeleteHard to image more inconvenient way of downloading sorces.
ReplyDeleteI'd ignorantly assumed more people would understand how to use SVN, but was apparently very wrong.
DeleteA 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!
hey ho can i play the game?
ReplyDeleteI 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.
DeleteWorks! 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.
ReplyDelete(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).
ReplyDeleteOne of the features that Java 7 introduced was the ability to omit redundant generic type parameters. For example;
DeleteList< 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.
where are codes
ReplyDeleteYou can download the source code here:
Deletehttps://github.com/PSNBTech/SnakeRemake
Parabéns pelo código, gostei muito!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteI hope you still look at this page becaues I would love that error to be fixed :) God bless good night :)
ReplyDeleteOk its a cool game but I suggest to put the game in a zip file, so people can download it fast.
ReplyDeletejust a thought...
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...
ReplyDeleteDo you know how can I run correctly the program from prompt?
does anyone in this group know how program snake using Ada program?
ReplyDeletehi...I have problem in 247,248 line(SnakerGame.java) in this code..how can i solve it ?
ReplyDeletethis.snake = new LinkedList<>();
this.directions = new LinkedList<>();
how to change the speed
ReplyDeletecant you change the background image?
ReplyDeletewhere is the code
ReplyDeleteAgen Judi Online
ReplyDeleteAgen Judi
Agen Judi Terpercaya
Agen Bola
Bandar Judi
Bandar Bola
Agen SBOBET
Agen Casino
Agen Poker
Agen IBCBET
Agen Asia77
Agen Bola Tangkas
Prediksi Skor
Prediksi Skor LIVERPOOL VS ASTON VILLA 26 September 2015
Prediksi FC NANTES VS PARIS SAINT-GERMAIN 26 September 2015
Hello. I'm a student who is learning java right now in computer science.
ReplyDeleteIf 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?
This comment has been removed by the author.
ReplyDeleteThank You So Much...
ReplyDeleteThat'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
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?
ReplyDeleteprivate 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) {
e.printStackTrace();
}
can you give me the source code sir?
Deletecan u give me source code..pls i need this code..
ReplyDeletewhere is the source code?
ReplyDeletemay i have class diagram
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteim getting an error Error: Could not find or load main class SnakeGame
ReplyDeleteCaused by: java.lang.ClassNotFoundException: SnakeGame