如何让敌人在一定距离内随机循环并跟随玩家?c++主机游戏
How to make enemies circulate randomly and follow player if withing certain distance? C++ Console game
我一直在尝试让Z随机移动几个小时。我的目标是什么;-
- 让僵尸(Z)在地图上随机移动
- 跟随玩家(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;
}
你可能缺少的是为僵尸保留一些状态,这比在地图上搜索角色'Z'
更容易访问。例如,在一个结构中持有x,y坐标,并拥有它们的数组来表示僵尸。
struct position {
int x, y;
};
#define NUM_ZOMBIES 5;
struct position zombies[NUM_ZOMBIES];
在main中,您将围绕当前创建僵尸的代码创建一个简单的循环,而不是直接将地图中的角色设置为"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 };
你可以改变你的刷新功能来绘制僵尸到地图上,在你把僵尸打印到屏幕上之前把它们添加到地图上。
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_toward,但是这里有它们的原型来帮助你。
bool in_range(struct position zombie, struct position player);
struct position move_towards(struct position zombie, struct position player);
"我已经尝试过了,只能让它工作,如果一个按钮被按下"
这是因为您使用了_getch()
。首先,注意前面的下划线。这表明该函数是Microsoft扩展。这不是问题的真正原因,但这意味着你的代码只能在Windows上运行。然后,你有所有这些GetAsyncKeyState
调用,所以无论如何这是给定的。
真正的原因是_getch()
可能等待键盘输入。如果你不按一个键,什么也不会发生。特别是,当_getch()
等待时,没有(Z)僵尸移动。还有另一个微软函数,_kbhit
,它会告诉你如果键被击中(但不是哪个);如果没有按键,可以跳过_getch
呼叫。
但是你为什么要调用_getch
呢?您没有使用返回的值。我可以预测一件事:如果你移除按键限制,你的僵尸将会移动得非常快。您将需要另一个限制
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 如何让敌人在一定距离内随机循环并跟随玩家?c++主机游戏