#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h> int g_Dir = ;
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3 struct FOOD
{
int X = ;
int Y = ;
bool State = ;
}Food; //方向控制
void SnekeMove()
{
if (::GetAsyncKeyState(VK_UP) & )
g_Dir = ;
if (::GetAsyncKeyState(VK_DOWN) & )
g_Dir = ;
if (::GetAsyncKeyState(VK_LEFT) & )
g_Dir = ;
if (::GetAsyncKeyState(VK_RIGHT) & )
g_Dir = ;
} //主函数
int main()
{
srand((unsigned int)time(NULL));
int W = ;
int H = ;
int Len = ;
int Map[ * ] = { };
int Snake[] = { };
//Snake[0] = 2;//为蛇头 for (int i = ; i < Len; i++)
{
Snake[i] = Len - i - ;
}
Food.X = W / ;
Food.Y = H / ;
Map[Food.Y * W + Food.X] = ; const char *SNAKE[] = { " ", "■" ,"★"};
while (true)
{
system("cls");
for (int i = ; i < Len; i++)
{
// 2 1 0
// 1 1 1 // 3 2 1
// 0 1 1 1
//这里需要注意
Map[Snake[i]] = ;
}
Map[Food.Y * W + Food.X] = ;
if (Map[Snake[]] == Map[Food.Y * W + Food.X])
{
Map[Food.Y * W + Food.X] = ;
do
{
bool FoodState = false;
Food.X = rand() % W;
Food.Y = rand() % H;
for (int i = ; i < Len; i++)
{
if (Map[Food.Y * W + Food.X] != Map[Snake[i]])
{
FoodState = true;
}
}
if (FoodState)
{
Map[Food.Y * W + Food.X] = ;
break;
}
} while (true);
Len++;
} for (int i = ; i < H; i++)
{
for (int j = ; j < W; j++)
{
//蛇头在地图上的坐标
if (i * W + j == Snake[])
{
if (i == H - || j == W - )
{
//撞墙
system("pause");
return ;
}
}
}
} for (int i = ; i < H; i++)
{
for (int j = ; j < W; j++)
{
std::cout << SNAKE[Map[i * W + j]];
}
std::cout << std::endl;
}
std::cout << std::endl;
//for (int i = 0; i < Len; i++)
//{
// std::cout << Snake[i]<<" ";
//}
//std::cout << std::endl;
std::cout <<"蛇的长度为 : "<<Len<<std::endl;
memset(Map, , sizeof(Map)); SnekeMove(); for (int i = Len; i > ; i--)
{
// 2 1 0
//默认往右挪
// 2 2 1 0
//后面++
// 3 2 1 0
//完成一次移动
Snake[i] = Snake[i - ];
/*
比如往下
// 3 2 1 0
// 23 3 2 1 0
*/
}
if (g_Dir == UP)
{
Snake[] -= W;
}
if (g_Dir == DOWN)
{
Snake[] += W;
}
if (g_Dir == LEFT)
{
Snake[]--;
}
if (g_Dir == RIGHT)
{
Snake[]++;
} Sleep();
} //https://www.bilibili.com/video/av29007126/?spm_id_from=333.788.videocard.1
system("pause");
return ;
}

最新文章

  1. Android初级教程_获取Android控件的宽和高
  2. PHP基础班初学心得:JS里inner的一些理解
  3. svn server
  4. IOS UIScrollView + UIButton 实现segemet页面和顶部标签页水平滚动效果
  5. javaweb实现验证码功能
  6. UML: 状态机图
  7. Contest1065 - 第四届“图灵杯”NEUQ-ACM程序设计竞赛(个人赛)D拿糖果
  8. msm8610 lcd driver code analysis
  9. 超好:web app变革之rem
  10. STM32驱动W25X64存储器
  11. UE4使用C++创建枚举变量适用于C++与蓝图
  12. 南京邮电大学java程序设计作业在线编程第八次作业
  13. IDEA 创建包和类及基本操作
  14. angularjs 设置全局变量(constant)
  15. vscode中添加git
  16. android sdk下载及安装教程
  17. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码
  18. A1016. Phone Bills
  19. JVM调优总结 -Xms
  20. Materialize和Material Design Lite的区别

热门文章

  1. 将时间 &#39;2018-08-06T10:00:00.000Z&#39; 格式转化为本地时间
  2. linux每日命令(3):which命令
  3. spring Aop设计原理
  4. Vue下渐变效果有时候失效
  5. react-router v4 理解
  6. 实体类Json串转成DataTable
  7. webapi 找到了与请求匹配的多个操作(ajax报500,4的错误)
  8. apache httpd.conf alias
  9. Jenkins忘记 admin 密码
  10. Interview - 面试题汇总目录