The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

more help with snake game

Hello all, this is godlynom again, thanks to those who helped me last time. Everything is working, now I just need the body parts to be added after I collide with the food. Essentially everytime you run into a food, you need to grow a body part, or another snake object behind the current one. I have been working on this for quite a bit now and have run into a roadblock, I feel like I need to create an array of SnakeBody objects, but I am confused as to how to go about that. We have not done too many arrays in class, if someone could just at least point me in the right way, that would be wonderful. Here is my code so far, there is some extraneous methods from me trying to figure out how to get it to work.
Food Class:
import java.awt.*;

Re: more help with snake game

I would recommend using a List, more specifically a ArrayList. It's created like any other object and you don't have to worry too much about the low-level details required with arrays (specifically you don't have to manually resize ArrayLists).

Lists are regular objects so you use them as you would any other object:

// create a new list
ArrayList<String> names =new ArrayList<String>();// add items to the list
names.add("Andrew");
names.add("Bill");
names.add("Charles");// print out all items in the listfor(int i =0; i < names.size();++i){System.out.println(names.get(i));}

Re: more help with snake game

So if I were to make many snake objects, would i just create an:

ArrayList <SnakeBody> snake = new ArrayList <SnakeBody>();

and then call them one by one in a for loop based on that?

@KevinWorkman I was not asking you to do this for me, and this is not homework, doing this for fun and practice, just wanted some advice on a direction to go, like helloworld922 did, thought posting the code would help some folks

Re: more help with snake game

they are drawn separately, with different x's and y's, but then the next time they move they all become the original snake's x and y and then are drawn upon each other, so it is all the bodyparts in one box basically

Re: more help with snake game

yea, i tested it out, the problem is in my follow method, what i do is i set the x and y of the bodyparts to the x and y of the snake, so now i need to make it so that it appears behind the snake, but travels at the same velocity and direction

Re: more help with snake game

I have figured it out, the snake game is up and running, everything works, all the collides and all, now to customize it how i want, thanks so much for your help both times Norm and anyone else who also helped