Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (636)
Games in Android Showcase (178)
games submitted by our members
Games in WIP (686)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Snake game tail  (Read 2493 times)
0 Members and 1 Guest are viewing this topic.
Offline 111poiss111

Senior Newbie

« Posted 2012-06-11 15:58:05 »

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):
Offline Sickan
« Reply #1 - Posted 2012-06-11 15:59:38 »

Ew! .RAR D:

Make it a zip file or a tarball, at least!
Offline 111poiss111

Senior Newbie

« Reply #2 - Posted 2012-06-11 16:04:19 »

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel

Medals: 202

« Reply #3 - Posted 2012-06-11 16:05:41 »

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.
Offline 111poiss111

Senior Newbie

« Reply #4 - Posted 2012-06-11 16:13:34 » I didn't know that that could be a problem. I have resources too but they cant be pasted into pastebin.I think.
Offline sproingie

JGO Kernel

Medals: 202

« Reply #5 - Posted 2012-06-11 16:21:35 »

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 (  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.
Offline 111poiss111

Senior Newbie

« Reply #6 - Posted 2012-06-11 17:07:28 »

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.
Offline sproingie

JGO Kernel

Medals: 202

« Reply #7 - Posted 2012-06-11 17:45:17 »

I even have no idea how to add indefined amount of new objects into game(segments of snake in my case)

Use a list.  Something like List<Polygon> segments = new ArrayList<Polygon>(), 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:

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.
Offline Kinaces

Junior Newbie

Wokka Wokka Wokka

« Reply #8 - Posted 2012-06-11 18:09:27 »

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.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Dwinin (63 views)
2015-11-07 13:29:08

Rems19 (77 views)
2015-10-31 01:36:56

Rems19 (69 views)
2015-10-31 01:32:37

williamwoles (103 views)
2015-10-23 10:42:59

williamwoles (91 views)
2015-10-23 10:42:45

Jervac_ (105 views)
2015-10-18 23:29:12

DarkCart (131 views)
2015-10-16 00:58:11

KaiHH (114 views)
2015-10-11 14:10:14

KaiHH (153 views)
2015-10-11 13:26:18

BurntPizza (167 views)
2015-10-08 03:11:46
Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48

Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!