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