• Post Reply Bookmark Topic Watch Topic
  • New Topic

Snake movement

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm having difficulties with snake movement. My snake is a vector that consists of points. Each point is 10*10 filled rect.
My problem is that when i create a snake that has for example 10 points - my program draws that perfectly, but when i want to move my snake - it is only 2 points long. I think the error is in movement functions.
here they are:


hope somebody finds the problem
[ August 09, 2004: Message edited by: Juhan Voolaid ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[fixed code tag]



I'll narrow your search a bit. The problem is in this method. Think logically through the order of these lines, run through an example in your head. You'll get it

one other note. Unless your fields ain't growin' much, I think you want the second one

fallow
follow
[ August 09, 2004: Message edited by: Joseph George ]
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah i ment follow .. not fallow, but i still don't see a problem in this function.
lastX and lastY are the cordinates that belong to element in front, so i think the order is like it has to be.

btw ... i should allso give how Vector v is done:


.. but maybe i am too blind to notice the error, so i would really like to know what should be different.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you want each consecutive point to take up the coords of the point ahead of it, right? So therefore, you need a copy of the coords that each point comes as, so that the next point can take its place. Is this being done?
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is how i itend to do it, but i think i have done that, what you think is missing.
So i highlight the copy of the coords, that the following point should have.


Seem to me you see something that i don't, perhaps you can "make it of wood and paint it red"?
[ August 10, 2004: Message edited by: Juhan Voolaid ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a concrete example, give your snake numbers, and run through that method.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK ... now you can tell me how to do it correctly.

In the follow() function all the elements get the same coords that the first head elemnt had. But with the same idea i make copy of coords to other elements.
Somehow lastX and lastY values remain the same even though i think i update them.

When i think it through with pen and piece of paper I can't come up with anything else.

So ... how would u do it?
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, but the record will note that I attempted to lead you in the right direction.



3: p points to the same thing pointed to by v.get(i).
4: object pointed to by both p and v.get(i) is updated to new coords
5-6: lastX and lastY set to the coords of the updated point, NOT the original coords.

Remember that when you have many references to the same object, and that object is changed, all the references still point to that (now changed) object.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

4: object pointed to by both p and v.get(i) is updated to new coords


So that's the problem. I thought that Point p will be copy of first one and will not be changed. Didn't know that its values will be updated allso.

So instead of making Point p, I made separate integers x and y to hold those values. If i would wanted to use the point object i guess i had to create new Point() object.

OK .. now it is working and i am very thankful.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember that p is NOT a Point. It is a pointer to a Point somewhere off in memory. When you say Point q=p, you say 'make this pointer point to the same object as q points to.' Therefore, when you modify that object, all the other pointers are not 'updated.' They do the same thing they've been doing. Point to that object. The object ITSELF is now different.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK now when i got that working I'm still not quite satisfied with the result.
Because the snake takes 10 px jumps, it doesen't move slowly. What i thought of doing was to make a for loop of moveHead() and follow() and repaint() it after 1 px of movement, but then all went wrong. Snake was again 2 points long and was still moving jumpingly.

What i did was:


I don't know why that kind of result. What is wrong now?
I allso fear that i must make new thread to make that 10px smooth step. I'm afraid of threads.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!