Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
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 1820 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): http://www.mediafire.com/?8l3phfxl0mywu4h
Offline Sickan

Senior Devvie


Medals: 9



« 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 »

ZIP: http://www.mediafire.com/?nqba3jbyb6oacd3
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 »

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.
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 (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.
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: 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.
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.

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

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org 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‑gaming.org
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!