Sample code for c language snakes game

Please ask for the c code of Snake, I think it’s quite funny

. 4555555555555

c language glutton code

Basic idea:

Snake every eat a food snake body to increase a frame, with UP,DOWN,LEFT,RIGHT control the movement of the snake head, and the snake body to follow the head of the snake to go after each frame after the snake body next to go to the position of the last frame of the snake body, and so on.

#include<stdio.h&gt.

Gluttony c code

#include<stdio.h>

#include<stdlib.h>

#include<Windows.h>

#include<conio.h& gt;

#include<time.h>

chargamemap[20][40];//game map size 20*40

intscore=0;//current score

///record of snake’s nodes

intx[800];// Row number of each node

inty[800];//column number of each node

intlen=0;//length of the snake

///record information about the fruit

intfx=0;//horizontal coordinate of the food

intfy=0;//vertical coordinate of the food

intfcount=0;//number of food

/Main function operation

voidcreatefood();//generate food

voidPrintgameMap(intx[],inty[]);//draw game map

voidmove(intx[],inty[]);//move the snake

intmain()

{

srand(time(NULL));

///Initialize the position of the snake’s head and body,the default length of the snake is 2 at the beginning

x[len]=9;

y[len]=9;

len++;

x[len]=9;

y[len]=8;

len++;

createfood();

PrintgameMap(x,y);

move(x,y);

return0;

}

voidcreatefood()

{

if(0==fcount)

{

inttfx=rand()%18+1;

inttfy=rand()%38+1;

inti,j;

inthave=0;//a 0 means the food is not part of the food

for(i=0;i<len;i++)

{

for(j=0;j<len;j++)

{

if(x[i]==fx&&y[j]==fy)

{

have=1;

break;

}

else

{

have=0;

}<

}

if(1==have)//if part of snake, execute next loop

{

continue;

}

else///otherwise generate new fruit

{

fcount++;

fx= tfx;

fy=tfy;

break;

}

}

}

}

//game map

voidPrintgameMap(intx[],inty[])

{

intsnake=0,food=0;

inti,j;

//Draw the game map, and draw the initial position of snake

for(i=0;i<20;i++)

{

for(j=0;j<40;j++)

{

if(i==0&&j>=1&&j<=38)

{

gamemap[i][j]=’=’;

}

elseif(i==19&&j>=1&& amp;j<=38)

{

gamemap[i][j]=’=’;

}

elseif(j==0||j==39)

{

gamemap[i][j]=’#’;

}

else

{

gamemap[i][j]=”;

}

// Determine if the snake is at the current position

intk;

for(k=0;k<len;k++)

{

if(i= =x[k]&&j==y[k])

{

snake=1;

break;

}

else

{

snake=0;

}

}

{

{

< p>if(fcount&&fx==i&&fy==j)

{

food=1;

}

else

{

food=0;

}

}

// If snake is at current location

if(1==snake)

{

printf(“*”);

}

elseif(1==food)

{

printf(“f”);

}

// If snake is not at the current position and there is no fruit at the current position

else

{

printf(“%c”,gamemap[i][j]);

}

}

printf(“\n”);

}

}

printf(“score:%d”,score);

}

voidmove(intx[],inty[])

{

chars;

s=getch();

intmove=0,beat=0;

while(1)

{

intcx[800];

intcy[800];

memcpy(cx,x,sizeof(int)*len);

memcpy(cy,y,sizeof(int)*len);

if(s==’w’)

{

x[0]–;

move=1;

if(x[0]<=0)

{

printf(“Gameover\n”);

break;

}

}

}

elseif(s==’s’)

{

x[0]++;

move=1;

if(x[0]>=19)

{

printf(“Gameover\n”);

break;

}

}

elseif(s==’a’)

{

y[0] –;

move=1;

if(y[0]<=0)

{

printf(“Gameover\n”);

break;

}

}

elseif(s==’d’ )

{

y[0]++;

move=1;

if(y[0]>=39)

{

printf(“Gameover\n”);

break;

}

}

}

//body

inti;

for(i=1;i<len;i++)

{

x[i]=cx[i-1];

y[i]=cy[i-1];

}

for(i=1;i<len;i++)//if it bites itself

{

if(x[0]==x[i]&&y[0]==y[i])

{

beat=1;

}

else

{

beat=0;

}

}

if(1==beat)

{

printf(“Gameover\n”);

break;

}

if(1==move)

{<

if(fcount&&x[0]==fx&&y[0]==fy)//if the fruit is eaten

{

//copy the current snakehead address to the second node

memcpy(x+1,cx,sizeof(int)*len);;.

memcpy(y+1,cy,sizeof(int)*len);

len++;

fcount–;

fx=0;

fy=0;

score++;

createfood();

}

Sleep(70);

system(“cls”);

PrintgameMap(x,y);

}

else

continue;

if(kbhit())// Determine if the key was pressed

{

s=getch();

}

}

}

}