Змея игра с использованием C++ не будет отображать хвост должным образом
I wrote this simple snake game using c++, all was working fine until I added the tail functionality using double linked lists, the idea is I have a pointer head pointing on a node called body, when the head is in the same position as a fruit a new body is allocated with a pointer tail pointing on it and depending on what buttons are pressed to generate movement the coordinates are added as you can see in the logic function. Now, I don't see where the problem is but when I added the code to display the tail in the display function for some reason the program stops working right before the line to display the tail. What seems to be the issue ?
КОД:
<pre>#include<iostream> #include<stdlib.h> #include<windows.h> #include<conio.h> using namespace std; bool gameover; const int width=20; const int height=20; int fruitX, fruitY; enum directions{STOP=0, LEFT, RIGHT, UP, DOWN}; enum directions dir; int score; struct body { int x; int y; body* next; body* prev; }*head=NULL; body* t; body* tail; body* q=head; void create() { gameover=false; dir=STOP; head=(body*)malloc(sizeof(body)); head->prev=NULL; head->next=NULL; head->x=(width/2)-1; head->y=(height/2)-1; fruitX=rand()%width; fruitY=rand()%height; score=0; } void display() { system("cls"); for(int i=0;i<width;i++) cout<<"x"; cout<<endl; for(int j=0;j<height;j++) { for(int i=0;i<width;i++) { if(i == 0 || i == width-1) cout<<"x"; else if(i == head->x && j == head->y) cout<<char(254); else if(i == fruitX && j == fruitY) cout<<"*"; else { bool printTail=false; while(q != head) { q=q->next; } if(i == q->x && j == q->y) { cout<<char(254); printTail=true; } if(!printTail) cout<<" "; } } cout<<endl; } for(int i=0;i<width;i++) cout<<"x"; cout<<endl; cout<<"Score:"<<score; } void logic() { switch(dir) { case UP:head->y--;break; case DOWN:head->y++;break; case LEFT:head->x--;break; case RIGHT:head->x++;break; } if(fruitX == head->x && fruitY == head->y) { fruitX=rand()%width; fruitY=rand()%height; score++; body* tail=(body*)malloc(sizeof(body)); t=head; t->next=tail; tail->prev=t; t=tail; if(dir==RIGHT) { t->x=t->prev->x++; t->y=t->prev->y; } else if(dir==LEFT) { t->x=t->prev->x--; t->y=t->prev->y; } else if(dir==UP) { t->x=t->prev->x; t->y=t->prev->y++; } else if(dir==DOWN) { t->x=t->prev->x; t->y=t->prev->y--; } } if(head->x > width-1) head->x=0; else if(head->x < 0) head->x=width-1; else if(head->y > height) head->y=0; else if(head->y < 0) head->y=height-1; } void input() { if(kbhit()) { switch(getch()) { case 'w':dir=UP;break; case 's':dir=DOWN;break; case 'd':dir=RIGHT;break; case 'a':dir=LEFT;break; case 'p':gameover=true;break; } } } int main() { create(); while(!gameover) { display(); input(); logic(); Sleep(100); } }
Что я уже пробовал:
Я попытался удалить код для отображения хвоста, он работал просто отлично, добавил его обратно, и происходит то же самое, так что, по-видимому, проблема заключается в отображении хвостовой линии, но я не могу понять, почему.
KarstenK
совет: сделайте структуру для своего глобальные переменные и использовать отладчик ;-)