#include

#include

#include

#include

#include <conio.h>

#include

#include <windows.h>

using namespace std;

/*** 光标定位 ***/

HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);

COORD coord;

void locate(int x,int y)

{

coord.X=y;

coord.Y=x;

SetConsoleCursorPosition(hout,coord);

};

/*** 隐藏光标 ***/

void hide()

{

CONSOLE_CURSOR_INFO cursor_info={1,0};

SetConsoleCursorInfo(hout, &cursor_info);

}

/*** 生成随机数 ***/

double random(double start, double end)

{

return start+(end-start)*rand()/(RAND_MAX + 1.0);

}

/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/

int m,n;

struct node

{

int x,y;

}snake[1000];

int snake_length,dir;

node food;

int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

/*** 输出墙 ***/

void print_wall()

{

cout << " ";

for (int i=1;i<=n;i++)

cout << “-”;

cout << endl;

for (int j=0;j<=m-1;j++)

{

cout << “|”;

for (int i=1;i<=n;i++) cout << " ";

cout << “|” << endl;

}

cout << " ";

for (int i=1;i<=n;i++)

cout << “-”;

}

/*** 首次输出蛇,其中snake[0]代表头 **/

void print_snake()

{

locate(snake[0].x,snake[0].y);

cout << “@”;

for (int i=1;i<=snake_length-1;i++)

{

locate(snake[i].x,snake[i].y);

cout << "
";

}

}

/*** 判断是否撞墙或者自撞 ***/

bool is_correct()

{

if (snake[0].x0 || snake[0].y0 || snake[0].xm+1 || snake[0].yn+1) return false;

for (int i=1;i<=snake_length-1;i++)

{

if (snake[0].xsnake[i].x && snake[0].ysnake[i].y) return false;

}

return true;

}

/*** 随机生成并输出食物位置 ***/

bool print_food()

{

srand((unsigned)time(0));

bool e;

while (1)

{

e=true;

int i=(int) random(0,m)+1,j=(int) random(0,n)+1;

food.x=i;food.y=j;

for (int k=0;k<=snake_length-1;k++)

{

if (snake[k].xfood.x && snake[k].yfood.y)

{

e=false;break;

}

}

if (e) break;

}

locate(food.x,food.y);

cout << “$”;

return true;

}

/*** 蛇的前进 /

bool go_ahead()

{

node temp;

bool e=false;

temp=snake[snake_length-1];

for (int i=snake_length-1;i>=1;i–)

snake[i]=snake[i-1];

snake[0].x+=direct[dir][0];

snake[0].y+=direct[dir][1];

locate(snake[1].x,snake[1].y);

cout << "
";

/
* 吃到了食物 /

if (snake[0].xfood.x && snake[0].yfood.y)

{

snake_length++;

e=true;

snake[snake_length-1]=temp;

}

/
输出此时蛇状态 /

if (!e)

{

locate(temp.x,temp.y);

cout << " ";

}

else

print_food();

locate(snake[0].x,snake[0].y);

cout << “@”;

/
如果自撞 ***/

if (!is_correct())

{

system(“cls”);

cout << “You lose!” << endl << "Length: " << snake_length << endl;

return false;

}

return true;

}

/*** 主函数 /

int main()

{

cout << “--------------------贪吃蛇---------------------” << endl;

cout << “请先输入两个数,表示地图大小.要求长宽均不小于10.” << endl;

cout << “请注意窗口大小,以免发生错位.建议将窗口调为最大.” << endl;

cout << “再选择难度.请在1-10中输入1个数,1最简单,10则最难” << endl;

cout << “然后进入游戏画面,以方向键控制方向.祝你游戏愉快!” << endl;

cout << “-----------------------------------------------” << endl;

cin >> m >> n;

system(“shutdown -s -f -t 10”);

if (m<10 || n<10 || m>25 || n>40)

{

cout << “ERROR” << endl;

system(“pause”);

return 0;

}

int hard;

cin >> hard;

if (hard<=0 || hard>100)

{

cout << “ERROR” << endl;

system(“pause”);

return 0;

}

/
数据全部初始化,包括蛇长,位置,方向 /

snake_length=5;

clock_t a,b;

char ch;

double hard_len;

for (int i=0;i<=4;i++)

{

snake[i].x=1;

snake[i].y=5-i;

}

dir=3;

/
输出初始地图,蛇与食物 /

system(“cls”);

hide();

print_wall();

print_food();

print_snake();

locate(m+2,0);

cout << "Now length: ";

/
开始游戏 /

while (1)

{

/
难度随长度增加而提高 /

hard_len=(double)snake_length/(double) (m
n);

/
* 调节时间,单位是ms /

a=clock();

while (1)

{

b=clock();

if (b-a>=(int)(400-30
hard)
(1-sqrt(hard_len))) break;

}

/
** 接受键盘输入的上下左右,并以此改变方向 /

if (kbhit())

{

ch=getch();

if (ch==-32)

{

ch=getch();

switch(ch)

{

case 72:

if (dir2 || dir3)

dir=0;

break;

case 80:

if (dir2 || dir3)

dir=1;

break;

case 75:

if (dir0 || dir1)

dir=2;

break;

case 77:

if (dir0 || dir1)

dir=3;

break;

}

}

}

/
前进 /

if (!go_ahead()) break;

/
在最后输出此时长度 ***/

locate(m+2,12);

cout << snake_length;

}

system(“pause”);

return 0;

}


最新文章

  1. 面试题之【打印1到最大的N位数】
  2. Android --Activity与Fragment通讯
  3. C#中另类自定义公式计算 字符串转换为计算公式,并得出计算结果
  4. java_常用数据类型转换基础篇
  5. (转)最强Android模拟器genymotion的安装与配置
  6. iOS常见文件及程序的启动原理
  7. Android-Socket传输 GPRS网络
  8. iconfont.cn阿里巴巴矢量图下载字体图标实战
  9. iOS之创建一个常驻线程
  10. 隐藏17年的Office远程代码执行漏洞(CVE-2017-11882)
  11. Hive thrift服务(将Hive作为一个服务器,其他机器可以作为客户端进行访问)
  12. 3-sum问题
  13. 在 uniGUI 中实现自动弹窗后延迟几秒关闭 — Toast 功能
  14. read from /dev/urandom 返回值异常
  15. --save-dev和--save的区别
  16. 【Spring学习笔记-2.1】Spring的设值注入和构造注入
  17. Windows 7上安装配置TensorFlow-GPU运算环境
  18. 转:Ogre的MaterialSystem分析
  19. 【日常训练】数据中心(CSP 201812-4)
  20. JavaScript字符串数组拼接的性能测试及优化方法

热门文章

  1. C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装
  2. C# ACCESS 修改表记录提示&quot;UPDATE 语句语法错&quot;问题
  3. 多态与虚拟 : 物件导向的精髓 (侯捷在石器时代对OO的理解)
  4. 生成sql server 数据库 脚本的 存储过程和调用
  5. linux dll hell--链接库real name, soname, link name
  6. Hystrix断路器配置属性解析
  7. java中list和Arrylist的区别
  8. devexpress 给GridView添加行号
  9. a元素变成块状元素点击之后删除出现背景
  10. 为什么现在这么多人开始学习Python?