彩版_贪吃蛇C语言版
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// (彩版——贪吃蛇代码)在vc++6.0 中运行通过~~ //以下为运行效果图:
//好了,现在上源文件,,哈哈# include
# include
# include
# include
# include
# include
# define ESC 27
# define SPACE 32
# define MAX_X 25
# define MAX_Y 50
int max_food;// 食物总数
int length = 0;// 蛇身长度
char sna_dir = 'd';// 蛇头方向
char temp;
int snaDir_x, snaDir_y;// 蛇头方向坐标clock_t now_time;// 取系统时钟
double wait_time = 300;// 限制蛇的速度
typedef struct Node// 结构体
{
int x;
int y;
struct Node *pNext;
}NODE, *PNODE;
typedef struct Queue// queue 队列
{
PNODE front;
PNODE rear;
}QUEUE, *PQUEUE;
PQUEUE pSnake = (PQUEUE)malloc(sizeof(QUEUE));// 全局
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取句柄
void hideCursor()// 隐藏光标
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, &cursor_info);
}
void Setcolor(int color)// 实现彩色的函数
{
SetConsoleTextAttribute(hConsole, color);
}
void gotoxy(int x, int y)// 确定坐标
{
COORD loc;
loc.X = y;
loc.Y = x;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HAND LE), loc);
}
void destroy()// 销毁
{
PNODE p = pSnake->front;
PNODE t = pSnake->front;
while (NULL != p)//最后使pSnake->front 和p 都为NULL
{
p = p->pNext;
free(t);
t = p;
}
}
void game_over()// 游戏结束
{
gotoxy(11, 20);
destroy();
printf("Game Over");
system("pause>nul");
exit(0);
}
void enqueue(int x, int y)// 进队
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->x = x;
pNew->y = y;
pSnake->rear->pNext = pNew;
pSnake->rear = pNew;
pSnake->rear->pNext = NULL;
length++;
}
void dequeue()// 出队
{
PNODE p = pSnake->front;
pSnake->front = pSnake->front->pNext;
pSnake->front->x = p->x;
pSnake->front->y = p->y;
free(p);
length--;
}
void pri_sna_info()// 打印信息
{
int i;
Setcolor(13);
gotoxy(4, 58);
printf("Length: %d",length);
gotoxy(6, 58);
printf("还差%d 就通关了~~!", max_food+1);
for (i=51; i<80; i++)
{
gotoxy(10, i);
printf("-");
}
Setcolor(13);
gotoxy(15, 58);
printf("按1 加速");
gotoxy(16, 58);
printf("按2 减速");
gotoxy(19, 58);
printf("空格键暂停");
gotoxy(20, 58);
printf("ESC 退出");
Setcolor(15);
}
void draw_wall()// 画墙{
int i;