Archived

This topic is now archived and is closed to further replies.

Maestin

Nibbles

Recommended Posts

Hello all. I''m trying to write a small nibbles/snake-game in C using SDL and OpenGL. When the snake moves, and turns, the rest of the snake (which is made out of textured cubes) is supposed to turn with the "head". I''ve tried some things I thought could accomplish this, but so far nothing has worked. This is what I tried: I used a two-way linked list to represent the snake, and each structure represents a cube, and contains x-position, y-position, direction, and pointers to the next and previous structures. I used a function, MoveSnake(), to go through the list and move each part to the location of the previous part. That didn''t work very well. So, any suggestions? Sorry of this is a stupid question, but I am new to game-programming

Share this post


Link to post
Share on other sites
that sounds like a good way to go about it to me
but remember to start at the end of the list and work your way to the beginning of the list when making each piece position equal to its previous pieces position....
if you start at the head then here is what happens

head->position = wherever player moves
head->next->position = head->position;
head->next->next->position = head->next->position;

kinda like that...but if you will notice
head->next->position = head->position;
so then when you have that third line of code
it also make that position equal to the head position....so all the pieces will end up being where the head is

so start at the tail of the list and work your way to the beginning


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

Share this post


Link to post
Share on other sites
Sorry to bother you again, but it still doesnt work.

It works better than before. I start at the end of the list, but now the other parts move at twice the head''s speed.

This is my code to move the snake:

void MoveSnake(struct SnakePart *SnakeHead)
{
struct SnakePart *CurPart = SnakeHead;
while(CurPart->NextPart != NULL) // Move to the end
CurPart = CurPart->NextPart;

// Step through the list starting with the last part
// Move each part to the position of the PrevPart

while(CurPart->PrevPart != NULL)
{
CurPart->posx = CurPart->PrevPart->posx;
CurPart->posy = CurPart->PrevPart->posy;
CurPart = CurPart->PrevPart;
}

// Move the head in current direction

switch(SnakeHead->dir)
{
case SDIR_RIGHT:
CurPart->posx += SNAKE_SPEED;
break;
case SDIR_LEFT:
CurPart->posx -= SNAKE_SPEED;
break;
case SDIR_UP:
CurPart->posy += SNAKE_SPEED;
break;
case SDIR_DOWN:
CurPart->posy -= SNAKE_SPEED;
break;
default:
break;
}
}

I dunno what I do wrong, but it must be something

// Maestin

Share this post


Link to post
Share on other sites