Snake source code

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <conio.h> int x[50]={1,1,1,1} ;//Initial snake coordinates x  int y[50]={1,1,1,1};//Initial snake coordinates y  int n=3; int t=0; int score=0; unsigned int x1=0; //Initial food coordinates x  unsigned int y1=0;//Initial food coordinates y  void action_y(); void action_x(); void gotoxy(); void paint(); void no_text_cursor(); void snake_birth(); void snake_action (); void array_w(); void array_s(); void array_a(); void array_d(); void game_report(); void (*pfun)(void)=array_d;//Move function pointer to control movement 




















int main(void)
{
no_text_cursor();//Hide the cursor 
snake_birth();//Birth 
for(int i=0;i<1;)//Continuously draw the interface 
{
paint();
game_report();
pfun() ;
snake_action();
gotoxy(0,0);//Move the cursor to (0,0). 
}
return 0;

void paint()//Drawing interface function 
{


if(t%100==0)//Let food exist regularly 
{
srand(time(NULL));
x1=1+rand()%18;/ /The coordinates of the randomly generated food x 
y1=1+rand()%18;//The coordinates of the randomly generated food y 
}
    system("color 12");//The color is changed to green on a blue background 
for(unsigned int i=0 ;i<20;i++)//Draw the game interface, i is vertical, k is horizontal 
{
for(unsigned int k=0;k<20;k++)
{
if(i==0||i==19)
{
printf("█");
}
else
{
if(k==0||k==19)
{
printf("█");
}else
{
if(i==y1&&k==x1)
{
printf("○"); } else {   char o=0;   char time=0;       for(int j=0;j<=n;j++)   {   if(x[j]==k&&y[j]==i)   {   if(j==0)   { if(time==0)   { printf("★"); time++; o=1;   }   }    else   { if(time==0) { printf("●"); time++; o=1;     }











 

 
 
 




 
 
 
 
 

  }
          }
}
if(o==0){
printf(" ");
}
}
}
}
}
printf("\n");
}
printf("Three generations of Snake control keys: WASD\n");
printf(" Snake head coordinates%2d,%2d Food coordinates%2d,%2d Section number%2d\n",x[0],y[0],x1,y1,n+1); 
t++;
}
void snake_birth()// Initialization function 
{
x[0]=4;x[1]=3;x[2]=2;x[3]=1;
y[0]=1;y[1]=1;y[2]= 1;y[3]=1;
}
void snake_action()//Keyboard detection function 
{
char ch=0;
if(kbhit())
{
ch=getch();
switch(ch)
{
case 'd':
if( x[0]!=18)
{
pfun=array_d; 
        }
break;
case 'a':
if(x[0]!=1)
    {
pfun=array_a;
        }
    break;
case 'w':
if(y[0]!=1)
{
pfun=array_w;
    }
break;
case 's':
if(y[0]!=18)
{
pfun=array_s;
    }
break; } } } void array_w()//上移函数  { if(y[0]!=1) { action_y(); y[0]--; action_x(); } } void array_s()//下移函数  { if(y[0]!=18) { action_y(); y[0]++; action_x();



















}
}
void array_a()
{
if(x[0]!=1)
{
action_x();
x[0]--;
action_y();

}
void array_d()
{
if(x[0]!=18)
{ action_x(); x[0]++; action_y(); } } void action_y() { for(int i=n;i>=1;i--) { y[i]=y[i-1 ]; } } void action_x() { for(int i=n;i>=1;i--){ x[i]=x[i-1]; } } void game_report()//game report function The game result is processed for calculating the score.  {





















if((x[0]==x1&&y[0]==y1)||(x[1]==x1&&y[1]==y1)||(x[2]==x1&&y[2]==y1 )||(x[3]==x1&&y[3]==y1))
{
if(n<49)
{
x[++n]=x[n-1];y[n]=y[n- 1]; 
}
score+=10;
x1=0;y1=0; } printf("YOUR SCORE:%d ",score); } void no_text_cursor(void) //Hide cursor function  {   CONSOLE_CURSOR_INFO cursor_info = {1, 0} ;   SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); } void gotoxy(int x, int y)//Move cursor function  {     COORD coord = {x, y};        /*COORD is a structure defined in Windows API, which means The coordinates of a character on the console screen. It is defined as:     typedef struct _COORD {     SHORT X; // horizontal coordinate





















    SHORT Y; // vertical coordinate
    } COORD;*/
    
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325574208&siteId=291194637