链表版本比数组版本少用了“cls”指令,不需通过更新屏幕状态来实现蛇的移动,核心功能是,通过尾插法和删除链表元素来实现移动。这个版本增加了一些新功能:记录分数,将最高分写入文件,当分数越高,蛇的移动越快。
蛇身的定义:(与单链表类似)
typedef struct snake {
int x, y;
struct snake *next;
} SNAKE;
下面是功能函数:
void gotoxy(int x, int y);//location
void paintWall();//create the map
int biteSelf(SNAKE *s);//if the snake bite itself
void init();//initial the snake
void moveSnake();//make sure that the snake can move
void ChangeDir();//scan the keyboard and change the direction
void inifood();//initial food
void scoreAdd(int flag);//add the score
void gameOver();
定位屏幕函数与上一版本无异
void paintWall()
{
FILE *fp;//创建一个记录最高分的文件
fp = fopen("score.txt", "r");
fscanf(fp, "%d", &bestScore);
fclose(fp);
gotoxy(85, 8);
printf("YOUR BEST SCORE : %d\t", bestScore);
int i, j;
for (i = 0; i < 81; i++) {
gotoxy(i, 0);
printf("*");
gotoxy(i, 28);
printf("*");
}
for (i = 1; i < 28; i++) {
gotoxy(0, i);
printf("*");
gotoxy(81, i);
printf("*");
}
}
初始化蛇身:
void init()
{
SNAKE *t;
int i;
head = (SNAKE *)malloc(sizeof(SNAKE));
head->x