Hi,so I am trying to make a snake game(first game).I have snake and food it can eat,but I would like my snake to have tail pieces that follow the head.And lenght of tail should increase after food has been eaten.But I have no idea how to do it. Hope someone understands and can help.
Here is the code I have right now(Eclipse java project using slick libraries): http://www.mediafire.com/?8l3phfxl0mywu4h
Ew! .RAR D:
Make it a zip file or a tarball, at least!
If you’re sharing source, please use a site like github or google code. I simply won’t bother with random archive files on file sharing sites.
http://pastebin.com/FBLsLQ11 I didn’t know that that could be a problem. I have resources too but they cant be pasted into pastebin.I think.
Oh heh, if it’s one file, pastebin works too. Resources are handy to get things runnable, but not really necessary for looking at code. This site also has a handy pastebin (pastebin.java-gaming.org). When it grows beyond that though, I really can’t stress enough the usefulness of a proper project hosting site.
Anyway, impressions of the code: you seem to be representing the entire snake with one polygon, and I seriously doubt that’s going to scale. Every snake game I’ve seen is tile-based, but using polygons could be a nice twist, if a little bit CPU-heavy. If you’re going to go that route, I believe you’re going to have to make the snake body a list of polygons, one per segment, then you’ll need to test whether the head polygon intersects any of the body polygons as well as a bomb or food.
You could be really fancy and use physics to pull each segment along (that would be an awesome snake game if you pulled that off), but chances are you’ll want to start with moving the body along by moving each body segment to be placed and aligned where the segment in front was in the last step.
I had it moving by tiles(20x20),but it was very bumpy and then I changed it to more linear movement.I used polygons because I read somewhere that it was only way to detect colisions in slick. I have no idea how to use physics in slick to drag tail behind head.I even have no idea how to add indefined amount of new objects into game(segments of snake in my case).I could do if statements for every segment, but then I cant add more segments than I have if statements.
Use a list. Something like List segments = new ArrayList(), then a method that creates new segments. Judging by the code, I’d say you might be new to collections, so I would start here: http://docs.oracle.com/javase/tutorial/collections/index.html
Truthfully, if I were designing it I’d break things out into a Snake class that held a single Head and a list of Segment objects, but one thing at a time.
The separate segments is a good idea. That’s how I made my snake game.
To make the body parts follow the head. I’ll tell you how I did it. Have each segment’s location in an array. The first index should be the head. When you move the head the next body part will follow and be in the same spot the head was before you moved it.
So saying that. When you move the head you can take the previous position of the head, and move it down the array to the next piece, and that piece moves gives it’s old position to the next segment, and you repeat the process till you run out of segments.
Lets say you have 3 segments. The head and then 2 more body pieces.
Array of points.
first Index is the head.
Head moves.
2nd segment = head old position.
3rd segment = 2nd segment’s old position.
Ta-Dah.