The C language implements snake code

  • 2020-11-26 18:53:14
  • OfStack

This article examples for you to share C language implementation snake code specific code, for your reference, the specific content is as follows


#include"stdafx.h"
#include<stdio.h>
#include<time.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>

#define U 1
#define D 2
#define L 3
#define R 4      // The state of the snake  U On:  D: Under the  L: On the left  R: right  
typedef struct snake    // The snake 1 A node  
{
 int x;      // The node's x coordinates  
 int y;      // The node's y coordinates 
 struct snake *next;   // The snake 1 A node  
 }snake;
 int score=0,add=10;    // Total score and every eat 1 Secondary food score  
 int highscore=0;     // The highest  
 int status,sleeptime=200;  // Snake advance state, the time interval between each run  
 snake *head,*food;    // Snake head pointer, food pointer  
 snake *q;      // The pointer used to traverse a snake  
 int endgamestatus=0;    // The state at the end of the game  
 HANDLE hOut;      // Console handle  
 void gotoxy(int x,int y);
 int color(int c);
 void printsnake();
 void wlcome();
 void createmap();
 void scoreandtips();
 void initsnake();
 void createfood();
 int biteself();
 void cantcrosswall();
 void speedup();
 void speeddown();
 void snakemove();
 void keyboardcontrol();
 void lostdraw();
 void endgame();
 void choose();
 void file_out();
 void file_in();
 void explation();
 main()
 { 
 system("mode con cols=100 lines=30");
 printsnake();
 wlcome();
 file_out();
 keyboardcontrol();
 endgame();
 }
 void gotoxy(int x,int y)// Set cursor position  
 {COORD c;
 c.X=x;
 c.Y=y;
 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c); // Locate the cursor position  
 }
 int color(int c)// Set the color  
 {
 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
 return 0;
 } 
 void printsnake()// Print character snake  
 {
 gotoxy(2,5);
 color(3);
 printf(" Name: Zhang Xiaoai ");
 
 gotoxy(2,6);
 color(3);
 printf(" Student number: 1910101099"); 
 
 gotoxy(35,1);
 color(6);
 printf("/^\\/^\\");
 
 gotoxy(34,2);
 printf("|_| o|");
 
 gotoxy(33,2);
 color(2);
 printf("_");
 
 gotoxy(25,3);
 color(12);
 printf("\\/");
 
 gotoxy(31,3);
 color(2);
 printf("/");
 
 gotoxy(37,3);
 color(6);
 printf("\\_/");
 
 gotoxy(41,3);
 color(10);
 printf(" \\");
 
 gotoxy(26,4);
 color(12);
 printf("\\____");
 
 gotoxy(32,4);
 printf("_________");
 
 gotoxy(31,4);
 color(2);
 printf("|");
 
 gotoxy(43,4);
 color(10);
 printf("\\");
 
 gotoxy(32,5);
 color(2);
 printf("\\_______");
 
 gotoxy(44,5);
 color(10);
 printf("\\");
 
 gotoxy(39,6);
 printf("| |    \\");
 
 gotoxy(38,7);
 printf("/ /    \\");
 
 gotoxy(37,8);
 printf("/ /    \\ \\");
 
 gotoxy(35,9);
 printf("/ /    \\ \\");
 
 gotoxy(34,10);
 printf(" / /     \\ \\");
 
 gotoxy(33,11);
 printf("/ /  _----_  \\ \\");
 
 gotoxy(32,12);
 printf("/ /   _-~ ~-_  | |");
 
 gotoxy(31,13);
 printf("(  (  _-~ _--_ ~-_  _/ |");
 
 gotoxy(32,14);
 printf("\\ ~-____-~ _-~ ~-_ ~-_-~ /");
 
 gotoxy(33,15);
 printf("~-_   _-~  ~-_  _-~");
 
 gotoxy(35,16);
 printf("~--____-~    ~-___-~");
 } 
 
 void wlcome()// The welcome screen  
 {int n;
 int i,j=1;
 gotoxy(43,18);
 color(11);
 printf(" The greedy snake fights ");
 color(14);
 for(i=20;i<=26;i++)
 {
  for(j=27;j<=74;j++)
  {
  gotoxy(j,i);
  if(i==20||i==26)
  {
  printf("-");
 }
 else if(j==27||j==74)
 {
 printf("|");
 }
 }
 }
 color(12);
 gotoxy(35,22);
 printf("1. Start the game ");
 gotoxy(55,22);
 printf("2. Game shows ");
 gotoxy(35,24);
 printf("3. Quit the game ");
 gotoxy(29,27);
 color(3);
 printf(" Please select a 1 2 3\n");
 color(14);
 scanf("%d",&n);
 switch(n)
 {
 case 1: 
 system("cls");// Clear the screen  
 createmap();
 initsnake();
 createfood();
 keyboardcontrol();
 break;
 case 2:
 explation();
 break;
 break;
 case 3:
  exit(0);
 break;
 } 
 } 
void createmap()// Create a map  
{
 int i,j;
 for(i=0;i<58;i+=2)
 {
 gotoxy(i,0);
 color(5);
  printf(" - ");
  gotoxy(i,26);
 printf(" - "); 
 }
 for(i=0;i<26;i++)
 {
 gotoxy(0,i);
  printf(" - ");
  gotoxy(56,i);
 printf(" - "); 
 }
 for(i=2;i<56;i+=2)
 {
 for(j=1;j<26;j++)
 {
 gotoxy(i,j);
  color(3);
  printf(" s \n\n"); 
 }
 
 }
}
 void scoreandtips()// Score and tips on the right side of the game screen 
 {
 file_out();
 gotoxy(64,4);
 color(11);
 printf("* The record *: %d",highscore);
 gotoxy(64,8);
 color(14);
 printf(" score : %d ",score);
 color(13);
 gotoxy(73,11);
 printf(" tip ");
 gotoxy(60,13);
 color(6);
 printf("+---------------------+");
 gotoxy(60,25);
 printf("+---------------------+");
 color(3);
 gotoxy(64,14);
 printf(" Each food score: %d points ",add);
 gotoxy(64,16);
 printf(" You can't go through walls. You can't bite yourself ");
 gotoxy(64,18);
 printf(" Use ↑↓←→ Control the snake's movement respectively ");
 gotoxy(64,20);
 printf("F1 To speed up, F2 To slow down ");
 gotoxy(64,22);
 printf("space:  Pause the game ");
 gotoxy(64,24);
 printf("ESC: Quit the game ");
}
void file_out()// Open file record for highest score 
{
 FILE *fp;
 fp=fopen("save.txt","a+");
 fscanf(fp,"%d",&highscore);
 fclose(fp);
 } 
 void initsnake()
 {
 snake *tail;
 int i;
 tail=(snake*)malloc(sizeof(snake));
 tail->x=24;
 tail->y=5;
 tail->next=NULL;
 for(i=1;i<=4;i++)
 {
 head=(snake*)malloc(sizeof(snake));
 head->next=tail;
 head->x=24+2*i;
 head->y=5;
 tail=head;
 }
 while(tail!=NULL)
 {gotoxy(tail->x,tail->y);
 color(14);
 printf(" u ");     // The snake body is made up of  u  
 tail=tail->next; 
 }
}
void createfood()// Random food  
{
 snake *food_1;
 srand((unsigned)time(NULL));
 food_1=(snake*)malloc(sizeof(snake));
 while((food_1->x%2!=0))
 {
  food_1->x=rand()%52+2;
 }
 food_1->y=rand()%24+1;
 q=head;
 while(q->next==NULL) 
 {
  if(q->x==food_1->x&&q->y==food_1->y)
  {
  free(food_1);
  createfood();
 }
 q=q->next;
 }
 gotoxy(food_1->x,food_1->y);
 food=food_1;
 color(12);
 printf("@");
}
int biteself()
{
 snake *self;    // define self Is a node on a snake other than its head  
 self=head->next;
 while(self!=NULL)
 {
 if(self->x==head->x&&self->y==head->y)
 {
 return 1;
 }
 self=self->next;
 }
 return 0;
 } 
 
 void cantcrosswall()
{
 if(head->x==0||head->x==56||head->y==0||head->y==26)
 {
 endgamestatus=1;
 endgame();
 }
 } 
 void speedup()// To speed up  
{
 if(sleeptime>=50)
 {
 sleeptime=sleeptime-10;
 add=add+2;
 } 
}

void speeddown()// Slow down  
{
 if(sleeptime<350)
 {
 sleeptime=sleeptime+30;
 add=add-2;
 if(sleeptime==350)
 {
 add=1;
 }
 } 
}

 void snakemove()// Control the direction  
{
 snake *nexthead;
 cantcrosswall();
 nexthead=(snake*)malloc(sizeof(snake));
 if(status==U)// on 
 {
 nexthead->x=head->x;   // As we go up, x The same, y-1 
 nexthead->y=head->y-1;
 nexthead->next=head;
 head=nexthead;
 q=head;
 // If the 1 There's food in one place. Down 1 The coordinates are the same as the food coordinates  
 if(nexthead->x==food->x&&nexthead->y==food->y)
 {
 while(q!=NULL)
 {
 gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
 }
 score=score+add;
 speedup();
 createfood();
 }
 else
 {
 while(q->next->next!=NULL)// If there is no food  
  {
   gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
  }
 // Go through the cycle above   . q Point to the tail, under the tail 1 The step is where the snake goes  
  gotoxy(q->next->x,q->next->y);
  color(3);
  printf(" s ");// Restore the position you have traveled  
  free(q->next);
  q->next=NULL; 
 } 
 
 } 
 if(status==D)
 {
  nexthead->x=head->x;   // As I go down, x The same, y+1 
 nexthead->y=head->y+1;
 nexthead->next=head;
 head=nexthead;
 q=head;
 // If the 1 There's food in one place. Down 1 The coordinates are the same as the food coordinates  
 if(nexthead->x==food->x&&nexthead->y==food->y)
 {
 while(q!=NULL)
 {
 gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
 }
 score=score+add;
 speedup();
 createfood();
 }
 else
 {
 while(q->next->next!=NULL)// If there is no food  
  {
   gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
  }
 // Go through the cycle above   . q Point to the tail, under the tail 1 The step is where the snake goes  
  gotoxy(q->next->x,q->next->y);
  color(3);
  printf(" s ");// Restore the position you have traveled  
  free(q->next);
  q->next=NULL; 
 }
 }
 if(status==L)// On the left  
 {
  nexthead->x=head->x-2;   // As I go to the left, x The same, y+1 
 nexthead->y=head->y;
 nexthead->next=head;
 head=nexthead;
 q=head;
 // If the 1 There's food in one place. Down 1 The coordinates are the same as the food coordinates  
 if(nexthead->x==food->x&&nexthead->y==food->y)
 {
 while(q!=NULL)
 {
 gotoxy(q->x,q->y);  // The food became the snake's 1 Part of the  
 color(14);
 printf(" u ");
 q=q->next; 
 }
 score=score+add;
 speedup();
 createfood();
 }
 else
 {
 while(q->next->next!=NULL)// If there is no food  
  {
   gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
  }
 // Go through the cycle above   . q Point to the tail, under the tail 1 The step is where the snake goes  
  gotoxy(q->next->x,q->next->y);
  color(3);
  printf(" s ");// Restore the position you have traveled  
  free(q->next);
  q->next=NULL; 
 } 
 }
 if(status==R)
 {
  nexthead->x=head->x+2;   // As we go up, x The same, y-1 
 nexthead->y=head->y;
 nexthead->next=head;
 head=nexthead;
 q=head;
 // If the 1 There's food in one place. Down 1 The coordinates are the same as the food coordinates  
 if(nexthead->x==food->x&&nexthead->y==food->y)
 {
 while(q!=NULL)
 {
 gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
 }
 score=score+add;
 speedup();
 createfood();
 }
 else
 {
 while(q->next->next!=NULL)// If there is no food  
  {
   gotoxy(q->x,q->y);
 color(14);
 printf(" u ");
 q=q->next; 
  }
 // Go through the cycle above   . q Point to the tail, under the tail 1 The step is where the snake goes  
  gotoxy(q->next->x,q->next->y);
  color(3);
  printf(" s ");// Restore the position you have traveled  
  free(q->next);
  q->next=NULL; 
 } 
}
 if(biteself()==1)
 {
 endgamestatus=2;
 endgame();
 }
}
void keyboardcontrol()
{
 status=R;
 while(1)
 {
 scoreandtips();
 //GetAsyncKeyState The function determines the state of the virtual key specified by the function call 
 if(GetAsyncKeyState(VK_UP)&&status!=D)
 {
 status=U;
 } 
 else if(GetAsyncKeyState(VK_DOWN)&&status!=U)
 {
 status=D;
 } 
 else if(GetAsyncKeyState(VK_LEFT)&&status!=R)
 {
 status=L;
 } 
 else if(GetAsyncKeyState(VK_RIGHT)&&status!=L)
 {
 status=R;
 } 
 if(GetAsyncKeyState(VK_SPACE))
 {
  while(1)
   {
 // call sleep Function to stop the process until the specified parameter time is reached  
  Sleep(300);
  if(GetAsyncKeyState(VK_SPACE))
  {
   break;
 } 
 }
 }
 else if(GetAsyncKeyState(VK_ESCAPE))
 {
   endgamestatus=3;
   break;
 }
 else if(GetAsyncKeyState(VK_F1))
 {
   speedup();
 }
 else if(GetAsyncKeyState(VK_F2))
 {
   if(sleeptime<350)
   {
   sleeptime=sleeptime+30;
   add=add-2;
   if(sleeptime==350)
   {
   add=1;
 }
   }
 }
 Sleep(sleeptime);
 snakemove();
 }
}

