一键建设网站,微信小程序怎么做成链接,互联网招聘网站,新闻媒体发稿平台利用空余时间写出了这么一个简单的小游戏#xff0c;直接使用dos界面运行。那么问题来了#xff0c;如何让一个字符在dos界面上自由移动#xff1f;#xff1f;#xff1f;对于这个问题我采用的解决方案是实现gotoxy函数来控制指针位置从而实现字符的移动。那么我们就先来… 利用空余时间写出了这么一个简单的小游戏直接使用dos界面运行。那么问题来了如何让一个字符在dos界面上自由移动对于这个问题我采用的解决方案是实现gotoxy函数来控制指针位置从而实现字符的移动。那么我们就先来实现这个函数。gotoxy 函数并非系统函数我将其储存于 gotoxy.h 的头文件中方便调用。gotoxy.h#include lt;windows.hgt;
void gotoxy(int x,int y)
{ COORD pos; pos.X x - 1; pos.Y y - 1; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}现在我们已经能够利用 gotoxy 函数对指针进行控制那么实现字符的移动则只需将原来位置的字符清除然后利用此函数移动指针到想去的坐标后打印字符即可。在对此函数进行测试的时候我发现了一个重要的问题因为代码是一行一行的运行那么在等待我输入方向的时候其他代码是无法执行的这意味这我的蛇只能是我给一下方向它移动一下那么该如何使得字符在等待我输出方向的同时自行移动呢对于这个问题有两个解决方案一、创建线程对于当时的我来说线程还很陌生 二、利用 kbhit() 非阻塞函数百度一下你就知道。当然我选择的是第二个方案再配合使用 getch() 函数即可完美实现方向的输入。该游戏的两个难点都解决了话不多说 ↓这里主要运用到的知识有这些结构体srand(), rand(), kbhit(), getch(), Sleep()./*******************http://blog.csdn.net/lcsy000**********************/ #includelt;iostreamgt;
#includegotoxy.h
#includelt;windows.hgt;
#includelt;conio.hgt;
#includelt;time.hgt;
using namespace std;
char direction_a,direction_b; //方向a、b,用于方向的限制
int scores,num,fool_x,fool_y,speed100; //得分、num用于蛇身起步、食物x坐标、食物y坐标
bool end; //结束标记
struct node //蛇身结点
{ int x,y; node *next;
}*headNULL,*p,*tail; void init(); //初始化开始界面
void start(); //游戏开始入场
void init_snake(); //初始化蛇身
void delete_snake(); //删除蛇身
void control(); //方向控制
void move(); //蛇身移动
void limit(); //方向限制
void panduan(); //配合limit限制方向
void fool(); //食物的出现以及食物被吞
void isEnd(); //结束判断
void zhuangwei(); //撞尾判断
void zhuangqiang(); //撞墙判断 int main ()
{ srand((unsigned)time(NULL)); init(); cingt;gt;direction_a; if(direction_a!yamp;amp;direction_a!Y) return 0; do { system(cls); //清除屏幕 endfalse; start(); delete_snake(); init_snake(); scores0; num0; fool_x(rand() % (79-21)) 2; fool_y(rand() % (22-21)) 2; gotoxy(fool_x,fool_y); coutlt;lt;0; direction_agetch(); while(direction_a!damp;amp;direction_a!samp;amp;direction_a!w) direction_agetch(); while(true) { if(numamp;amp;direction_a!damp;amp;direction_a!samp;amp;direction_a!wamp;amp;direction_a!a) { direction_adirection_b; } control(); fool(); Sleep(speed); if(kbhit()) //kbhit 非阻塞函数 { direction_agetch(); //使用 getch 函数获取键盘输入 limit(); } panduan(); num1; zhuangqiang(); zhuangwei(); if(end) break; } }while(direction_ay||direction_aY); return 0;
}
void init()
{ gotoxy(35,8); coutlt;lt;★贪 吃 蛇★; gotoxy(36,10); coutlt;lt;开始请输入y:;
}
void start()
{ for(int i0;ilt;79;i) { Sleep(10); coutlt;lt;*; gotoxy(i1,24); coutlt;lt;*; gotoxy(i2,1); } gotoxy(1,2); for(int i0;ilt;21;i) { Sleep(20); coutlt;lt;*; for(int j0;jlt;77;j) coutlt;lt; ; coutlt;lt;*; }
}
void init_snake()
{ int n3; headnew node; tailhead; head-gt;x40; head-gt;y12; while(n--) { pnew node; tail-gt;nextp; p-gt;xtail-gt;x-1; p-gt;ytail-gt;y; tailp; } tail-gt;nextNULL; node *qhead-gt;next; gotoxy(head-gt;x,head-gt;y); coutlt;lt;#; while(q!NULL) { gotoxy(q-gt;x,q-gt;y); coutlt;lt;*; qq-gt;next; }
}
void delete_snake()
{ while(head!NULL) { node *qhead; headq-gt;next; delete q; }
}
void move()
{ gotoxy(tail-gt;x,tail-gt;y); coutlt;lt; ; gotoxy(head-gt;next-gt;x,head-gt;next-gt;y); coutlt;lt;*; gotoxy(head-gt;x,head-gt;y); coutlt;lt;#; node *qtail; tailhead; while(tail-gt;next!q) { tailtail-gt;next; } tail-gt;nextNULL; delete q;
}
void control()
{ node *qnew node; q-gt;nexthead; q-gt;xhead-gt;x; q-gt;yhead-gt;y; headq; switch(direction_a) { case w: head-gt;y--;break; case s: head-gt;y;break; case a: head-gt;x--;break; case d: head-gt;x;break; default : break; } move();
}
void limit()
{ if(direction_bsamp;amp;direction_aw) direction_as; if(direction_bwamp;amp;direction_as) direction_aw; if(direction_baamp;amp;direction_ad) direction_aa; if(direction_bdamp;amp;direction_aa) direction_ad;
}
void panduan()
{ if(direction_as) direction_bs; if(direction_aw) direction_bw; if(direction_ad) direction_bd; if(direction_aa) direction_ba;
}
void fool()
{ node *q; if(head-gt;xfool_xamp;amp;head-gt;yfool_y) { fool_x(rand() % (79-21)) 2; fool_y(rand() % (22-21)) 2; gotoxy(fool_x,fool_y); coutlt;lt;0; num0; scores; node *qnew node; q-gt;xtail-gt;x; q-gt;ytail-gt;y; tail-gt;nextq; tailq; tail-gt;nextNULL; } qhead; while(q!NULL) { if(q-gt;xfool_xamp;q-gt;yfool_y) { fool_x(rand() % (79-21)) 2; fool_y(rand() % (22-21)) 2; gotoxy(fool_x,fool_y); coutlt;lt;*; qhead; continue; } qq-gt;next; }
}
void isEnd()
{ endtrue; Sleep(600); system(cls); gotoxy(35,8); coutlt;lt;您 输 啦 ~; gotoxy(33,10); coutlt;lt;您的分数为: lt;lt;scores; gotoxy(31,12); coutlt;lt;重新开始请输入y:; cingt;gt;direction_a;
}
void zhuangwei()
{ node *qhead-gt;next; while(q!NULL) { if(head-gt;xq-gt;xamp;amp;head-gt;yq-gt;y) { isEnd(); break; } qq-gt;next; }
}
void zhuangqiang()
{ if(head-gt;x80||head-gt;x1||head-gt;y24||head-gt;y1) isEnd();
}效果图由于考虑到游戏的各种 BUG 故自定义函数很多有兴趣的朋友可以自行改动一些函数对比效果。完它不仅仅是一个码扫码关注C资源免费送