gpt4 book ai didi

c++ - 如何让敌人随机循环并在一定距离内跟随玩家? C++ 控制台游戏

转载 作者:行者123 更新时间:2023-11-28 07:01:49 25 4
gpt4 key购买 nike

几个小时以来,我一直在尝试让 Z 随机移动。我的目标是什么;-

  • 让僵尸(Z)在 map 上随机移动
  • 如果玩家(M)在一定距离之外就跟随他

我对编程完全是个业余爱好者,我非常感谢您的帮助!完整代码如下:

#include <iostream>
#include <Windows.h>
#include <time.h>
#include <conio.h>
using namespace std;



inline void Refresh(char q[50][50]){
system("cls");
int i, j;
cout << endl;
for (i = 0; i < 50; i++) {
cout << "\t";
for (j = 0; j < 50; j++)
{
cout << q[i][j];
}
cout << endl;
}
}


int launch(char q[][50], int a1, int a2, int p1, int p2, int x0, int y0);

//**************************Main Function*********************************************
int main(){
char map[50][50] = {
"#################################################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"#################################################"
};
// my character
int x0 = 1, y0 = 1;



// Randomly make and spawn the holes
int a1, a2;
srand(time(0));
do {
a1 = rand() % 48 - 1;
a2 = rand() % 48 - 1;
} while (map[a1][a2] != ' ');
map[a1][a2] = 'O';

// Randomly make and spawn the zombies
int p1, p2;
srand(time(0));
do {
p1 = rand() % 48 - 1;
p2 = rand() % 48 - 1;
} while (map[p1][p2] != ' ');
map[p1][p2] = 'Z';


launch(map, a1, a2, p1, p2, x0, y0);

}


int launch(char q[][50], int a1, int a2, int p1, int p2,int x0, int y0){
int over, x = x0, y = y0;
while (1) // Infinite loop
{
_getch();

// UP key
if (GetAsyncKeyState(VK_UP))
{
x = x - 1;
}
// DOWN key
else if (GetAsyncKeyState(VK_DOWN))
{
x = x + 1;
}
// LEFT key
else if (GetAsyncKeyState(VK_LEFT))
{
y = y - 1;
}
// RIGHT key
else if (GetAsyncKeyState(VK_RIGHT))
{
y = y + 1;
}


if (x == 0 || x == 50 || y == 0 || y == 50)
{
cout << "Game Over" << endl;
return 0;
}

if (x == p1 && y == p2) { // Touch the zombies
cout << "Game Over" << endl;
system("PAUSE");
return 0;
}

if (x == a1 && y == a2) { // Touch the holes
cout << "Game Over" << endl;
system("PAUSE");
return 0;
}

else { // the hero just moving around
q[x0][y0] = ' ';
q[x][y] = 'M';

Refresh(q);
}
x0 = x;
y0 = y;

}
return 0;
}

最佳答案

您可能缺少的是为僵尸保存一些状态,这比在 map 上搜索字符 'Z' 更容易访问。例如,将 x,y 坐标保存在一个结构中,并用它们组成的数组来表示僵尸。

struct position {
int x, y;
};

#define NUM_ZOMBIES 5;
struct position zombies[NUM_ZOMBIES];

然后,在 main 中,您将围绕当前创建僵尸的代码创建一个简单的循环,而不是直接将 map 中的角色设置为“Z”,而是设置僵尸的位置

for (int i=0; i<NUM_ZOMBIES; i++) {
int p1, p2;
srand(time(0));
do {
p1 = rand() % 48 - 1;
p2 = rand() % 48 - 1;
} while (map[p1][p2] != ' ');
zombies[i] = { .x = p1, .y = p2 };
}

为方便起见,我们也使用播放器的位置。

struct position player = {.x = x0, .y = y0 };

然后,您可以更改 Refresh 函数以将僵尸绘制到它上面,方法是在将僵尸打印到屏幕之前将它们添加到 map 中。

for (int i=0; i<NUM_ZOMBIES; i++) {
q[[zombies[i].x][zombies[i].y] = 'Z';
}

所以最后一部分是测试它们是否在范围内。您在主循环中有另一个循环,它同样循环遍历所有僵尸,比较与玩家的距离,如果它们在您想要的范围内,您就开始跟随

for (int i=0; i<NUM_ZOMBIES; i++) {
if (in_range(zombies[i], player)) {
zombie[i] = move_towards(zombies[i], player);
}
}

我会留给您实现 in_range 和 move_towards,但这里有它们的原型(prototype)可以帮助您。

bool in_range(struct position zombie, struct position player);
struct position move_towards(struct position zombie, struct position player);

关于c++ - 如何让敌人随机循环并在一定距离内跟随玩家? C++ 控制台游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22338048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com