void lostdraw()
{
 system("cls");
 int i,j;
 gotoxy(17,5);
 color(11);
 printf("+------------------------");
 
 gotoxy(35,5);
 color(14);
 printf("o00o");
 
 gotoxy(39,5);
 color(11);
 printf("----------");
 
 gotoxy(48,5);
 color(14);
 printf("---");
 
 gotoxy(51,5);
 color(11);
 printf("----------");
 
 gotoxy(61,5);
 color(14);
 printf("o00o");
 
 gotoxy(65,5);
 color(11);
 printf("-----------------+");
 
 for(i=6;i<=19;i++)
 {
 gotoxy(17,i);
  printf("|");
  gotoxy(82,i);
  printf("|");
 }
 gotoxy(17,20);
 printf("+----------------------------------");
 
 gotoxy(52,20);
 color(11);
 printf("-----------------------------+"); 
 } 
 
 void endgame()
 {
 system("cls");
 if(endgamestatus==1)
 {
 lostdraw();
 gotoxy(35,9);
 color(12);
 printf(" Sorry, you hit a wall. Game over! ");
 }
 else if(endgamestatus==2)
 {
 lostdraw();
 gotoxy(35,9);
 color(12);
 printf(" Sorry, you bit yourself. Game over! ");
 }
 else if(endgamestatus==3)
 {
 lostdraw();
 gotoxy(40,9);
 color(12);
 printf(" You're done with the game. ");
 } 
 gotoxy(43,12);
 color(13);
 printf(" Your score is  %d",score);
 if(score>=highscore)
 {
  color(10);
  gotoxy(33,16);
  printf(" A new record! You are great!! ");
 file_in(); 
  } 
 choose(); 
 }
 void file_in()// Store the highest score in a file  
 {
 FILE *fp;
 fp=fopen("save.txt","w+");// Creates the file as read and write  
 fprintf(fp,"%d",score);
 fclose(fp);
 }
 void choose()
 {
 int n;
 gotoxy(25,23);
 color(12);
 printf(" replay 1 Bureau of -------1");
 gotoxy(52,23);
 printf(" Stop playing. Quit -------2"); 
 gotoxy(46,25);
 color(11);
 printf(" Options: ");
 scanf("%d",&n);
 switch(n)
 {
 case 1:
  system("cls");
  score=0;
  sleeptime=200;
  add=10;
  printsnake();
  wlcome();
  break;
 case 2:
  exit(0);
  break;
 default:
  gotoxy(35,27);
  color(12);
  printf(" You typed it incorrectly. Please retype it ");
  system("pause >nul");// Press any key  
 endgame();
 choose();
 break; 
 }
 } 
 void explation()
 {
 int i,j=1;
 system("cls");
 color(13);
 gotoxy(44,3);
 printf(" Game shows "); 
 color(2);
 for(i=6;i<=22;i++)
 {
 for(j=20;j<=75;j++)
 {
 gotoxy(j,i);
 if(i==6||i==22)printf("=");
 else if(j==20||j==75)printf("||");
 }
 }
 color(3);
 gotoxy(30,8);
 printf("1 You can't go through walls. You can't bite yourself ");
 color(3);
 gotoxy(30,8);
 printf("1 You can't go through walls. You can't bite yourself ");
 color(10);
 gotoxy(30,11);
 printf("2 Use ↑↓←→ Control the snake's movement respectively ");
 color(14);
 gotoxy(30,14);
 printf("3 , F1 To speed up, F2 To slow down ");
 color(11);
 gotoxy(30,17);
 printf("4 Press the space bar to pause the game and then press the space bar to continue ");
 color(4);
 gotoxy(30,20);
 printf("5 , ESC : Quit the game, space : Pause the game ");
 getch();// Press any key to return to the main interface 
 system("cls");
 printsnake();
 wlcome(); 
 
}

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary


Related articles: