#include
#include
#include
#include
#include
using namespace std;
# define HIGH 20
# define WIDE 41
Int wall [HIGH] [WIDE];
Int food_x food_y;
Enum {UP, DOWN, RIGHT, LEFT} point;
Typedef struct snake
{
Int x, y;
The prior struct snake *;
Struct snake * next;
} the snake;
The snake head, * tail;
Void add_head (int a, int b)
{
The snake * temp=new snake;
Temp - & gt; X=a;
Temp - & gt; Y=b;
Temp - & gt; The prior=NULL;
If (head==NULL)
{
The head=tail=temp;
The head - & gt; Next=NULL;
}
The else
{
The head - & gt; The prior=temp;
Temp - & gt; Next=head;
The head=head - & gt; The prior;
}
The wall [a]=1;
}
Void delete_tail ()
{
The snake * temp=tail;
Tail=tail - & gt; The prior;
Tail - & gt; Next=NULL;
The delete temp.
}
Void create_food ()
{
Srand (unsigned (time (0)));
Do
{
18 + food_x=rand () % 1;
39 food_y=rand () % + 1;
} while (wall [food_x] [food_y]==1);
Wall [food_x] [food_y]=1;
}
Void the output ()
{
int i,j;
for(i=0; i
for(j=0; J
If (wall [j])
Printf (" * ");
The else
Printf (" ");
}
printf("\n");
}
}
Void change_point (char keydown)
{
The switch (keydown)
{
Case 'W' :
Case 'w' : the point=UP; break;
Case 'A' :
Case 'a' : the point=LEFT; break;
In case the 'S' :
Case 's' : the point=DOWN; break;
Case 'D' :
Case 'd' : the point=RIGHT; break;
}
}
Void move ()
{
Int a=head - & gt; x;
Int b=head - & gt; y;
The switch (point)
{
Case UP: - a; break;
The case DOWN: + + a; break;
Case RIGHT: + + b; break;
Case LEFT: - b; break;
}
If (wall [a]==1 & amp; & Food_x!=a & amp; & Food_y!=b)
{
The output ();
Printf (" game over ");
}
If (food_x==a & amp; & Food_y==b)
{
Add_head (a, b);
Create_food ();
return;
}
Add_head (a, b);
Delete_tail ();
}
Void init ()
{
int i;
for(i=0; i
Wall [0]=1;
Wall [HIGH - 1]=1;
}
for(i=0; i
Wall [0]=1;
Wall [WIDE - 1]=1;
}
The head=tail;
Point=RIGHT;
Add_head (6, 4);
Add_head (6, 5);
Add_head (6, 6);
}
Int main ()
{
init();
Create_food ();
The output ();
Printf (" please write note: W, A, S left, D -> \ n press any key to start ");
While (true)
{
Char keydown=getch ();
Change_point (keydown);
while(! Kbhit ())
{
system("cls");
move();
The output ();
Sleep (500);
}
}
return 0;
}
I don't eat the snake food also grew up automatically, and cannot be GAME OVER, and snake generated is a fixed position,
Problem a lot of, ask ace to solve, but is based on the change, don't all break, vc + + 6.0 environment can run simple greedy
The snake, don't too complicated
http://wenku.baidu.com/view/33b6f9d2195f312b3169a5ab.html
I as for this tutorial, but the tutorial itself has a problem