在Windows环境下的C语言小项目——贪吃蛇
# include <stdio.h>
# include <time.h>
# include <windows.h>
# include <stdlib.h>
# define U 1
# define D 2
# define L 3
# define R 4
typedef struct SNAKE
{
int x;
int y;
struct SNAKE * next;
} snake;
int score = 0 , add = 10 ;
int status, sleeptime = 200 ;
snake * head, * food;
snake * q;
int endGamestatus = 0 ;
void Pos ( ) ;
void CreatMap ( ) ;
void InitSnake ( ) ;
int BiteSelf ( ) ;
void CreateFood ( ) ;
void CantCrossWall ( ) ;
void SnakeMove ( ) ;
void Pause ( ) ;
void RunGame ( ) ;
void InitGame ( ) ;
void EndGame ( ) ;
void GameStart ( ) ;
int main ( )
{
GameStart ( ) ;
RunGame ( ) ;
return 1 ;
}
void Pos ( int x, int y)
{
COORD pos;
HANDLE hOutput;
pos. X = x;
pos. Y = y;
hOutput = GetStdHandle ( STD_OUTPUT_HANDLE) ;
SetConsoleCursorPosition ( hOutput, pos) ;
}
void CreatMap ( )
{
int i;
for ( i = 0 ; i < 58 ; i += 2 )
{
Pos ( i, 0 ) ;
printf ( "■" ) ;
Pos ( i, 26 ) ;
printf ( "■" ) ;
}
for ( i = 1 ; i < 26 ; i++ )
{
Pos ( 0 , i) ;
printf ( "■" ) ;
Pos ( 56 , i) ;
printf ( "■" ) ;
}
}
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