Code
#include <stdio.h> #include <windows.h> #include <time.h> #define Framewidght #define Frameheight 22
Short Generaterandnumber (short A, short b);
void Posconsolecursor (short x, short y);
void Hideconsolecursoricon ();
void Initstarthint ();
void Inithint ();
void Initframe ();
void Initsnake ();
void Initgame ();
void Generatefood ();
int knocksankebody (int yx);
void move (short dx, short dy);
void Pausegame ();
void Listenkey (int *dx, int *dy);
void run ();
/** * Definition community, short x,y and int p occupy the same position * A small section of the position of the structure * system uses the small end mode of storage * x is P's low, Y is P's high/typedef struct _POSITION {Union
{struct {short x,y;
} ;
int YX;
};
struct _position *next;
}position;
/** * The structure of the snake * * typedef struct{INT len;
Position *front;
Position *rear;
} Snake;
/** * Define global variable */Snake Snake;
Position *food;
int score = 0;
int isgameover;
/** * Main function */int main () {//Initialize global variable food = NULL;
Score = 0;
Isgameover = 0; Snake.front = NULL;
Snake.len = 0;
Snake.rear = NULL;
Random seed Srand ((unsigned) time (NULL));
Initialize the display of initgame ();
Run ();
Posconsolecursor ((framewidght+1) *2,4);
printf ("Game over!!, your score is:");
System ("pause");
Release of the requested space if (food!=null) free (food);
Position *p = Snake.front;
Position *next = NULL;
while (p!=null) {next = P->next;
Free (p);
P =next;
return 0;
/** * Generate random number function * * Short generaterandnumber (short A,short b) {return A+rand ()% (b-a+1);
/** * Move cursor position/void Posconsolecursor (short x,short y) {COORD pos; Pos.
x = x; Pos.
y = y;
HANDLE houtput = GetStdHandle (Std_output_handle);
SetConsoleCursorPosition (Houtput,pos);
return;
}/** * Hide cursor/void Hideconsolecursoricon () {Console_cursor_info CCI;
Cci.bvisible = false;
cci.dwsize = sizeof (CCI);
HANDLE houtput = GetStdHandle (Std_output_handle); SetconsolecursorInfo (Houtput,&cci);
return;
}/** * Initialization game start prompt column/void Initstarthint () {printf ("\n\n\t");
printf ("Game hint:");
printf ("\n\n\t");
printf ("↑ Move \t↓ down \t← left shift \t→ right)";
printf ("\n\n\t");
printf ("Space suspend \tesc end");
printf ("\n\n\t");
System ("pause");
System ("CLS");
/** * initialization prompt/void Inithint () {posconsolecursor (framewidght+1) *2,4);
printf ("Your Score:");
Posconsolecursor ((framewidght+1) *2,5);
printf ("%d", score);
}/** * Initialization box/void Initframe () {int i;
Hideconsolecursoricon ();
For (i=0 i <framewidght; ++i) {posconsolecursor (i*2,0);
printf ("-");
For (i=0 i <framewidght; ++i) {posconsolecursor (i*2,frameheight-1);
printf ("-");
For (I=1 i <frameheight-1; ++i) {posconsolecursor (0,i);
printf ("-");
For (I=1 i <frameheight-1; ++i) {Posconsolecursor ((FrameWidght-1) *2,i); printf ("-");
}/** * Initialize snake/void Initsnake () {Position *p = (Position *) malloc (sizeof (Position));
Position *next = NULL; P->yx = ((FrameHeight-2)/2<<16) |
(framewidght)/2;
P->x = FRAMEWIDGHT/2;
P->y = (FrameHeight-2)/2;
Snake.front = p;
Posconsolecursor (P->x*2,p->y);
printf ("");
Snake.len = 4;
for (int i=0; i<3; ++i) {next = (position*) malloc (sizeof (Position)); Next->yx = (p->y<<16) |
(p->x-1);
Next->x = p->x-1;
Next->y = P->y;
P->next = Next;
p = Next;
Posconsolecursor (P->x*2,p->y);
printf ("");
} p->next = NULL;
Snake.rear = p;
/** * Initialization game */void Initgame () {initstarthint ();
Initframe ();
Inithint ();
Initsnake ();
Generatefood ();
/** * Generate food/void Generatefood () {short x, y;
int YX; do {x = Generaterandnumber (1,frAMEWIDGHT-2);
y = Generaterandnumber (1,frameheight-2);
YX = (y<<16) |x;
}while (Knocksankebody (YX));
Food = (position*) malloc (sizeof (Position));
Food->yx = YX;
Food->next =null;
Posconsolecursor (Food->x*2,food->y);
printf ("");
/** * Determines whether the generated random number is on the body of the snake * * int knocksankebody (int yx) {int flag = 0;
Position *p = Snake.front;
for (int i= 0; i<snake.len; ++i) {if (P->yx==yx) {flag = 1;
Break
} p = P->next;
return flag;
/** * Suspend game/void Pausegame () {while (1) {sleep (500);
if (Getasynckeystate (vk_space)) break;
}/** * Snake mobile/Void Move (short dx, short dy) {Position *p = Snake.front;
Position *move_p = snake.rear;
int x = SNAKE.FRONT->X+DX;
int y = Snake.front->y+dy;
int yx = (y<<16) |x;
Determine if the hit frame if (x<1 | | x > (FRAMEWIDGHT-2) || y<1 | |
Y > (FrameHeight-2)) {isgameover = 1;
return;
else if (Yx = = Food->yx)//whether to eat food {food->next = Snake.front;
Snake.front = food;
snake.len++;
score+=10;
Posconsolecursor ((framewidght+1) *2,5);
printf ("%d", score);
Generatefood ();
else {while (p->next!=move_p) {//Whether or not to hit the body if (P->YX==YX)
{isgameover = 1;
break;
} p = P->next;
} if (isgameover!=1) {snake.rear = P;
P->next = NULL;
Clear Posconsolecursor (move_p->x*2,move_p->y);
printf ("");
Print head move_p->yx = YX;
Move_p->next = Snake.front;
Snake.front = move_p;
Posconsolecursor (Snake.front->x*2,snake.front->y); printf ("");
}}/** * Listening key/void Listenkey (int *dx, int *dy) {if (Getasynckeystate (vk_up)) {if (*dy==0)
{*dx= 0;
*dy=-1;
} return;
} if (Getasynckeystate (Vk_down)) {if (*dy==0) {*dx= 0;
*dy=1;
} return;
} if (Getasynckeystate (Vk_left)) {if (*dx==0) {*dx=-1;
*dy=0;
} return;
} if (Getasynckeystate (vk_right)) {if (*dx==0) {*dx=1;
*dy=0;
} return;
} if (Getasynckeystate (Vk_space)) {pausegame ();
}/** * Game run/Void run () {int dx=1;
int dy=0;
while (isgameover==0) {sleep (200);
Listenkey (&dx,&dy);
Move (Dx,dy);
}
}
Results