带有N多BUG的小程序——贪吃蛇
2015-11-25 20:18
791 查看
只考虑贪吃蛇的头和尾对象,对头尾做不同的动作分析。
//bug::1食物可能出现在蛇身上 //2不能穿墙或者遇到墙失败之类的、。 //3蛇身体相交时候,就乱了。 #include<iostream> #include <stdlib.h> #include <time.h> using namespace std; int map[10][10] = { 0 }; int flag = 0;//为1 时候需要重新生成食物 class Point_head { int x; int y; public: Point_head(int tx, int ty) :x(tx), y(ty){} void run_right() { y += 1; } void run_left() { y -= 1; } void run_up() { x -= 1; } void run_down() { x += 1; } void put() { map[x][y] = 1;//1为蛇 } bool judge()//判断是否碰到食物 { if (map[x][y] == 2) { flag = 1; return true; } else return false; } }; class Point_tail { int x; int y; public: Point_tail(int tx, int ty) :x(tx), y(ty){} void move()//移动尾部对象 { if (map[x][y-1] == 1) y -= 1; else if (map[x][y+1] == 1) y += 1; else if (map[x-1][y] == 1) x -= 1; else if (map[x+1][y] == 1) x += 1; } void put()//尾部对象赋值 { map[x][y] = 0;//1为蛇 } }; //void init() //{ // map[5][5] = map[5][6] =map[5][7]= 1; // Point_head P1(5, 5); // Point_tail P2(5, 7); //} void print() { for (int i = 0; i != 10; i++) { for (int j = 0; j != 10; j++) { cout << map[i][j] << " "; } cout << endl; } } int random()//生成0——9的随机数 { srand((unsigned)time(NULL)); int num = rand() % 10; while (num == 5||num==6||num==7) num = rand() % 10; return num; } int main() { cout << "贪吃蛇小游戏,0为空地,1为蛇,2为食物" << endl << "w向上,a向左,s向下,d向右" << endl; map[5][5] = map[5][6] = map[5][7] = 1; Point_head P1(5, 5); Point_tail P2(5, 7); map[random()][random()] = 2;//2为食物 print(); char dir; while (cin >> dir) { switch (dir) { case 'a':P1.run_left(); if (P1.judge()){ P1.put(); } else { P1.put(); P2.put(); P2.move(); } break; case 's':P1.run_down(); if (P1.judge()){ P1.put(); } else { P1.put(); P2.put(); P2.move(); }break; case 'd':P1.run_right(); if (P1.judge()){ P1.put(); } else { P1.put(); P2.put(); P2.move(); }break; case 'w':P1.run_up(); if (P1.judge()){ P1.put(); } else { P1.put(); P2.put(); P2.move(); }break; } if (flag == 1) { map[random()][random()] = 2;//2为食物 flag = 0; } print(); } return 0; }
相关文章推荐
- 微信第三方平台推送verify_ticket的接收处理(PHP实现)
- Android实现微信分享及注意事项
- C# 微信扫码支付 回调页面
- 微信扫码支付模式二支付状态Ajax轮询实例
- 微信中得到的GPS经纬度放在百度,腾迅地图中不准的原因及处理
- js检测页面是否在微信浏览器打开
- 四大行及邮储微信银行体验
- 用阿里云构建自己的Ngrok
- 微信移动支付V3开发详细教程服务端采用.net mvc webapi(C#)
- HTML5与微信开发(1)-HTML标签语法变化和使用概念
- Android 超高仿微信图片选择器 图片该这么加载
- SSO第三方登录友盟微信登录失败
- iOS 第三方支付 支付宝 银联 微信 遇到的问题
- 微信红包接口实现
- 使用retrofit实现微信登录的相关接口
- 友盟登录微信第三方登录报错解决linker command failed with exit code 1
- iOS 微信支付集成
- WPF控件--利用Winform库中的NotifyIcon实现托盘小程序
- 安卓版微信小视频先保存再分享方法
- iOS客户端,微信,bug 通知栏点击失效,且无法删除。