# Including
# Including
# Including
# Including
# Including
const int H = 8; //The height of the map
const int L = 16; //The length of the map
Char game map [h] [l]; //game map
Int key; //Press the button to save.
int sum = 1,over = 0; //The length of the snake, the game is over (eat yourself or hit the wall)
int dx[4] = {0,0,- 1, 1 }; //Left, right, up and down directions
int dy[4] = {- 1, 1,0,0 };
Structsnake//the data type of each node of snake.
{
int x,y; //Left position
Int now// Save the direction of the current node. 0, 1, 2 and 3 are left, right, up and down respectively.
} Snake[H * L];
Const char Shead =' @// snakehead
Const char Sbody =' #// snake body
Const char Sfood =' *// food
const char Snode = ' . '; //'.'is marked as empty on the map.
void Initial(); //Initialization of mapping
void Create _ Food(); //Randomly produce food on the map
void Show(); //Refresh the display map
void Button(); //Take out the key and judge the direction.
void Move(); //The movement of the snake
void Check _ Border(); //Check whether the snakehead crossed the line.
void Check_Head(int x,int y); //Check the position of the snakehead after moving.
int main()
{
initial();
show();
Returns 0;
}
Initialization of Void Initial() // mapping
{
int i,j;
int hx,hy;
System ("snake"); //The title of the console
Memset (game map,'.', sizeof); //Initialization maps are all empty "."
System ("CLS");
srand(time(0)); //Random seed
hx = rand()% H; //Produce snakehead
hy = rand()% L;
GameMap[hx][hy]= Shead;
Snake [0]. X = hx snake [0]. y = hy
Snake [0]. Now =-1;
create _ Food(); //Produce food at will
for(I = 0; I & lth;; I++) // Map display
{
for(j = 0; j & ltl; j++)
printf("%c ",GameMap[I][j]);
printf(" \ n ");
}
Printf ("\ nSmall C language snake \ n");
Printf ("Press any direction key to start the game \ n");
getch(); //First accept a key and let the snake start walking in this direction.
button(); //Take out the key and judge the direction.
}
Void Create_Food() // Randomly produce food on the map.
{
int fx,fy;
while( 1)
{
FX = rand()% H;
fy = rand()% L;
If (game map [FX] [FY] ='. ')// cannot appear in the position occupied by snakes.
{
GameMap[FX][fy]= s food;
Break;
}
}
}
Void Show() // Refresh the display map.
{
int i,j;
while( 1)
{
_ Sleep (500); //Delay for half a second (1000 is 1s), that is, refresh the map every half a second.
button(); //First, judge that the key is moving.
move();
If(over) // If you eat or touch the wall, the game is over.
{
Printf("\n** Game over * * \ n ");
printf(" & gt; _ & lt\ n ");
getchar();
Break;
}
System ("CLS"); //Clear the map, and then display the refreshed map.
for(I = 0; I & lth;; i++)
{
for(j = 0; j & ltl; j++)
printf("%c ",GameMap[I][j]);
printf(" \ n ");
}
Printf ("\ nSmall C language snake \ n");
Printf ("Press any direction key to start the game \ n");
}
}
Void Button() // Take out the key and judge the direction.
{
if(kbhit()! = 0) // Check whether there is keyboard input at present, and return a non-zero value if there is, otherwise return 0.
{
while(kbhit()! = 0) // There may be multiple keys, so you should take them all, giving priority to the last one.
key = getch(); //Take out the key from the console and save it in the key.
Switch (key)
{//Left side
Case 75: Snake [0]. Now = 0;
Break;
//Right
Case 77: Snake [0]. Now =1;
Break;
//Up
Case 72: Snake [0]. Now = 2;
Break;
//Down
Case 80: Snake [0]. Now = 3;
Break;
}
}
}
Void Move() // snake's movement
{
int i,x,y;
Int t = sum// Save the length of the current snake.
//Record the current position of the snakehead, set to null, and the snakehead moves first.
x = Snake[0]。 x; y = Snake[0]。 y; Game map [x] [y] ='.';
Snake [0]. x = Snake[0]。 x + dx[ Snake[0]。 Now];
Snake [0]. y = Snake[0]。 y + dy[ Snake[0]。 Now];
Check_Border()。 //Did the snakehead cross the line?
Check_Head(x,y); //The position of snakehead after moving. The parameter is: the starting position of the snake head.
If(sum == t) // The snake moves without food.
for(I = 1; I < sum; I++) // To move forward from an anticlimactic node, use the previous node as a reference.
{
If(i == 1) // Move after setting the tail node to null.
Game map [snake [me]. x ][ Snake[i]。 y ] = ' . ';
If(i == sum- 1) // is the snake node behind the snake head, which is specially treated.
{
Snake [me]. x = x
Snake [me]. y = y
Snake [me]. now = Snake[0]。 Now;
}
Else // Other snakes went to the snake position before.
{
Snake [me]. x = Snake[i+ 1]。 x;
Snake [me]. y = Snake[i+ 1]。 y;
Snake [me]. Now = snake [i+ 1]. Now;
}
Game map [snake [me]. x ][ Snake[i]。 y]= ' # '; //After moving, it should be set to' #' snake.
}
}
Void Check_Border() // Check whether the snakehead crossed the line.
{
If(Snake[0].x 0 || snake [0]. x & gt= H
|| Snake[0]。 Y 0 || Snake [0]. y & gt= L)
over = 1;
}
Void Check_Head(int x, int y) // Check the position of the snakehead after moving.
{
If (game map [snake [0].x] [snake [0]. Y] = ='. ')// is empty.
Game map [Snake[0]. x ][ Snake[0]。 y]= ' @ ';
other
If (game map [snake [0].x] [snake [0]. Y]=' * ')// It's food.
{
Game map [Snake[0]. x ][ Snake[0]。 y]= ' @ ';
Snake [sum]. X = x// The newly added snake is the one behind the head of the snake.
Snake [sum]. y = y
Snake [sum]. now = Snake[0]。 Now;
Game map [snake [and]. X ][ snake [and]. y]= ' # ';
sum++;
create _ Food(); //When the food is finished, another kind of food will be served soon.
}
other
over = 1;
}