Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使对象从点A移动到点B,但点B正在移动?C++ 我正在制作一个简单的游戏来帮助我学习基本的C++代码。我想让僵尸能够在2d蛇形环境中跟随/追逐玩家。虽然我还没能找到一个方法让它发挥作用 #include <iostream> #include <Windows.h> #include <conio.h> using namespace std; const int width = 60; const int height = 20; int x, y; int zombX, zombY; void Setup() { x = width / 2; y = height / 2; zombX = rand() % width; zombY = rand() % height; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; for (int i = 0; i < height; i++) //i = height { for (int j = 0; j < width; j++) { if (j == 0) cout << "*"; if (i == y && j == x) cout << "I"; else if (i == zombY && j == zombX) cout << "Z"; else { bool print = false; if (!print) cout << " "; } if (j == width - 1) cout << "*"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; }_C++ - Fatal编程技术网

如何使对象从点A移动到点B,但点B正在移动?C++ 我正在制作一个简单的游戏来帮助我学习基本的C++代码。我想让僵尸能够在2d蛇形环境中跟随/追逐玩家。虽然我还没能找到一个方法让它发挥作用 #include <iostream> #include <Windows.h> #include <conio.h> using namespace std; const int width = 60; const int height = 20; int x, y; int zombX, zombY; void Setup() { x = width / 2; y = height / 2; zombX = rand() % width; zombY = rand() % height; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; for (int i = 0; i < height; i++) //i = height { for (int j = 0; j < width; j++) { if (j == 0) cout << "*"; if (i == y && j == x) cout << "I"; else if (i == zombY && j == zombX) cout << "Z"; else { bool print = false; if (!print) cout << " "; } if (j == width - 1) cout << "*"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; }

如何使对象从点A移动到点B,但点B正在移动?C++ 我正在制作一个简单的游戏来帮助我学习基本的C++代码。我想让僵尸能够在2d蛇形环境中跟随/追逐玩家。虽然我还没能找到一个方法让它发挥作用 #include <iostream> #include <Windows.h> #include <conio.h> using namespace std; const int width = 60; const int height = 20; int x, y; int zombX, zombY; void Setup() { x = width / 2; y = height / 2; zombX = rand() % width; zombY = rand() % height; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; for (int i = 0; i < height; i++) //i = height { for (int j = 0; j < width; j++) { if (j == 0) cout << "*"; if (i == y && j == x) cout << "I"; else if (i == zombY && j == zombX) cout << "Z"; else { bool print = false; if (!print) cout << " "; } if (j == width - 1) cout << "*"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "*"; cout << endl; },c++,C++,在主循环中,在调用Draw之前,更改僵尸的位置。使其在X或Y轴上的位置更靠近玩家一步 这很容易做到,因为你知道玩家的当前位置。若僵尸-X高于玩家-X,则减少僵尸-X。若相反,则增加僵尸-X。和Y位置相同 if(X > ZombX) ZombX += random(2); else ZombX -= random(2); if(Y > ZombY) ZombY += random(2); else ZombY -= random(2); 要使僵尸不只是上下或左右行走,

在主循环中,在调用Draw之前,更改僵尸的位置。使其在X或Y轴上的位置更靠近玩家一步

这很容易做到,因为你知道玩家的当前位置。若僵尸-X高于玩家-X,则减少僵尸-X。若相反,则增加僵尸-X。和Y位置相同

if(X > ZombX) 
  ZombX += random(2);
else
  ZombX -= random(2);
if(Y > ZombY)
  ZombY += random(2);
else
 ZombY -= random(2);
要使僵尸不只是上下或左右行走,请在主循环中每秒调整僵尸的X位置,并每秒调整僵尸的Y位置。或者您可以随机选择X或Y


简而言之,您的B点始终是玩家当前的位置。

您的游戏逻辑应该是这样的

Setup();
do
{
 Draw();
 Delay();
 Update();
}while(!bIsEnd);
if(X > ZombX) 
  ZombX += random(2);
else
  ZombX -= random(2);
if(Y > ZombY)
  ZombY += random(2);
else
 ZombY -= random(2);
“更新”是更新对象位置的位置。 包括检查任何人工输入键盘/鼠标和 基于它移动对象。 某些对象会自动移动

此函数用于放置更新位置的logicI&Z 我假设‘I’是人类控制的,‘Z’是计算机控制的人工智能。 对于“I”,您可以使用功能和do开关盒从键盘获取输入。 对于“Z”,因为这是一个游戏,你应该让它具有挑战性。 不要太愚蠢或太聪明。 您可以为运动X或Y方向指定随机值。 对于每4次更新调用,您可以通过从 “I”和“Z”位置

if(X > ZombX) 
  ZombX += random(2);
else
  ZombX -= random(2);
if(Y > ZombY)
  ZombY += random(2);
else
 ZombY -= random(2);
4个更新调用就是一个例子。您可以使用另一个常量或开发新的逻辑


如果X==ZombX&&Y==ZombY,则设置bIsEnd。

如何每秒调整X位置?@Kamarkios如果某个计数器+++%2==0更改\u X\u位置;否则更改位置;你知道我将如何为僵尸制作一个产卵系统吗?比如,如果我想再添加两个具有相同移动、不同x和y位置的僵尸,我该怎么做?如果有一个链接到一本书或一段视频,我也可以看,这可能会很有帮助。这个问题非常广泛。请详细说明您的具体问题,并向我们展示您所做的工作