我好像不能让节点跟随前一个节点,节点只跟随起始节点的头,我怎样才能让节点跟随前一个节点并进行蛇形运动,请帮助。目前,在输出屏幕中,当按下方向键时,蛇不会弯曲,所有节点都会同时对齐方向。
`#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;
}
}
}`
我试图用链表在 C 语言中制作蛇游戏,但我似乎无法让它工作
至少这个问题:
ptr->prev = NULL;
失踪
在头节点的情况下。
if (start == NULL)
{
ptr->prev = NULL; // add
start = ptr;
}
我更改了一些代码,我需要用它们之前节点的坐标更新节点
if (points > 0)
{
for (temp = last; temp != start; temp = temp->prev)
{
temp->x = ((temp->prev)->x);
temp->y = ((temp->prev)->y);
gotoxy(temp->x, temp->y);
printf("%c", (char)254);
}
}