Home > Back-end >  A freshman, just self-study list, to learn from the CSDN bosses snake, but my zha is a dead snake. B
A freshman, just self-study list, to learn from the CSDN bosses snake, but my zha is a dead snake. B

Time:02-06

#include
#include
#include
#include
#include
Char click;
Void gotoxy (short x, short y) {
COORD COORD={x, y};
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
}
Typedef struct Snake
{
Int x;
Int y;
Struct Snake * next;
} the snake;
The snake * head;//define the snake pointer;
Struct Food
{
Int x;
Int y;
} the food;
Void creatmap ();//create a map
Void creatfood ();//create food
Void movebody ();//move
Void eatfood ();//to eat the food
Int clickcontrol ();//keyboard information
Int Judge ();//whether the game end
Void freesnake ();//release list memory
Void gotoxyprint (int x, int y);//print square
Void gotoxydelet (int x, int y);//remove squares
Int main ()
{
System (" color 0 b ");
Creatmap ();
Creatfood ();
If (clickcontrol ()) return 0;

}
Void creatmap early ()//to create maps and set the snake
{
int i;
The snake * p, * q;
For (I=0; I & lt; 50; I +=2)
{
Gotoxyprint (0, I);
Gotoxyprint (24, I);
}
For (I=0; I & lt; 24. I +=1)
{
Gotoxyprint (I, 0);
Gotoxyprint (I, 48);

}
Head=(snake *) malloc (sizeof (snake));
P=(snake *) malloc (sizeof (snake));
Q=(snake *) malloc (sizeof (snake));
The head - & gt; X=5;
The head - & gt; Y=5;
P - & gt; X=6;
P - & gt; Y=5;
Q - & gt; X=7;
Q - & gt; Y=5;
The head - & gt; Next=p;
P - & gt; Next=q;
Q - & gt; Next=NULL;
Gotoxyprint (head - & gt; X, the head - & gt; y);
Gotoxyprint (p - & gt; X and p - & gt; y);
Gotoxyprint (q - & gt; X, q - & gt; y);
}
Void gotoxyprint (int x, int y)
{
Gotoxy (y, x);
Printf (" a square ");//content cannot contain the following special characters
}
Void gotoxydelet (int x, int y)
{
Gotoxy (y, x);
printf(" ");
}
Void creatfood ()
{

Int flag=1;
The snake * l=head;
Srand ((int) time (0));
While (flag)
{
Flag=0;
Food. X=rand () % (48-2 + 1) + 2;
Food. Y=rand () % (24-1 + 1) + 1;
While (1)//out food to the snake body
{
If (l==NULL)
break;
If (food. X==l - & gt; X & amp; & Food. Y==l - & gt; Y)
flag=1;
L=l - & gt; Next;

}
}
Gotoxy (food. X, food. Y);
Printf (" you ");
}
Int clickcontrol ()
{
While (1)
{
If (Judge ()==0) return 0;
If (_kbhit ())
Click=_getch ();
Sleep (150);
Movebody ();
Eatfood ();
}
return 1;

}
Void movebody ()
{
The snake * p=head;
The switch (click)
{
Case 'w' : the head - & gt; X=1;
break;
Case 's' : the head - & gt; X +=1;
break;
Case 'a' : the head - & gt; Y=2;
break;
Case 'd' : the head - & gt; Y +=2;
}
While (p - & gt; Next)
{
P=p - & gt; Next;
}
Gotoxydelet (p - & gt; X and p - & gt; y);
Free (p);
P=NULL;
Gotoxyprint (head - & gt; X, the head - & gt; y);
P=(snake *) malloc (sizeof (snake));
P - & gt; Next=head;
The head=p;
}
Void eatfood ()
{
Snake * tail=(snake *) malloc (sizeof (snake), * p=head;
If (food. X==head - & gt; X& & Food. Y==head - & gt; Y)
{
While (p - & gt; Next)
{
P=p - & gt; Next;
}
P - & gt; Next=tail;
Tail - & gt; Next=NULL;
}
}
Int Judge ()//whether the game end
{
The snake * p=head;
If (the head - & gt; X==0 | | head - & gt; X==50 | | head - & gt; Y==0 | | head - & gt; Y==25)
{
Freesnake ();
return 0;
}
return 0;
While (p)
{
P=p - & gt; Next;
If (the head - & gt; X==p - & gt; X& & The head - & gt; Y==p - & gt; Y)
{
Freesnake ();
return 0;
}

}
return 1;
}
Void freesnake ()//release list memory
{
The snake * p;
While (the head)
{
P=the head;
The head=head - & gt; Next;
Free (p);
}
}






CodePudding user response:

Mobile body parts logic has a problem, it is suggested that try to miss zhao step through law

CodePudding user response:

Don't rely on the debugger output complex data structure. And complex data structures to the entire content of every step of the process it using a small piece of code is easy to understand by yourself the format of the output, help to debug very much! Or can be said to be "infrastructure"
Refer to the following:
Singly linked list data structure on data sort http://bbs.csdn.net/topics/392201633

CodePudding user response:

The most obvious is that Judge ()
If (the head - & gt; X==0 | | head - & gt; X==50 | | head - & gt; Y==0 | | head - & gt; Y==25)
{
Freesnake ();
return 0;
}
//return 0;
//while (p)
Rueturn 0 is why deleted, while (p), infinite loop with movebody ()
While (p - & gt; Next)
{
P=p - & gt; Next;
}
Have risk death cycle, debug it slowly wish you well
Prev:Python
Next:fission
  • Related