C++ VS2013 贪吃蛇小游戏
2014-12-13 12:50
381 查看
#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<windows.h> #include<time.h> #include<stdlib.h> #include<conio.h> #define N 21 void gotoxy(int x, int y)//位置函数 { COORD pos; pos.X = 2 * x; pos.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void color(int a)//颜色函数 { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); } void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果) { int i, j;//初始化围墙 int wall[N + 2][N + 2] = { { 0 } }; for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) wall[i][j] = 1; } color(11); for (i = 0; i<N + 2; i++) { for (j = 0; j<N + 2; j++) { if (wall[i][j]) std::cout << "■"; else std::cout << "□"; } std::cout << std::endl; } gotoxy(N + 3, 1);//显示信息 color(20); std::cout << "按 W S A D 移动方向" << std::endl; gotoxy(N + 3, 2); color(20); std::cout << "按任意键暂停" << std::endl; gotoxy(N + 3, 3); color(20); std::cout << "得分:" << std::endl; apple[0] = rand() % N + 1;//苹果 apple[1] = rand() % N + 1; gotoxy(apple[0], apple[1]); color(12); std::cout << "●" << std::endl; } int main() { int i, j; int** snake = NULL; int apple[2]; int score = 0; int tail[2]; int len = 3; char ch = 'p'; srand((unsigned)time(NULL)); init(apple); snake = (int**)realloc(snake, sizeof(int*)*len); for (i = 0; i<len; i++) snake[i] = (int*)malloc(sizeof(int)* 2); for (i = 0; i<len; i++) { snake[i][0] = N / 2; snake[i][1] = N / 2 + i; gotoxy(snake[i][0], snake[i][1]); color(14); std::cout << "★" << std::endl; } while (1)//进入消息循环 { tail[0] = snake[len - 1][0]; tail[1] = snake[len - 1][1]; gotoxy(tail[0], tail[1]); color(11); std::cout << "■" << std::endl; for (i = len - 1; i>0; i--) { snake[i][0] = snake[i - 1][0]; snake[i][1] = snake[i - 1][1]; gotoxy(snake[i][0], snake[i][1]); color(14); std::cout << "★" << std::endl; } if (_kbhit()) { gotoxy(0, N + 2); ch = _getche(); } switch (ch) { case 'w':snake[0][1]--; break; case 's':snake[0][1]++; break; case 'a':snake[0][0]--; break; case 'd':snake[0][0]++; break; default: break; } gotoxy(snake[0][0], snake[0][1]); color(14); std::cout << "★" << std::endl; Sleep(abs(200 - 0.5*score)); if (snake[0][0] == apple[0] && snake[0][1] == apple[1])//吃掉苹果后蛇分数加1,蛇长加1 { score++; len++; snake = (int**)realloc(snake, sizeof(int*)*len); snake[len - 1] = (int*)malloc(sizeof(int)* 2); apple[0] = rand() % N + 1; apple[1] = rand() % N + 1; gotoxy(apple[0], apple[1]); color(12); std::cout << "●" << std::endl; gotoxy(N + 5, 3); color(20); std::cout << score << std::endl; } if (snake[0][1] == 0 || snake[0][1] == N || snake[0][0] == 0 || snake[0][0] == N)//撞到围墙后失败 { gotoxy(N / 2, N / 2); color(30); std::cout << "失败!!!" << std::endl; for (i = 0; i<len; i++) free(snake[i]); Sleep(INFINITE); exit(0); } } return 0; }
相关文章推荐
- VS2013 C++ 关闭安全检查
- C++基于控制台实现的贪吃蛇小游戏
- 请禁用VS(C++)2013的链接器的COMDAT选项
- vs2013无法编译c/c++ 错误 TRK0002: 未能执行命令cl.exe 存储控制块地址无效
- 设置 VS2013 c++ 新建项目时默认为64位
- 如何使用VS2013本地C++单元测试框架
- C++ VS2013 修改阳光 三
- C++ VS2013 修改金钱 二
- 如何使用VS2013对C++进行编程
- VS2013编译的用c++写的win32程序兼容性问题
- VS2013 c++ 修改默认活动解决方案平台
- VS2013 C++ 项目占用空间较大的解决方法
- C++ VS2013 种植物 四
- Lua在vs2013的配置环境以及C++如何调用lua
- matlab2013 a/b 找不到vs2013 c++ 编译器 解决方案
- 吐槽VS2013 C++编辑器的一些“BUG”
- libcurl在vs2013下C++项目中的编译连接
- WIN7 + VS2013 使用c++创建项目各种报错问题解决方案
- C++连接MYSQL数据库(使用MYSQL API方式,环境VS2013+MYSQL5.6)
- C++实现贪吃蛇小游戏