利用链表的贪吃蛇,感觉自己写的时候还是有很多东西不熟悉,
1.预编译
2.很多关于系统的头文件也不是很熟悉
3.关于内存
第一个是.h头文件
#ifndef _SNAKE_H_H_H #define _SNAKE_H_H_H //调节游戏界面大小,这里设置为15*15 int const COL=15; int const ROW=15; //蛇结点 typedef struct Node { //data域 int x; int y; //指针域 struct Node* pre; struct Node* next; }NODE,*pNODE; //食物 typedef struct Food { int x; int y; char c; }FOOD,*pFOOD; //初始化函数 //初始化蛇链表,创建蛇的第一节 pNODE InitSnake(); //初始化食物 FOOD InitFood(void); //其它函数 //蛇运动函数 int MoveSnake(pNODE pHead,char c); //检查键盘按键 char KbHit(char orient);//函数重载 //蛇吃到食物时的处理函数 pNODE SnakeEatFood(pNODE pHead,pFOOD pFood); //打印游戏界面函数 void Print(pNODE pHead,FOOD food); //游戏结束,释放内存函数 void FreeMemory(pNODE *ppHead); #endif
第二个是.cpp文件
#include "stdafx.h" #include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h>//windows/dos界面下可以用来生成一些漂亮的字符,另外getchar(),getch()等函数包含在其中 #include<Windows.h> #include"Snake.h" //主函数 int main(void) { char orien = 'a', get_char; int game_over = 0; FOOD food = InitFood(); pNODE head = InitSnake(); while (1) { head = SnakeEatFood(head, &food); get_char = KbHit(orien); if (27 == get_char) { game_over = 1; break; } else orien = get_char; game_over = MoveSnake(head, orien); if (game_over) break; system("cls"); Print(head, food); Sleep(200); //ms级,刷屏间隔时间 } if (game_over) { printf("游戏结束! "); FreeMemory(&head); if (NULL == head) printf("内存释放成功! "); else printf("内存释放失败! "); } _getch(); return 0; } //初始化,创建蛇的第一节 pNODE InitSnake(void) { pNODE pHead = (pNODE)malloc(sizeof(NODE)); srand((unsigned)(time(NULL)+1)); if (NULL == pHead) { printf("内存分配失败! "); exit(-1); } pHead->x = rand() % ROW; pHead->y = rand() % COL; pHead->next = NULL; pHead->pre = NULL; return pHead; } //初始化食物成员 FOOD InitFood(void) { FOOD food; srand((unsigned)time(NULL)); food.x = rand() % ROW; food.y = rand() % COL; food.c = 65 + rand()%26; return food; } //蛇吃到食物处理函数 pNODE SnakeEatFood(pNODE pHead, pFOOD pFood) { pNODE p_add = NULL, pt = NULL, rear = NULL; if (pFood->x == pHead->x && pFood->y == pHead->y) { p_add = (pNODE)malloc(sizeof(NODE)); if (NULL == p_add) { printf("内存分配失败! "); exit(-1); } pt = pHead; while (pt->next != NULL) { pt = pt->next; } p_add->pre = pt; p_add->next = NULL; pt->next = p_add; *pFood = InitFood(); //不让食物出现在蛇的位置上 pt = pHead; while (pt != NULL) { if (pFood->x == pHead->x && pFood->y == pHead->y) { *pFood = InitFood(); break; } pt = pt->next; } } return pHead; } //检查键盘按键 char KbHit(char orient) { char c; if (_kbhit()) { c = _getch(); if (orient != 'd' && c == 'a') { orient = c; } else if (orient != 'a' && c == 'd') { orient = c; } else if (orient != 'w' && c == 's') { orient = c; } else if (orient != 's' && c == 'w') { orient = c; } else if (27 == c) { orient = c; } } return orient; } //蛇运动函数 int MoveSnake(pNODE pHead, char c) { INT game_over = 0; pNODE pt = pHead; //让pt指向蛇尾 while (pt->next != NULL) { pt = pt->next; } //从蛇尾向向蛇头前进 while(pt != pHead) { pt->x = pt->pre->x; pt->y = pt->pre->y; pt = pt->pre; } if ('d' == c) { pHead->x += 1; if (pHead->x >= ROW) { pHead->x -= ROW; } } if ('a' == c) { pHead->x -= 1; if (pHead->x < 0) { pHead->x += ROW; } } if ('s' == c) { pHead->y += 1; if (pHead->y >= COL) { pHead->y -= COL; } } if ('w' == c) { pHead->y -= 1; if (pHead->y < 0) { pHead->y += COL; } } //当蛇头碰到蛇身,游戏结束 pt = pHead->next; while (pt != NULL) { if (pt->x == pHead->x && pt->y == pHead->y) { game_over = 1; } pt = pt->next; } return game_over; } //打印游戏界面函数 void Print(pNODE pHead, FOOD food) { int row = 0, col = 0, flag = 0; pNODE pt = NULL; printf(" 方向控制——上:w 下:s 左:a 右:d(Esc退出) "); for (row=0; row<ROW; row++) { printf("—"); } putchar(' '); for (col=0; col<COL; col++) { printf(" |"); for (row=0; row<ROW; row++) { pt = pHead; flag = 0; //打印出蛇 while (pt != NULL) { if (row == pt->x && col == pt->y) { if (pt == pHead) printf("■"); else printf("□"); flag = 1; break; } pt = pt->next; } //打印出食物或两个空格 if (0 == flag) { if (row == food.x && col == food.y) { putchar(food.c); putchar(food.c); continue; } printf(" "); } } printf("|"); putchar(' '); } printf(" "); for (row=0; row<ROW; row++) { printf("—"); } putchar(' '); } //释放内存函数 void FreeMemory(pNODE *ppHead) { pNODE p_delete = NULL, pt = NULL; while (*ppHead != NULL) { pt = (*ppHead)->next; if (pt != NULL) { pt->pre = NULL; } p_delete = *ppHead; free(p_delete); p_delete = NULL; *ppHead = pt; } }