# Snake C language code implementation (optional difficulty)

• 2020-06-01 10:44:20
• OfStack

In this paper, the example of C language for you to share the implementation of the snake specific code, for your reference, the specific content is as follows

``````
/*********************************************************
******************** Snake (optional) ********************
************** Producer: Xu Lizi   Date: 2012/12/31********
******************** Some functions can be used for reference ************************
**********************************************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>

int snakey[100]={5,4,3,2,1};  /* Define the abscissa of the snake */
int snakex[100]={1,1,1,1,1};  /* Define the ordinate of the snake. The starting position of the snake head is ( 5 . 1 ) */
int life=0; /* Defining the life of a snake, 0 Is survival, 1 Said death */
int lenght=5;  /* Define the length of the snake 5 section */

char map[12][24]={"***********************", /*y*/
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
"*      *",
/*x*/ "***********************"};

void put_money(int i,int j)  /* Put the money function, use a random number, randomly present the food */
{
int x=0,y=0;
srand(time(NULL));
while ( (map[y][x]==003) || (map[y][x]==002) || (map[y][x]=='*') || ((x==i)&&(y==j)) )
{
x=rand()%21+1;
y=rand()%10+1;
}
map[y][x]='\$';
return;
}

void output()  /* The output */
{
system("cls");
int i,j;
for(i=0; i<12; i++)
{
for(j=0; j<23; j++) printf("%c", map[i][j]);
printf("\n");
}
return;
}

void gameover()  /* Game over */
{
life=1;
printf(" You lose, stupid !!!\n");
return;
}

void turn_up()  /* Move up */
{
system("cls");
int i;
if ( (snakex[0]==1) || (map[snakex[0]-1][snakey[0]]==003) ) gameover(); else {
if (map[snakex[0]-1][snakey[0]]=='\$')
{
put_money( snakey[0], snakex[0]-1 );
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakex[0]--;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}

void turn_down()   /* down */
{
system("cls");
int i;
if ( (snakex[0]==10) || (map[snakex[0]+1][snakey[0]]==003) ) gameover();else {
if (map[snakex[0]+1][snakey[0]]=='\$')
{
put_money(snakey[0],snakex[0]+1);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
snakex[0]++;
map[snakex[lenght]][snakey[lenght]]=' ';
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}

void turn_left()  /* To the left */
{
system("cls");
int i;
if ( (snakey[0]==1) || (map[snakex[0]][snakey[0]-1]==003) ) gameover();else {
if (map[snakex[0]][snakey[0]-1]=='\$')
{
put_money(snakey[0]-1,snakex[0]);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]--;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}

void turn_right()  /* To the right */
{
system("cls");
int i;
if ( (snakey[0]==21) || (map[snakex[0]][snakey[0]+1]==003) ) gameover();else {
if (map[snakex[0]][snakey[0]+1]=='\$')
{
put_money(snakey[0]+1,snakex[0]);
lenght++;
map[snakex[lenght-1]][snakey[lenght-1]]=003;
}
for(i=lenght; i>0; i--)
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
map[snakex[lenght]][snakey[lenght]]=' ';
snakey[0]++;
for(i=lenght-1; i>0; i--) map[snakex[i]][snakey[i]]=003;
map[snakex[0]][snakey[0]]=002;
output();
}
return;
}

int main()
{
int i,timeover,hard;
long start;
char name , direcation;

printf("\n  Moving up: W  ; Moving down: S ;  Moving to the left: A  ;   Moving to the right: D \n");
printf("\t Please select difficulty (number) \n\t points 1~5 level , Represent the \n\t1 difficult ,2 Upper middle ,3 In the ,4 The middle and lower 5, Yi: \n");
scanf("%d",&hard);
system("cls");

for(i=1;i<5;i++) map[1][i]=003;  /* Output snake-body. */
map[1][5]=002;  /* Output the snake */

put_money(0,0);
output();

while(life!=1) /* The cycle ends when the snake dies */
{
/* The function that makes the snake run automatically ****** With reference to */
timeover=1;
start=clock();
while((timeover=(clock()-start<=hard*100))&&!kbhit());  // Difficulty setting
if(timeover)
{
direcation=getch();
}
/* The function that makes the snake run automatically ****** With reference to */

switch(direcation)
{
case 'w':turn_up();break;
case 's':turn_down();break;
case 'a':turn_left();break;
case 'd':turn_right();break;
}
}
return 0;
}
``````

Related articles: