【C++游戏程序】easyX图形库还原游戏《贪吃蛇大作战》(三)
承接上一篇文章:【C++游戏程序】easyX图形库还原游戏《贪吃蛇大作战》(二),我们这次来补充一些游戏细节,以及增加吃食物加长角色长度等设定玩法,也是本游戏的最后一篇文章。
一.玩家边界检测
首先是用来检测玩家是否触碰地图边界的代码部分:
if (player.x > map_wide || player.x <0 || player.y >map_hight || player.y < 0){body_num = 5;score = 100;kill = 0;player.x = Wide / 2;player.y = Hight / 2;player.r = 10;player_body[0].x = player.x - player.r;player_body[0].y = player.y;player_body[0].r = player.r;for (int i = 1; i < body_num; i++){player_body[i].x = player_body[i - 1].x - player_body[i - 1].r;player_body[i].y = player_body[i - 1].y;player_body[i].r = player_body[i - 1].r;}}
二.AI边界检测/AI触碰玩家尾部
for (int i = 0; i < Ai_num; i++){if (Ai[i].x > map_wide || Ai[i].x <0 || Ai[i].y >map_hight || Ai[i].y < 0){Ai_body_food_num[i] = Ai_body_num[i];for (int z = 1; z < Ai_body_num[i]; z++){Ai_body_food[i][z].x = Ai_body[i][z].x;Ai_body_food[i][z].y = Ai_body[i][z].y;Ai_body_food[i][z].r = Ai_body[i][z].r;Ai_body_food[i][z].color = RGB(rand() % 256, rand() % 256, rand() % 256);}Ai[i].x = rand() % map_wide;Ai[i].y = rand() % map_hight;Ai[i].r = 10;Ai_body_num[i] = 5;Ai_body[i][0].x = Ai[i].x - Ai[i].r;Ai_body[i][0].y = Ai[i].y;Ai_body[i][0].r = Ai[i].r;for (int j = 1; j < Ai_body_num[i]; j++){Ai_body[i][j].x = Ai_body[i][j - 1].x - Ai_body[i][j - 1].r;Ai_body[i][j].y = Ai_body[i][j - 1].y;Ai_body[i][j].r = Ai_body[i][j - 1].r;}}for (int j = 0; j < body_num; j++){if (Distance(player_body[j].x, player_body[j].y, Ai[i].x, Ai[i].y) < Ai[i].r + player_body[i].r){kill += 1;Ai_body_food_num[i] = Ai_body_num[i];for (int z = 1; z < Ai_body_num[i]; z++){Ai_body_food[i][z].x = Ai_body[i][z].x;Ai_body_food[i][z].y = Ai_body[i][z].y;Ai_body_food[i][z].r = Ai_body[i][z].r;Ai_body_food[i][z].color = RGB(rand() % 256, rand() % 256, rand() % 256);}Ai[i].x = rand() % map_wide;Ai[i].y = rand() % map_hight;Ai[i].r = 10;Ai_body_num[i] = 5;Ai_body[i][0].x = Ai[i].x - Ai[i].r;Ai_body[i][0].y = Ai[i].y;Ai_body[i][0].r = Ai[i].r;for (int z = 1; z < Ai_body_num[i]; z++){Ai_body[i][z].x = Ai_body[i][z - 1].x - Ai_body[i][z - 1].r;Ai_body[i][z].y = Ai_body[i][z - 1].y;Ai_body[i][z].r = Ai_body[i][z - 1].r;}}}
三.玩家碰到AI尾部
for (int j = 0; j < Ai_body_num[i]; j++){if (Distance(Ai_body[i][j].x, Ai_body[i][j].y, player.x, player.y) < player.r + Ai_body[i][j].r){body_num = 5;score = 100;kill = 0;player.x = Wide / 2;player.y = Hight / 2;player.r = 10;player_body[0].x = player.x - player.r;player_body[0].y = player.y;player_body[0].r = player.r;for (int i = 1; i < body_num; i++){player_body[i].x = player_body[i - 1].x - player_body[i - 1].r;player_body[i].y = player_body[i - 1].y;player_body[i].r = player_body[i - 1].r;}}}
四.AI触碰AI
for (int p = 0; p < Ai_num; p++){for (int j = 0; j < Ai_body_num[i]; j++){if (Distance(Ai_body[p][j].x, Ai_body[p][j].y, Ai[i].x, Ai[i].y) < Ai[i].r + Ai_body[p][j].r && i != p){Ai_body_food_num[i] = Ai_body_num[p];for (int z = 1; z < Ai_body_num[i]; z++){Ai_body_food[i][z].x = Ai_body[i][z].x;Ai_body_food[i][z].y = Ai_body[i][z].y;Ai_body_food[i][z].r = Ai_body[i][z].r;Ai_body_food[i][z].color = RGB(rand() % 256, rand() % 256, rand() % 256);}Ai[i].x = rand() % map_wide;Ai[i].y = rand() % map_hight;Ai[i].r = 10;Ai_body_num[i] = 5;Ai_body[i][0].x = Ai[i].x - Ai[i].r;Ai_body[i][0].y = Ai[i].y;Ai_body[i][0].r = Ai[i].r;for (int z = 1; z < Ai_body_num[i]; z++){Ai_body[i][z].x = Ai_body[i][z - 1].x - Ai_body[i][z - 1].r;Ai_body[i][z].y = Ai_body[i][z - 1].y;Ai_body[i][z].r = Ai_body[i][z - 1].r;}}}}
五.AI吃AI遗留的食物
for (int p = 0; p < Ai_num; p++){for (int i = 0; i < Ai_num; i++){for (int j = 0; j < Ai_body_food_num[i]; j++){if (Distance(Ai_body_food[i][j].x, Ai_body_food[i][j].y, Ai[p].x, Ai[p].y) <Ai[p].r + Ai_body_food[i][j].r + player_speed){int v = rand() % food_num;Ai[p].r += Ai_body_food[i][j].r / 5 * 0.01;Ai_body[p][0].r = Ai[p].r;for (int z = 1; z < Ai_body_num[p]; z++){Ai_body[p][z].r = Ai_body[p][z - 1].r;}Ai_body_food[i][j].x = food[v].x;Ai_body_food[i][j].y = food[v].y;Ai_body_food[i][j].r = food[v].r;Ai_body_food[i][j].color = food[v].color;Ai_body_num[p] += Ai_body_food[i][j].r / 5;Ai_body[p][Ai_body_num[p] - 1].x = Ai_body[p][Ai_body_num[p] - 2].x - Ai_body[p][Ai_body_num[p] - 2].r;Ai_body[p][Ai_body_num[p] - 1].y = Ai_body[p][Ai_body_num[p] - 2].y;Ai_body[p][Ai_body_num[p] - 1].r = Ai_body[p][Ai_body_num[p] - 2].r;}}}}
六.玩家吃食物
srand((unsigned)time(NULL));for (int i = 0; i < food_num; i++){if (Distance(food[i].x, food[i].y, player.x, player.y) < player.r + food[i].r + player_speed){score += 3;player.r += 0.01;player_body[0].r = player.r;for (int j = 1; j < body_num; j++){player_body[j].r = player_body[j - 1].r;}food[i].x = rand() % map_wide;food[i].y = rand() % map_hight;food[i].r = 5;body_num += 1;player_body[body_num - 1].x = player_body[body_num - 2].x - player_body[body_num - 2].r;player_body[body_num - 1].y = player_body[body_num - 2].y;player_body[body_num - 1].r = player_body[body_num - 2].r;}
七.玩家吃AI遗留的食物
for (int i = 0; i < Ai_num; i++){for (int j = 0; j < Ai_body_food_num[i]; j++){//玩家1吃尸体if (Distance(Ai_body_food[i][j].x, Ai_body_food[i][j].y, player.x, player.y) < player.r + Ai_body_food[i][j].r + player_speed){int v = rand() % food_num;score += Ai_body_food[i][j].r / 5 * 3;player.r += Ai_body_food[i][j].r / 5 * 0.01;player_body[0].r = player.r;for (int z = 1; z < body_num; z++){player_body[z].r = player_body[z - 1].r;}Ai_body_food[i][j].x = food[v].x;Ai_body_food[i][j].y = food[v].y;Ai_body_food[i][j].r = food[v].r;Ai_body_food[i][j].color = food[v].color;body_num += Ai_body_food[i][j].r / 5;player_body[body_num - 1].x = player_body[body_num - 2].x - player_body[body_num - 2].r;player_body[body_num - 1].y = player_body[body_num - 2].y;player_body[body_num - 1].r = player_body[body_num - 2].r;}}}