I can't seem to make the nodes follow the previous node, the nodes only follow the head which is the start node, how can I make the node follow the previous node and make a snake-like movement, please help. For now, in the output screen, the snake doesn't bend when direction keys are pressed, all of the nodes align in the direction all at once.
`#include <stdio.h>
#include <windows.h>
#include <conio.h>
#include <stdlib.h>
int point_x = 0, point_y = 0;
struct snake
{
int x, y;
struct snake *next;
struct snake *prev;
} *ptr, *temp, *start;
void create()
{
ptr = (struct snake *)malloc(sizeof(struct snake));
ptr->next = NULL;
if (start == NULL)
{
start = ptr;
}
else
{
for (temp = start; temp->next != NULL; temp = temp->next)
;
temp->next = ptr;
ptr->prev = temp;
}
}
void gotoxy(int x, int y)
{
COORD CRD;
CRD.X = x;
CRD.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), CRD);
}
void generatePoint()
{
point_x = rand() % 120;
point_y = rand() % 30;
}
int main()
{
/*-------------------------HIDING CURSOR------------------------*/
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO info;
info.dwSize = 100;
info.bVisible = FALSE;
SetConsoleCursorInfo(consoleHandle, &info);
/*-------------------------HIDING CURSOR------------------------*/
start = NULL;
int points = 0;
char ch1;
create();
start->x = 60;
start->y = 15;
gotoxy(start->x, start->y);
printf("%c", (char)254);
generatePoint();
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
while ((ch1 = _getch()) != 27)
{
if (ch1 == 'w')
{
while (_kbhit())
_getch();
while (start->y > 0)
{
system("cls");
start->y = start->y - 1;
gotoxy(start->x, start->y);
printf("%c", (char)254);
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
if (start->x == point_x && start->y == point_y)
{
points++;
generatePoint();
create();
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
else
{
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y) + 1;
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
gotoxy(110, 0);
printf("\033[34mPoints: %d\033[0m", points);
Sleep(90);
if (_kbhit())
{
break;
}
if (start->y == 0)
{
system("cls");
gotoxy(55, 15);
printf("\033[31mGame over!\033[0m");
system("pause>nul");
exit(0);
}
}
}
else if (ch1 == 'a')
{
while (_kbhit())
_getch();
while (start->x > 0)
{
system("cls");
start->x = start->x - 1;
gotoxy(start->x, start->y);
printf("%c", (char)254);
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
if (start->x == point_x && start->y == point_y)
{
points++;
generatePoint();
create();
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
else
{
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x) + 1;
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
gotoxy(110, 0);
printf("\033[34mPoints: %d\033[0m", points);
Sleep(90);
if (_kbhit())
{
break;
}
if (start->y == 0)
{
system("cls");
gotoxy(55, 15);
printf("\033[31mGame over!\033[0m");
system("pause>nul");
exit(0);
}
}
}
else if (ch1 == 's')
{
while (_kbhit())
_getch();
while (start->y < 30)
{
system("cls");
start->y = start->y + 1;
gotoxy(start->x, start->y);
printf("%c", (char)254);
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
if (start->x == point_x && start->y == point_y)
{
points++;
generatePoint();
create();
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
else
{
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y) - 1;
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
gotoxy(110, 0);
printf("\033[34mPoints: %d\033[0m", points);
Sleep(90);
if (_kbhit())
{
break;
}
if (start->y == 0)
{
system("cls");
gotoxy(55, 15);
printf("\033[31mGame over!\033[0m");
system("pause>nul");
exit(0);
}
}
}
else if (ch1 == 'd')
{
while (_kbhit())
_getch();
while (start->x < 119)
{
system("cls");
start->x = start->x + 1;
gotoxy(start->x, start->y);
printf("%c", (char)254);
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
if (start->x == point_x && start->y == point_y)
{
points++;
generatePoint();
create();
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
else
{
gotoxy(point_x, point_y);
printf("\033[32m%c\033[0m", (char)254);
}
if (points > 0)
{
for (temp = (start->next); temp != NULL; temp = temp->next)
{
temp->x = ((temp->prev)->x) - 1;
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}
gotoxy(110, 0);
printf("\033[34mPoints: %d\033[0m", points);
Sleep(90);
if (_kbhit())
{
break;
}
if (start->y == 0)
{
system("cls");
gotoxy(55, 15);
printf("\033[31mGame over!\033[0m");
system("pause>nul");
exit(0);
}
}
}
else
{
continue;
}
}
}`
I tried to make a snake game in c with linked list, but i cant seem to make it work